Solr facets and custom indices


Published on

Solr Facets and custom indices presentation to the Triangle Drupal user's group at Red Hat's wonderful new tower HQ.

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Solr facets and custom indices

  1. 1. Solr Facets and Custom Indices Filtering Search Results in new ways
  2. 2. Quick Intro to Solr 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
  3. 3. Solr and Drupal ApacheSolr ( )  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
  4. 4. ApacheSolr Related Modules • FacetAPI ( • FacetAPI Bonus ( • Apache Solr Attachments • Apache Solr User Indexing • Apache Solr Term Indexing • Apache Solr Multilingual • Apache Solr Views • …. and many more
  5. 5. 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.
  6. 6. What are Facets
  7. 7. 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).
  8. 8. Enabling Facets Admin Configuration Apache Solr Search Settings (tab)→ → →
  9. 9. Enabling Facets List 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.
  10. 10. Facet Display Settings Use Drop down.
  11. 11. Facet Dependencies Settings
  12. 12. Facet Filter Settings
  13. 13. Place Blocks Admin Structure Blocks→ → Note: Facets generally only show up when search results are displayed. So no need to limit to search pages.
  14. 14. Custom Indices WHY? 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.
  15. 15. 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)
  16. 16. Dynamic Solr Fields Custom 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.
  17. 17. Some Examples tm_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”.
  18. 18. 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' ); } } }
  19. 19. 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
  20. 20. Activating The Code If 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.
  21. 21. Using the new Indices Admin 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
  22. 22. Voila!
  23. 23. Questions?