Faceted search with Search API in Drupal
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.
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.
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.
- Go to Configuration > Search and metadata > Search API.
- Create a server by clicking on add server, and choosing Database Search as service class. This one going to be the backend.
- 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.
- In settings, add the newly created server as the server.
- 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.
- Under Workflow tab there are filters and processors for the indexed data. Like excluding bundles from indexing, or lowercase all data, etc.
- 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".
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.
- Edit the index, and note that a new Facets tab appeared.
- 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.
- Set the display widget and other options, then save.
- 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.
Today most of the websites have search functionality. With the help of Apache Solr the time spent on waiting for a search result can be radically reduced. In this article we are going to set up a basic searching infrastructure on a *nix-based system.
What do you do when you need to search in files as well? For a recent project I had to enable users to search the content of attached files mainly in .pdf format. The Apache Solr with Tika seemed to be a good solution.