Adding a “Filter by year” Facet for a Date Field in Apachesolr for Drupal 7
A Drupal 7 site I’m working on makes use of the Apache Solr Search module and FacetAPI. Some of the content has a date field named field_s_date. I was tasked with providing a sidebar “Filter by year” facet block to filter the content by the year value of this date field. I’m pretty sure I could have done this a few different ways, but I settled on adding the year value as a separate field to the solr index and adding an associated facet using hook_facetapi_facet_info() .
First, to add the field, I used hook_apachesolr_index_document_build() in a custom module as follows:
1 2 3 4 5 6 7 8 |
/** * Implements hook_apachesolr_index_document_build(). */ function mymodule_apachesolr_index_document_build($document, $node, $namespace) { if (isset($node->field_s_date[LANGUAGE_NONE][0]['value'])) { $document->addField('ts_field_s_date_year', substr($node->field_s_date[LANGUAGE_NONE]['0']['value'], 0, 4)); } } |
After saving that and clearing the Drupal caches, I re-queued all of the content for reindexing and executed Index all queued content (in the apachesolr module configuration pages).
After all of the content was reindexed I checked if the field was appearing in my solr index. It appeared in the Schema Browser (part of the solr admin) as expected.
Though it wasn’t required I decided to make this field available to theme_preprocess_search_result() by implementing hook_apachesolr_query_alter() as follows:
1 2 3 4 5 6 |
/** * Implements hook_apachesolr_query_alter(). */ function mymodule_apachesolr_query_alter($query) { $query->addParam('fl', 'ts_field_s_date_year'); } |
Again, I cleared the Drupal cache after running this.
Lastly, I implemented hook_facetapi_facet_info() as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
/** * Implements hook_facetapi_facet_info(). */ function mymodule_facetapi_facet_info($searcher_info) { $facets = array(); // Facets are usually associated with the type of content stored in the index. if (isset($searcher_info['types']['node'])) { $facets['field_s_date_year'] = array( 'name' => 'field_s_date_year', 'label' => t('Shared date field: Year'), 'description' => t('Filter by the year value from field_s_date.'), 'field' => 'ts_field_s_date_year', 'field alias' => 'field_s_date_year', 'query types' => array('term', 'range_filter'), 'default widget' => 'links', ); } return $facets; } |
After saving that code, I cleared the Drupal caches one last time. The facet appeared in the Apachesolr Settings » Facets admin form. I enabled the facet, and then placed the block on the search page using context.
It worked well out-of-the box, but I changed a few of the options:
Here’s what it looked like after some styling:
Similar Example: Introducing a Fallback Value
Here’s another version that uses the nodes’ created date as a fallback. Notice how I changed the naming so that it doesn’t lead to the false expectation that all of the values are coming from field_pdate.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
/** * Implements hook_apachesolr_index_document_build(). */ function mymodule_apachesolr_index_document_build($document, $node, $namespace) { if (isset($node->field_pdate[LANGUAGE_NONE][0]['value'])) { $document->addField('ts_pubdate_year', substr($node->field_pdate[LANGUAGE_NONE][0]['value'], 0, 4)); } else { $document->addField('ts_pubdate_year', date('Y', $node->created)); } } /** * Implements hook_apachesolr_query_alter(). */ function mymodule_apachesolr_query_alter(DrupalSolrQueryInterface $query) { $query->addParam('fl', 'ts_pubdate_year'); } /** * Implements hook_facetapi_facet_info(). */ function mymodule_facetapi_facet_info($searcher_info) { $facets = array(); // Facets are usually associated with the type of content stored in the index. if (isset($searcher_info['types']['node'])) { $facets['pubdate_year'] = array( 'name' => 'pubdate_year', 'label' => t('Published date: Year'), 'description' => t('Filter by the year value from field_pdate or node\'s created date.'), 'field' => 'ts_pubdate_year', 'field alias' => 'pubdate_year', 'query types' => array('term', 'range_filter'), 'default widget' => 'links', ); } return $facets; } |