Search Drupal 7 with Apache Solr Part 4 - Facet Filters

  • Tutorial

In previous articles, I mainly dealt with issues related to full-text search. This time we’ll talk about faceted filters for searching on Drupal using Apache Solr. For those who have not read the previous materials, I recommend that you familiarize yourself with them for a better understanding of the topic. Caution, under the cut a lot of pictures.

Why is a facet needed and what is it. A facet is a filter by a specific field. This filter works as a qualifier.

Consider a small example. You have the essence of a car and it is characterized by color and the manufacturer. Initially, you can select several colors in the filter.

But choosing a manufacturer’s company, you filter the selection and now only those colors that correspond to the selected manufacturer are available to you.

If you press Renault again, the filter will return to its original state.

Such functionality is widely used for searching in various information databases and catalogs.

So let's see how to configure facets for searching on Drupal, through Apache Solr. In past lessons, we have already installed the necessary modules and configured the Apache Solr server.

Now we need to perform a few more steps to configure the site:

1. First you need to install the facetapi module and Search facets (included in facetapi).

2. Now create new Auto Tipe content and add the fields for the color and the manufacturer’s company there.

3. We also need to change the index settings so that it indexes nodes of type auto. To do this, in the index settings, go to the workflow tab and configure the bundle filter.

4. Then go to the Fields tab and include the color and brand fields in the index.

So, we set up an index to search for nodes of type auto. Now let's go directly to the facets. If you have already enabled the necessary modules and cleared the cache (this is the most important thing in Drupal), then in the index settings you should have a new Facets tab. In this tab, you can enable facet filters for fields that are added to the index. You can specify settings for each facet, but about them a little later. For now, just turn on the facets and index our nodes with type auto.

Now you need to configure the output. For each facet filter, a regular block is created and you can display it as you like, for example, through regions or panels. Facets have one nuance - in order for them to be rendered - the view through which the request is made in Apache Solr should already work out. Simply put, you should first place a view with the results on the page, and facet blocks lower in level (respectively later in rendering). After I placed the facet blocks in the region, I got such a page.

Now let's talk about facet settings. In the screenshot you can see the settings form.

Consider the most important of them:
  • Display widget - allows you to select a widget for the facet. By default, widgets are available in the form of links and checkboxes. There are several additional widgets, but I will tell them in the next article. In it, we will learn how to write our own widget.
  • Sorting - determines by what criteria the values ​​in the facet block will be sorted when displayed.
  • The operator is AND or OR. In the case of AND, you can select only one value in each facet, and in the case of OR, several.
  • Display for searches - you can specify for which displays the facet will trigger. This can come in handy if you have several displays with search results on one page and you want to filter only one of them.

For clarity, I recorded a video with an example of the operation of facet filters

Also popular now: