{"id":1633,"date":"2016-02-28T18:48:48","date_gmt":"2016-02-28T22:48:48","guid":{"rendered":"http:\/\/agileadam.com\/?p=1633"},"modified":"2016-07-21T16:20:27","modified_gmt":"2016-07-21T20:20:27","slug":"adding-a-filter-by-year-facet-for-a-date-field-in-apachesolr-for-drupal-7","status":"publish","type":"post","link":"https:\/\/agileadam.com\/2016\/02\/adding-a-filter-by-year-facet-for-a-date-field-in-apachesolr-for-drupal-7\/","title":{"rendered":"Adding a “Filter by year” Facet for a Date Field in Apachesolr for Drupal 7"},"content":{"rendered":"

A Drupal 7 site I’m working on makes use of the Apache Solr Search<\/a> module and FacetAPI<\/a>. Some of the content has a date field named\u00a0field_s_date<\/em>. I was tasked with providing a sidebar “Filter by year” facet block to filter the content by\u00a0the 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()<\/span>\u00a0.<\/p>\n

First, to add the field, I used hook_apachesolr_index_document_build()<\/span>\u00a0in a custom module as follows:<\/p>\n

\/**\r\n * Implements hook_apachesolr_index_document_build().\r\n *\/\r\nfunction mymodule_apachesolr_index_document_build($document, $node, $namespace) {\r\n  if (isset($node->field_s_date[LANGUAGE_NONE][0]['value'])) {\r\n    $document->addField('ts_field_s_date_year', substr($node->field_s_date[LANGUAGE_NONE]['0']['value'], 0, 4));\r\n  }\r\n}<\/pre>\n

After saving that and clearing the Drupal caches, I re-queued all of the content for reindexing and executed\u00a0Index all queued content (<\/em>in the apachesolr module configuration pages).<\/p>\n

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.<\/p>\n

Though it wasn’t required I decided to make this field available to theme_preprocess_search_result()<\/span>\u00a0 by implementing hook_apachesolr_query_alter()<\/span>\u00a0\u00a0as follows:<\/p>\n

\/**\r\n * Implements hook_apachesolr_query_alter().\r\n *\/\r\nfunction mymodule_apachesolr_query_alter($query) {\r\n  $query->addParam('fl', 'ts_field_s_date_year');\r\n}<\/pre>\n

Again, I cleared the Drupal cache after running this.<\/p>\n

Lastly, I implemented hook_facetapi_facet_info() as follows:<\/p>\n

\/**\r\n * Implements hook_facetapi_facet_info().\r\n *\/\r\nfunction mymodule_facetapi_facet_info($searcher_info) {\r\n  $facets = array();\r\n\r\n  \/\/ Facets are usually associated with the type of content stored in the index.\r\n  if (isset($searcher_info['types']['node'])) {\r\n    $facets['field_s_date_year'] = array(\r\n      'name' => 'field_s_date_year',\r\n      'label' => t('Shared date field: Year'),\r\n      'description' => t('Filter by the year value from field_s_date.'),\r\n      'field' => 'ts_field_s_date_year',\r\n      'field alias' => 'field_s_date_year',\r\n      'query types' => array('term', 'range_filter'),\r\n      'default widget' => 'links',\r\n    );\r\n  }\r\n\r\n  return $facets;\r\n}<\/pre>\n

After saving that code, I cleared the Drupal caches one last time. The facet appeared in the Apachesolr Settings \u00bb Facets<\/em> admin form. I enabled the facet, and then placed the block on the search page using context.<\/p>\n

It worked well out-of-the box, but I changed a few of the options:<\/p>\n

\"2016-02-28_17-48-07\"<\/a><\/p>\n

Here’s what it looked like after some styling:<\/p>\n

\"2016-02-28_17-45-29\"<\/a><\/p>\n


\n

<\/a>Similar Example: Introducing a Fallback Value<\/strong><\/h2>\n

Here’s another version that uses the nodes’ created<\/em> 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<\/em>.<\/p>\n

\/**\r\n * Implements hook_apachesolr_index_document_build().\r\n *\/\r\nfunction mymodule_apachesolr_index_document_build($document, $node, $namespace) {\r\n  if (isset($node->field_pdate[LANGUAGE_NONE][0]['value'])) {\r\n    $document->addField('ts_pubdate_year', substr($node->field_pdate[LANGUAGE_NONE][0]['value'], 0, 4));\r\n  }\r\n  else {\r\n    $document->addField('ts_pubdate_year', date('Y', $node->created));\r\n  }\r\n}\r\n\r\n\/**\r\n * Implements hook_apachesolr_query_alter().\r\n *\/\r\nfunction mymodule_apachesolr_query_alter(DrupalSolrQueryInterface $query) {\r\n  $query->addParam('fl', 'ts_pubdate_year');\r\n}\r\n\r\n\/**\r\n * Implements hook_facetapi_facet_info().\r\n *\/\r\nfunction mymodule_facetapi_facet_info($searcher_info) {\r\n  $facets = array();\r\n\r\n  \/\/ Facets are usually associated with the type of content stored in the index.\r\n  if (isset($searcher_info['types']['node'])) {\r\n    $facets['pubdate_year'] = array(\r\n      'name' => 'pubdate_year',\r\n      'label' => t('Published date: Year'),\r\n      'description' => t('Filter by the year value from field_pdate or node\\'s created date.'),\r\n      'field' => 'ts_pubdate_year',\r\n      'field alias' => 'pubdate_year',\r\n      'query types' => array('term', 'range_filter'),\r\n      'default widget' => 'links',\r\n    );\r\n  }\r\n\r\n  return $facets;\r\n}<\/pre>\n

 <\/p>\n","protected":false},"excerpt":{"rendered":"

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\u00a0field_s_date. I was tasked with providing a sidebar “Filter by year” facet block to filter the content by\u00a0the 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()\u00a0. First, to add the field, I used hook_apachesolr_index_document_build()\u00a0in a custom module as follows:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[79],"tags":[95,84,83,282],"_links":{"self":[{"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/posts\/1633"}],"collection":[{"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/comments?post=1633"}],"version-history":[{"count":7,"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/posts\/1633\/revisions"}],"predecessor-version":[{"id":1711,"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/posts\/1633\/revisions\/1711"}],"wp:attachment":[{"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/media?parent=1633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/categories?post=1633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/tags?post=1633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}