solr facets and custom indices

23
Solr Facets and Custom Indices Filtering Search Results in new ways

Upload: cgmonroe

Post on 29-Jan-2018

2.616 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Solr facets and custom indices

Solr Facets and Custom Indices

Filtering Search Results in new ways

Page 2: Solr facets and custom indices

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

Page 3: Solr facets and custom indices

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

Page 4: Solr facets and custom indices

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

Page 5: Solr facets and custom indices

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.

Page 6: Solr facets and custom indices

What are Facets

Page 7: Solr facets and custom indices

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).

Page 8: Solr facets and custom indices

Enabling Facets

Admin Configuration Apache Solr Search Settings (tab)→ → →

Page 9: Solr facets and custom indices

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.

Page 10: Solr facets and custom indices

Facet Display SettingsUse Drop down.

Page 11: Solr facets and custom indices

Facet Dependencies Settings

Page 12: Solr facets and custom indices

Facet Filter Settings

Page 13: Solr facets and custom indices

Place BlocksAdmin Structure Blocks→ →

Note: Facets generally only show up when search results are displayed. So no need to limit to search pages.

Page 14: Solr facets and custom indices

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.

Page 15: Solr facets and custom indices

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)

Page 16: Solr facets and custom indices

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.

Page 17: Solr facets and custom indices

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”.

Page 18: Solr facets and custom indices

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' ); } }}

Page 19: Solr facets and custom indices

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

Page 20: Solr facets and custom indices

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.

Page 21: Solr facets and custom indices

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

Page 22: Solr facets and custom indices

Voila!

Page 23: Solr facets and custom indices

Questions?