Creating facets for Apache SOLR Multisite Search
This page shows how to create new facets for the SOLR Multisite search module.
This issue lists the steps I went through and are more like notes to self than real documentation.
Standard text facets
You first need to patch the module as described in the issue. I could post a patch, but pasting the code yourself guarantees you'll be able to apply the correction if the module evolves.
It basically renders the apachesolr_multisitesearch_enabled_facets_form function hookable.
You can then implement this in your module :
function yourmodule_apachesolr_multisitesearch_facets($rebuild = FALSE) {
$facets = array();
$facets['yourfacetfield'] = array(
'info' => t('yourmodule search: Filter by human_readable_name_of_your_field'),
'facet_field' => 'yourfacetfield',
);
return $facets;
}
To enable a block for your facet, you have to implement hook_block :
function yourmodule_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
// This code is copied from the original apachesolr_multisitesearch module
$enabled_facets = apachesolr_multisitesearch_enabled_facets('yourmodule');
// This line must be modified to take advantage of the new hook
$facets = yourmodule_apachesolr_multisitesearch_facets();
// Add the blocks
$blocks = array();
foreach ($enabled_facets as $delta => $facet_field) {
if (isset($facets[$delta])) {
$blocks[$delta] = $facets[$delta] + array('cache' => BLOCK_CACHE_PER_PAGE,);
}
}
return $blocks;
break;
case 'view' :
if (apachesolr_multisitesearch_has_searched()) {
// Get the query and response. Without these no blocks make sense.
$response = apachesolr_static_response_cache();
if (empty($response)) {
return;
}
$query = apachesolr_current_query();
// Here is the place where you inform Drupal about your blocks
switch ($delta) {
case 'yourfacetfield' :
return apachesolr_facet_block($response, $query, 'yourmodule', $delta, $delta, t('Filter by human_readable_name_of_your_field'));
}
}
}
}
Your facet should then be visible on the page admin/settings/apachesolr/multisite-filters.
Once you’ve enabled it, you should see a corresponding block on admin/build/block/list.
You can customize the block a bit if you like, and then assign it to a region.
You don’t have to worry about visibility settings as the various Drupal SOLR modules take care of this for you.
For a standard text facet, that’s all there is to it.
Numeric values based facets
If you need to build a facet based on a numeric field and want some understandable (mean : string) output, you have to code a callback function. Otherwise, numeric values will be shown which seldomly make sense for the user.
In the code for hook_block above :
case 'yourfacetfield' :
return apachesolr_facet_block($response, $query, 'yourmodule', $delta, $delta, t('Filter by human_readable_name_of_your_field'));
You must add a call to your callback function :
case 'yourfacetfield' :
return apachesolr_facet_block($response, $query, 'yourmodule', $delta, $delta, t('Filter by human_readable_name_of_your_field'), 'yourmodule_yourfacetfield');
You can name the function whatever you want, but a bit of namespacing never hurts.
You then have to code your callback function:
function yourmodule_yourfacetfield($facet) {
if (! $facet) {
return "No value";
}
// Fetch whatever text value matching the numeric id passed by the facet
// Here, as often, we just query the node database table
// but you can code any way to match your ids to string values
$sql = 'SELECT title FROM {node} WHERE nid = %d';
return db_result(db_query($sql, $facet));
}
Other considerations
- Beware if you personalized some fields in schema.xml. Some parsers render custom string fields unusable for faceting. With the standard type ‘string’, no problem.
- It’s a bit out of this page's scope, but anytime you change something in schema.xml, you have to restart the SOLR service, or the container running it (i.e /etc/init.d/tomcat restart or something like that) otherwise your changes are not validated.
- You can test your environment directly from the SOLR interface: http://yoursite.com/solr/select/?q=testtext&fl=*,score&sort=field_to_sor... desc&facet=true&facet.field=yourfacetfield. It can really be useful to test this way before spending hours wondering what's wrong with your code.
I used it to enable more than 10 facets on one of my sites. Feel free to comment.
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion