solr facets and custom indices
TRANSCRIPT
Solr Facets and Custom Indices
Filtering Search Results in new ways
Quick Intro to Solr
Solr (lucene.apache.org/solr)
Open Source Enterprise Search Platform Powerful full-text search Highly reliable, scalable and fault tolerant Faceted Search Java based, standalone server within a servlet
container (e.g. Jetty, Tomcat, Jboss, and the like) Large community
Solr and DrupalApacheSolr ( drupal.org/project/solr )
Faceted Search - Faceted search is supported if you use the facet API module.
More like this - "More like this" blocks can be added to any node page.
Search Pages - Multiple search pages Search Environments - Add multiple Solr Search cores Range Queries – (Requires Facet Api Slider) Full Entity support – Hooks for indexing custom entities Exportable - Configs are exportable
ApacheSolr Related Modules
• FacetAPI (drupal.org/project/facetapi)
• FacetAPI Bonus (drupal.org/project/facetapi_bonus)
• Apache Solr Attachments• Apache Solr User Indexing• Apache Solr Term Indexing• Apache Solr Multilingual• Apache Solr Views• …. and many more
What are Facets
Wikipedia: Faceted Search
Faceted search, also called faceted navigation or faceted browsing, is a technique for accessing information organized according to a faceted classification system, allowing users to explore a collection of information by applying multiple filters. A faceted classification system classifies each information element along multiple explicit dimensions, enabling the classifications to be accessed and ordered in multiple ways rather than in a single, pre-determined, taxonomic order.
What are Facets
Facets Prerequisites
Solr Set up
ApacheSolr Module installed and configured(see Docs link from project)
FacetAPI Module installed
(optionally) FacetAPI Bonus Module Installed
Note: The FacetAPI partially supports Drupal's internal search via the Faceted Navigation for Search module (search_facatapi).
Enabling Facets
Admin Configuration Apache Solr Search Settings (tab)→ → →
Enabling FacetsList of available Facets that can be enabled with settings options.
List is based on supported entity fields being indexed.
Enabled Facets generate blocks to be placed on page.
Facet Display SettingsUse Drop down.
Facet Dependencies Settings
Facet Filter Settings
Place BlocksAdmin Structure Blocks→ →
Note: Facets generally only show up when search results are displayed. So no need to limit to search pages.
Custom IndicesWHY?
Not all fields are indexed by Solr, for example, file attachments.
Not all field types are supported by FacetAPI display widgets. You can “convert” them to a string or text index which is supported by a display widget.
The Hooks/** * Build the Solr document object before passing it back * to be indexed. * Add index fields here. */function hook_apachesolr_index_document_build(
ApacheSolrDocument $document, $entity, $entity_type,$env_id)
Note: Formerly known as hook_apachesolr_update_index()
/** * Alter the query after it's prepared and cached. * */Function hook_apachesolr_query_alter( DrupalSolrQueryInterface $query)
Dynamic Solr FieldsCustom index fields need to be named in a way that Solr can understand it's type. The Drupal Solr configuration uses the following convention for dynamic fields:
<type><single/multiple>_<field name>
<type> = s for string, t for text, b for boolean, or i for integer<single/multiple> = s for single / m for multiple values<field name> is the name to id this field.
Note: Even though you can create boolean and numeric indices, there are not many facet display widgets that work well with this. String and Text types are best.
Some Examplestm_multiple_dynamic_text_areas
This would be an index who's content will contain multiple searchable text areas.
ss_single_string_field
This would be an index who's content will be a single string value.
Note: text type indices will be index by words contain in them. Strings are indexed “whole”.
Putting this together/*Implements hook_apachesolr_index_document_build(). */function my_module_apachesolr_index_document_build( ApacheSolrDocument $document, $entity, $entity_type, $env_id) { if(in_array($entity->type, array('article','book','etd','media'))) { if( ! empty($entity->field_attachments) ) { foreach($entity->field_attachments AS $lang => $attachments) { foreach ($attachments as $id => $attachment) { $document->setField('ss_has_field_attachments', 'Yes' ); if ( isset($attachment['filename'])) { $ext = strtoupper(pathinfo($attachment['filename'], PATHINFO_EXTENSION)); $document->setMultiValue('sm_field_attachments_ext', $ext); } } } } else { $document->setField('ss_has_field_attachments', 'No' ); } }}
Putting this together
/** * Implementation of hook_apachesolr_query_alter($query) * * Add the newly indexed fields from above to the query result. */function nph_search_apachesolr_query_alter($query) { $query->addParams(array( 'fl' => array('ss_has_field_attachments')) ); $query->addParams(array( 'fl' => array('sm_field_attachments_ext')) );}
Note: The key above has a lower case L not a 1
Activating The CodeIf not enabled, enable the new module with hooks.
If code added to existing module, clear cache so new hooks registered.
Rebuild the entire Solr index by:
Go to Admin Configuration Apache Solr Search→ → Use the “Delete the Search & Solr Index” button Use the “Queue all content for reindexing” button Repeatedly click the “Index queued content” button until all content sent.
(Less load on the system than Index All Queued content)
Wait for a few minutes until the Solr server has indexed everything.
Using the new IndicesAdmin Configuration Apache Solr Search Settings (tab)→ → →
Click on the Facets link
Enable the new file extension facet
Set your display settings:
Use checkbox list / “Or” this facet
Add the block to the sidebar
Voila!
Questions?