×
×

Configuring Drupal With Elasticsearch For Facet Search Functionality

Drupal offers an in-built Search module that can be helpful for basic and small websites. As a standalone, it is competent enough to deliver quick search results. But it doesn't provide the sophistication and customization sought by customers in search results today.

A suitable alternative will give you control over what gets displayed in search results on your site. For example, you may want only a specific product to display for a search while not displaying other active ones. You can achieve this and a lot more customized functionality by combining Drupal’s Search API with a 3rd party search tool.

Search API is a competent module offered by Drupal to help deliver the best search results. It understands Drupal's content architecture thoroughly. This module is primarily used by Drupal users to show and filter search results. You will have to download, unzip and enable it on your Drupal site. This will be covered soon.

Search API works perfectly with 3rd-party search servers such as Apache Solr, Xapian and Sphinx. This article will discuss Drupal’s integration with Elasticsearch to deliver exceptional search results.

Ever since its launch in 2010, Elasticsearch has gained a reputation as an efficient real-time search server. Deployed by companies such as Facebook, WordPress, Netflix and StumbleUpon, Elasticsearch's popularity will continue to grow in the years to come.

Let’s get started!

The goal of this article is to use Elasticsearch to power the Facet search functionality on a Drupal site.

Facet search is popularly used online, especially on sites such as Amazon.com to filter search results. For example, in the screenshot below, only sugar-free chocolates belonging to the Hershey brand are displayed:

We will achieve the same functionality in this article by combining Elasticsearch with Drupal’s very own Search API. Here is the kind of view we will be creating in this article:

This view features a collection of some great songs from the past. A user will be able to choose a product based on the Facet filters provided on the left.

Configuring Elasticsearch to search a Drupal site

1. Installing Elasticsearch

It's easy to install Elasticsearch. Visit https://www.elastic.co/downloads/elasticsearch to download and unzip the Elasticsearch distribution of your choice:

Use the command prompt to go to the newly created Elasticsearch folder. Run any of the following commands:

  • bin/elasticsearch (for UNIX users)
  • bin/elasticsearch.bat (for Windows users)

This command installs Elasticsearch on your machine.

2. Copying Elastica to your Drupal installation's Libraries folder

