Pointing All Taxonomy Term Links to ApacheSolr Search Results
I’ve been working on a project that leverages ApacheSolr for many of the displays on the site. One of the requirements is that every instance of a taxonomy term link on the site needs to point to search results pre-filtered (using FacetAPI) on the term. The other requirement is that if a taxonomy term’s vocabulary doesn’t have an associated search facet, the user should end up on the search page with results as if the user had searched for the term’s name in the search text box.
- If you see a “Climate Change” term (tid = 153) link from the “Topics” vocabulary (vid = 8), it should render as <a href="/search/site?f=im_field_tr_topics%3A153">Climate Change</a> in the DOM.
- If you see a “Blog” term (tid = 123) link from the “Keywords” vocabulary (vid = 6), it should render as <a href="/search/site/blog">Blog</a> in the DOM.
Indexing Specific Field Collection Fields in Apache Solr for Drupal 7
I’m not sure how much of this is possible without Display Suite, but here’s the setup I’ve used to index specific fields within a field collection as part of the parent entity. I use the “Search index” view mode to specify which fields I want to index on each content type. Here’s what that looks like:
As you can see, the “Versions” field and the “Associated people / organizations” field are field collections. In this example I only wanted to index a couple of fields from the “Versions” field collection as part of the search index for each “Book” node.
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:
Rendering an Image in Apache SOLR Search results (Drupal 7)
There are a few ways to get extra information into the output of your Apache Solr Search results. The most efficient ways involve getting the information into the solr index and pulling it directly from the solr index. We do not want to introduce extra node_load() calls to render our search results. We do not want to have to use Views either.
So, using just apachesolr and some functions, I’m going to show how to get an image field into the search index and pull it back out in our result output. I know I’m not handling alt and title attributes in this example. I’ll let you figure that one out.
This is a step-by-step breakdown of my experience getting this to work. In this example I’m attempting to render an image field called field_forum_image for the forum (content type) nodes in my search results. You can use the same procedure for adding other fields, but please make sure you understand the solr prefixes (e.g., ss, ts, tm, etc.) before doing so. You can find these in the schema.xml file with in the apachesolr module.
Adding Metatag Keywords to Search API Index
For a great explanation of the implementation below, please visit http://enzolutions.com/articles/2015/06/02/how-to-add-customs-fields-to-solr-index-using-entity-api-amp-search-api/
The following demonstrates how to add metatag keywords (from the Metatag module) to your Search API fields list, so that you can index them and make them searchable.
Sorting by Specific Fields with Apache Solr
Recently I had to configure an Apache Solr search page to have the results sorted by a date field. We’re using the apachesolr module, which has a number of useful hooks, two of which we’ll use to accomplish this custom sort.
If you browse to Administration » Reports » Apache Solr search index you will see a list of the indexed fields (example shown below). Most of the documentation I’ve seen out there regarding apachesolr sorting says you can use $query->setAvailableSort() and $query->setSolrSort() within an implementation of hook_apachesolr_query_prepare() , but there is at least one caveat: the field you want to sort on must be a single-value field! Here’s a look at the field list on the site I’m working on: