This document discusses how to create custom indices in Solr to index additional fields beyond what is supported out of the box. It explains that custom indices allow indexing fields that are not normally indexed, such as file attachments, and fields with data types that are not supported by facet display widgets. It provides examples of naming conventions for custom indices and code samples for implementing hook_apachesolr_index_document_build() to add custom fields to the Solr document and hook_apachesolr_query_alter() to make those fields available in search results. Reindexing is required after adding custom code. The new facets can then be configured and displayed.
1. Solr Facets and Custom
Indices
Filtering Search Results in new
ways
2. 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
3. Solr and Drupal
ApacheSolr ( 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
4. 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
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.
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).
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.
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. 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. 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. 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. 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”.
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. 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. 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