Before we proceed to configuring Elasticsearch with Drupal, you must copy Elastica’s files to your Libraries folder (//drupal/sites/all/libraries/). Elastica is a competent PHP client that works with Elasticsearch.

You can accomplish this by downloading the files from Github (https://github.com/ruflin/Elastica).

3. Downloading and enabling necessary Drupal modules

You will have to download and copy the following modules to your Drupal install’s Modules folder:

Downloading is easy. For example, clicking on any of the above links will take you to a Drupal.org page for the module. Go to the bottom of the page and download the version you would like to work with. In my case, I downloaded the tar.gz version as shown below:

The next step is to unzip these files to your Modules folder. I have used an SSH tool for this purpose.

After you are done extracting the files, you will have to enable the modules shown in the screenshots below. For this, click “Modules” on the top-level menu of your Drupal application:

You will be directed to a page featuring modules. The recently unzipped modules will be shown here. Enable the modules by ticking the boxes next to them. Here are the modules you will have to enable:

Click “Save configuration” at the bottom of the page.

4. Adding an Elasticsearch cluster

Just like Drupal, a node plays a central role in Elasticsearch terminology. It represents a working instance of Elasticsearch. The cluster we are going to add below represents a collection of such nodes.

A cluster is smart and can balance its workload to accommodate addition and removal of nodes.

Let’s now add the cluster. Click “Configuration” on the top level menu:

Search for “Elasticsearch Clusters” under ELASTICSEARCH CONNECTOR:

In the new page, click “Add cluster”:

Fill in the following values:

  • Administrator cluster name: Give a symbolic name to your cluster. I have named it as "Elasticsearch Cluster Demo".
  • Server URL: Provide the URL and the port that attends to Elasticsearch server. Most likely, the value will be “localhost:9200”. You can verify this by running the lsof –i :9200 command on your command prompt as shown below:

Leave the other values as default. Look at the screen below for more information:

Click “Save”. The cluster has been saved:

Click “Info” in the Edit dropdown on the right to view details of the newly added cluster:

A screen similar to the one below will be displayed:

5. Adding a Search API Server

Search API provides a handy framework that facilitates complex searches of Drupal entities. It offers support for displaying results by using Views, apart from other functionality.

Search API offers effortless integration with Elasticsearch. The first step is to add a Search API server.

Let’s start by going to the Configuration page again. Search for “Search API” under SEARCH AND METADATA:

In the new page, click “Add server”:

Fill in the following values:

  • Server name: Give a symbolic name to the server. I have named it as "Demo Search Saver".
  • Service class: Select "Elasticsearch Connector" service from the dropdown.
  • Cluster: Select the newly created cluster from the dropdown. In my case, it is "Elasticsearch Cluster Demo".

Click "Create server":

The server has been created:

6. Adding a Search API Index

Go to Configuration and click “Search API” like you did in the preceding step. Click “Add index”:

Fill in the following details:

  • Index name: Give a symbolic name to the index. I have named it as "demo index".
  • Item type: Choose the kind of content you would like to apply the search functionality for. I chose "Node" from the dropdown. As soon as you choose this value, two new check boxes will appear under the title Bundles. I have ticked both the boxes for "Basic page" and a new content type created solely to showcase faceted search – “Music For The soul”.
  • Enable: Tick this box if you want to make the index functional.
  • Server: Select the recently created server – "Demo Search Server".

Go further below the page to view the following fields:

  • Index items immediately: I have ticked this box to ensure faster indexing of updates made to the site.
  • Cron batch size: I have kept the default value “50”.
  • Select index: This dropdown is a required field. I created a random index named “demoindex”.

Click "Create index" to save your settings. Refer the screenshot below for more info:

After you create the index, you should specify the fields you wish to be indexed. In my case, I am ticking the boxes for fields associated with the “Music for the soul” content type:

The index has been saved.

7. Creating taxonomy tags

It’s now time to create content that can be found through Facet search.

The first step is to create Taxonomy tags that will be used as filters to enable enhanced search functionality.

Click “Structure”:

Click “Taxonomy”:

Click “Add vocabulary”:

Provide a name for the vocabulary to be used as filters:

I have created 2 Vocabulary types. They are “Genre” and “Price ($)”:

Let’s add tags for each vocabulary. Click “edit vocabulary” against the newly created Vocabulary.

Click “LIST” on the top-right:

Click “Add term”:

Add the tags you want to filter the final search results. These are the tags I have chosen:

For “Genre”:

For “Price ($)”:

8. Configuring fields for “Music for the soul” content type

Go to "Administration" -> "Structure" -> "Content types"

Let’s create a unique content type for this project.

This can be done by clicking “Structure” on the top-level menu:

Click “Content types”:

Click “Add Content type”:

You will get a screen similar to the one below. Give an appropriate name for your content type. I have named mine as "Music For The Soul". I intend to create content nodes featuring some of America's biggest hit numbers. Click “Save and add fields” at the bottom of the page:

You will be taken to a page that asks you to specify fields for your content type. These are the fields I chose:

  • Title
  • Artist
  • CD Cover
  • About the song
  • Genre
  • Year of release
  • Price

Let’s take a closer look at them in the screenshot below:

Notice the field type for “Genre” and “Price ($)”. Selecting “Term reference” from the dropdown will allow you to associate these fields with the Taxonomy terms created by you above.

For example, the Vocabulary dropdown you see in the next screen is where you specify the reference tag. This is for mapping “Price ($)” with the “Price($)” vocabulary type created in Step 7 of this article:

8. Creating content based on the new content type

With the fields ready, I can now create new content based on this content type.

Click “Content” on the top-level menu:

Click “Add content”:

Choose the content type. I am selecting the newly created one “Music For The Soul”:

You will get the following screen with the newly created fields. Provide values as required:

Further below on the page, you will be able to associate the song with Genre and Price tags. Choose a genre and price. The values you provide here will help with the facet search later in the article:

I am going to add different artists and their hit songs. Here is “Run To You” by Bryan Adams:

I have created 8 entries in total. Some of them can be viewed below:

9. Further Configuring The Search API Index

Let’s configure our custom index "demo index".

Go to "Configuration" and click "Search API" under "SEARCH AND METADATA" like you did before:

Click "demo index":

You will get the following screen. Notice the tabs on the top-right. Click “FIELDS”.

Select the fields of your choice to be indexed. In our case, they are "Genre" and "Price ($)":

Click “Save changes” at the bottom of the page and you will be taken to the “Filters” tab. There are no changes to be made here.

Click “Save configuration” at the bottom of the page. You will be taken to the “Facets” tab. Click “configure display” for Genre:

The following page appears:

I have made 2 updates. I have chosen “Links with checkboxes” for the Display widget dropdown. This means that a user can view multiple products based on the choice they make

I have also chosen “OR” for Operator under GLOBAL SETTINGS. This allows the user to check the box against multiple boxes and view them at once.

Click “Save and go back to realm settings” and perform similar updates for “Price ($)”.

10. Confirming whether Facet blocks have been created or not

As soon as you specify fields for Facet search, blocks will be created for them. Let’s confirm whether blocks have been allocated for your Facet search.

Go to "Structure" and click "Blocks":

 You will find an entry just like below:

At first, both of them will be disabled. You will have to assign a region, preferably “Sidebar first” in the dropdown to activate the blocks.

11. Manually indexing the site

For the search functionality to work, it is important that all the new updates on the site should be indexed. Go to “Configuration” and search for “Search settings” under SEARCH AND METADATA:

You will notice a page that displays INDEXING STATUS. Click Re-index site”:

In the next screen, click “Re-index site” once again:

Sometimes this option won’t work because the Drupal system is waiting for a Cron run to take place.

If this is the case, you can also index your site by going to “Configuration” -> “Cron”. Click “Run cron”:

Cron was run successfully:

We can verify this by going to “Configuration” -> “Search Settings” once again:

As you can see there are no more items to index.

12. Configuring the search view to showcase the Music-related posts

It’s time to display the content types mentioned by you. Go to “Structure” and click “Views”:

Click “Add new view”.

Provide a name for your view.

  • In the “Show” dropdown, select the index you created recently. In my case, it is demo index. In the “sorted by” dropdown, select Unsorted.
  • Tick the check box for “Create a page”.
  • Provide a Page title and an easily recognizable path for this view. I named it “/beautiful-music”.
  • For the Display format, I chose Grid to highlight fields.

The screenshot below shows the summary of my view:

As can be seen in the screenshot above, I have added two fields for this view – “Title” and “CD Cover”. You will notice a Facets block tab on the top-left.

Save your settings and go to the view path specified by you. In my case, it is my primary Drupal site affixed with the path mentioned above – “/beautiful-music”. This is how the view looks for me:

Clicking the Title link above the image will take you to that particular song page. Notice the facet filters on the left. I am going to select songs that belong to the rock genre:

 

And I am going to further filter my search by selecting rock CDs that are priced at $5:

Conclusion

As can be seen above, the Facet functionality for filter search is working smoothly. Follow the instructions in this article to deploy the same functionality for your Drupal store!

Services: 
Drupal Development

Sign up for our weekly newsletter


Comments

  • by vblanco (not verified)
  • Mon, 05/30/2016 - 04:03

What version of elastica are using ?

I used 2.3.2 versin in Elaista and I've this error when use the face filters

Fatal error: Class 'Elastica\Facet\Terms

  • by Kim @ Hexagon I... (not verified)
  • Wed, 07/27/2016 - 00:14

Excellent info you have shared with us thanks for this!

Add new comment