Menu icon
RedCrackle
Menu icon
Services
01Design Transformation
About UsCase StudiesBlogContact Us

<

Blog post

Configuring Drupal With Elasticsearch For Facet Search Functionality

Neerav Mehta

Founder & CEO

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:

Configuring Drupal With Elasticsearch For Facet Search Functionality

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:

Configuring Drupal With Elasticsearch For Facet Search Functionality

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:

Configuring Drupal With Elasticsearch For Facet Search Functionality

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:

Configuring Drupal With Elasticsearch For Facet Search Functionality

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:

Configuring Drupal With Elasticsearch For Facet Search Functionality

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:

Configuring Drupal With Elasticsearch For Facet Search Functionality Configuring Drupal With Elasticsearch For Facet Search Functionality Configuring Drupal With Elasticsearch For Facet Search Functionality Configuring Drupal With Elasticsearch For Facet Search Functionality

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:

Configuring Drupal With Elasticsearch For Facet Search Functionality

Search for “Elasticsearch Clusters” under ELASTICSEARCH CONNECTOR:

Configuring Drupal With Elasticsearch For Facet Search Functionality

In the new page, click “Add cluster”:

Configuring Drupal With Elasticsearch For Facet Search Functionality

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:
Configuring Drupal With Elasticsearch For Facet Search Functionality

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

Configuring Drupal With Elasticsearch For Facet Search Functionality

Click “Save”. The cluster has been saved:

Configuring Drupal With Elasticsearch For Facet Search Functionality

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

Configuring Drupal With Elasticsearch For Facet Search Functionality

A screen similar to the one below will be displayed:

Configuring Drupal With Elasticsearch For Facet Search Functionality

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:

Configuring Drupal With Elasticsearch For Facet Search Functionality

In the new page, click “Add server”:

Configuring Drupal With Elasticsearch For Facet Search Functionality

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":

Configuring Drupal With Elasticsearch For Facet Search Functionality

The server has been created:

Configuring Drupal With Elasticsearch For Facet Search Functionality

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".
Configuring Drupal With Elasticsearch For Facet Search Functionality

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:

Configuring Drupal With Elasticsearch For Facet Search Functionality

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:

Configuring Drupal With Elasticsearch For Facet Search Functionality

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”:

Configuring Drupal With Elasticsearch For Facet Search Functionality

Click “Taxonomy”:

Configuring Drupal With Elasticsearch For Facet Search Functionality

Click “Add vocabulary”:

Configuring Drupal With Elasticsearch For Facet Search Functionality

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

Configuring Drupal With Elasticsearch For Facet Search Functionality

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

Configuring Drupal With Elasticsearch For Facet Search Functionality

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

Configuring Drupal With Elasticsearch For Facet Search Functionality

Click “LIST” on the top-right:

Configuring Drupal With Elasticsearch For Facet Search Functionality

Click “Add term”:

Configuring Drupal With Elasticsearch For Facet Search Functionality

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

For “Genre”:

Configuring Drupal With Elasticsearch For Facet Search Functionality

For “Price ($)”:

Configuring Drupal With Elasticsearch For Facet Search Functionality

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:

Configuring Drupal With Elasticsearch For Facet Search Functionality

Click “Content types”:

Configuring Drupal With Elasticsearch For Facet Search Functionality

Click “Add Content type”:

Configuring Drupal With Elasticsearch For Facet Search Functionality

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:

Configuring Drupal With Elasticsearch For Facet Search Functionality

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:

Configuring Drupal With Elasticsearch For Facet Search Functionality

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:

Configuring Drupal With Elasticsearch For Facet Search Functionality

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:

Configuring Drupal With Elasticsearch For Facet Search Functionality

Click “Add content”:

Configuring Drupal With Elasticsearch For Facet Search Functionality

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

Configuring Drupal With Elasticsearch For Facet Search Functionality

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

Configuring Drupal With Elasticsearch For Facet Search Functionality

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:

Configuring Drupal With Elasticsearch For Facet Search Functionality

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

Configuring Drupal With Elasticsearch For Facet Search Functionality

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

Configuring Drupal With Elasticsearch For Facet Search Functionality

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:

Configuring Drupal With Elasticsearch For Facet Search Functionality

Click "demo index":

Configuring Drupal With Elasticsearch For Facet Search Functionality

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

Configuring Drupal With Elasticsearch For Facet Search Functionality

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

Configuring Drupal With Elasticsearch For Facet Search Functionality

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:

Configuring Drupal With Elasticsearch For Facet Search Functionality

The following page appears:

Configuring Drupal With Elasticsearch For Facet Search Functionality

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":

Configuring Drupal With Elasticsearch For Facet Search Functionality

You will find an entry just like below:

Configuring Drupal With Elasticsearch For Facet Search Functionality

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:

Configuring Drupal With Elasticsearch For Facet Search Functionality

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

Configuring Drupal With Elasticsearch For Facet Search Functionality

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

Configuring Drupal With Elasticsearch For Facet Search Functionality

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”:

Configuring Drupal With Elasticsearch For Facet Search Functionality

Cron was run successfully:

Configuring Drupal With Elasticsearch For Facet Search Functionality

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

Configuring Drupal With Elasticsearch For Facet Search Functionality

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”:

Configuring Drupal With Elasticsearch For Facet Search Functionality

Click “Add new view”.

Configuring Drupal With Elasticsearch For Facet Search Functionality

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.
Configuring Drupal With Elasticsearch For Facet Search Functionality

The screenshot below shows the summary of my view:

Configuring Drupal With Elasticsearch For Facet Search Functionality

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:

Configuring Drupal With Elasticsearch For Facet Search Functionality

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:

Configuring Drupal With Elasticsearch For Facet Search Functionality

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

Configuring Drupal With Elasticsearch For Facet Search Functionality

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!

Neerav Mehta

Neerav Mehta

Founder & CEO

Neerav Mehta is the Founder & CEO of Red Crackle. With sterling qualities, Neerav’s technological acumen is firing a generation of progressive companies on the digital path. With an undergraduate degree in Electrical Engineering from India's most prestigious institution IIT Bombay and having spent seven years developing and contributing to the launch of AMD's innovative line of computer products, Neerav founded Red Crackle where he is lauded for his dynamic and innovative genius.

View all posts

>

Read Next

10 Tips For Entrepreneurs In 2015

10 Tips For Entrepreneurs In 2015

Learn more

10 Ways To Increase Productivity At Work

10 Ways To Increase Productivity At Work

Learn more

30 best WordPress widgets for your site

30 best WordPress widgets for your site

Learn more

Let’s get you started!

Contact Us

>

RedCrackle

Explore

About Us

Services

Contact Us

Our address

5346 Gerine Blossom Dr,

San Jose, CA 95123

USA

Socials

Twitter
LinkedIn

© 2023 RedCrackle. All rights reserved.