August 22, 2013
Drupal

Faceted search with Search API in Drupal

Author photo
Cheppers
Cheppers Zrt.

With Search API and Facet API it is easy to create facet blocks to narrow the result set of a view or a search. This tutorial covers the basics steps how to do this.

Faceted search with Search API in Drupal

Have you ever wondered how to create the cool filters on the sidebar of your favorite webshop? Every checkbox narrows the result set, and they even show how many potential results can you expect clicking on them. In Drupal Commerce, they are implemented using Search API and Facet API. The filters that narrows an existing result set are called facets.

Drupal Search API

Build it yourself

First of all, download and install Search API. This module provides a framework for creating searches. It requires a backend to operate. The backend module provides the connection between Search API and the search engine. The simplest solution is using Search API Database Search module. This module uses the default database as a search engine. This can be slow when there is a lot of data to search in, but the good news is that the backend can be replaced without any pain any time.

So, Search API and Database Search is installed, what next?

To use Search API you need to create an index. The search we are going to build can only use the data that is stored in the index by the search engine.

  1. Go to Configuration > Search and metadata > Search API.
  2. Create a server by clicking on add server, and choosing Database Search as service class. This one going to be the backend.
  3. There is a pre-created index shipped with Search API, called default_node_index. I suggest simply deleting it while it can cause problems when Features comes in picture.
  4. In settings, add the newly created server as the server.
  5. You can add data to the index in fields tab. You can see all fields and properties for all installed entities. It is also possible to add related fields at the bottom of the page. When selecting the type of the field, fulltext means that a string field will be indexed word by word.
  6. Under Workflow tab there are filters and processors for the indexed data. Like excluding bundles from indexing, or lowercase all data, etc.
  7. Under Status tab, press Index Now. This will update the index. The index is automatically updated on every cron run.

Now there are two ways of using the index for searches: Search Pages module, and views. Let's have a look of the latter one. Create a new view, but select the name of the search index as a base table ("Show"). Note that only the fields added to the index are available as filters, fields, or anything. Apart from that the view works as any other view, except some small limitations. So create a view and a page display for it, and optionally create an exposed filter for it, where the users can initiate the "search".

Facet API

Facets are filters that narrows an existing result set, like a view. Install Facet API module, and enable Search Facets in Search API. Facet API provides the interface, and Search Facets implements it providing facet links and facet check-boxes.

  1. Edit the index, and note that a new Facets tab appeared.
  2. Under Facets tab there is a row for every field in the index. After enabling them, configure display opens a new windows for the widget settings. Do not forget to press save after setting the check-boxes, because clicking on configure display will not save them.
  3. Set the display widget and other options, then save.
  4. Go to Structure/blocks and put the newly created facet blocks to their places. They will be only displayed on the search pages which is now the page of the view.

That's it. Facet API allows creating custom widgets, so there are a lot of modules that provides contributed widgets, like Search API ranges (A range slider) or Date Facets. Around Search API there is a rapidly growing ecosystem, new features and modules coming out frequently so it worths checking for new stuff every once in a while.

Related posts

Rules checklist
Author
2013-05-10
Drupal

Rules is one of the most sophisticated modules you can find in the drupal sphere. It can be used to build complex and flexible systems that respond to a specific event. The three most important components of the module are events, conditions and actions. A reaction rule always needs a specified event to happen on the site, but the action takes place only if the criteria match.

7 +1 steps to plan a successful Drupal website
Author
2014-08-05
Drupal

According to our experience the most usual approach for clients with web development needs is to contact multiple agencies with more or less vague ideas - asking for quotes, and then selecting a choice based on price. This approach is doomed to fail for two reasons.