Implement Faceted search with Solr in Drupal 8?
Blog

How to Implement Faceted search with Solr in Drupal 8?

Sometimes we need to implement a search functionality that looks similar to some of the renowned e-commerce site search (like Amazon, Flipkart and so on) as per the category, types and all.  For this kind of search, Facet is a good option. So what exactly Faceted search is?

Facet is a module, in Drupal 8, that provides a facility to arrange all the search results as per the category. Basically, it is an arrangement of search results based on categories, content type on indexed terms and content type.

Why we use Facets?

There are various reasons a Facet can be used:

  • It provides a drill-down search facility.
  • It can be used with default search and Solr as well.
  • It shows a number of item count for each category.
  • Facet provides a wide range of configurable setting in UI and so on.

Why Solr?

‘Speed’ is crucial for any website as well as for search engines success.

So the question here is how can we keep our Drupal system search fast when it has ’millions of content (like pages, nodes)? The answer is Apache Solr.

Faceting with Solr:

Solr supports a range of Faceting:

1. Field faceting - Retrieve the counts for all terms or just the top terms in any given field. The field must be indexed.

2. Range faceting – It can be used on any date field or numeric field that supports range queries. There are some parameters for range Faceting. For more, check Solr Ref Guide 6.6

3. Interval faceting - It allows users to set variable intervals and count the number of documents that have values within those intervals in the specified field.

4. Query faceting - This faceting returns a number of documents in the current search results that also match the given facet query.

5. Pivot faceting - It allows to break down the values by category and sub-categories.

Require Modules for Faceted-Solr:

Module to uninstall:

  • Search

Modules to install:

  • Facets
  • Search API
  • Search API Solr Search

How to use Apache Solr with Facet:

Follow the below steps to use Facet with Solr:

1. Create a Solr server: First, create a Solr server in your system. In order to have a better understanding, check out how to configure Apache Solr with Drupal for better content search.

Solr

2. Install Required Modules:

In Drupal 8, Composer is now a standard to include external PHP libraries.

The Search API Solr module requires Solarium codes, which is not included in the module repository. So if you download this module directly from drupal.org and put the module in your Module folder then it will show the Solarium Libraries missing error at the time of module enabled. So I recommend installing all the dependencies of a module via Composer.

Run the following commands to overcome this libraries dependency issue:

  1. $ composer config repositories.drupal composer packagist.drupal-composer.org
  2. $ composer require "drupal/search_api_solr 8.1.x-dev" (latest stable release)

After completing the above process, make sure you have all the required modules (Search API, Search API Solr Search and Facets ) in your module directory. Install all the required module from UI or using the drush command.

Modules

Note: You need to uninstall core “Search” module if you are not going to use that else it will conflict with solr and Facet search.

3. Configure Solr Server on the site:

Go to Search API configuration page (Configuration >> Search and metadata >> Search API). Here I am using the default Solr server, but you can create your own server by clicking on “Add Server” button.

Config Solr server

So I did some changes in configuration for default “Solr Server” by clicking on Edit link. Configuration changes similar to this:

Solr server setting.

After making the changes, visit the server status page on click on “Solr Server” link or from ‘/admin/config/search/search-api/server/default_solr_server’ path. If everything goes fine then you will find a pop-up box with the message “The Solr server could be reached” and “The Solr core could be accessed”. That means your Solr server connection is done successfully. Now you can use it.

Solr success configuration

4. Configure Search Index:

After configuring, Solr Server, you need to configure the Solr Index also. The Solr Search creates a default content index containing all published nodes. You can modify the configuration as per your requirement. To do this, click on the Edit link of “Default Solr content index”.

Search Index

5. Indexed Content

To check whether the index is working properly or not, click on the Search Index title (like Default Solr content index). You will find the indexed status and you can re-index all data from here also. Before re-indexing, you need to click on “Clear all indexed data” and then click on “Index now” button.

Content indexing

6. Solr Search view page:

The Solr search module creates a default view page for search results, which can be accessed at ‘/solr-search/content’.

Search page

7. Facet configuration:

You can configure Facet by visiting ‘/admin/config/search/facets’ path or from “Configuration >> Search and Metadata >> Facets”.

Create new Facet by clicking ‘Add Facets’ button. Here I am creating a Facet name Content type facet by following the below steps:

  • Click 'Add facet'.
  • In ‘Facet source’ drop-down box, choose the 'View Solr search content, display Page'.
  • Select “Content type (type)” in Field drop-down.
  • Name the facet 'Content-type'.
  • Click on ‘Save’ button

Facet configuration

After creating Facet, you can do some basic configuration like:

  • Select ‘Show the amount of results’ checkbox to display the count of results.
  • Select  ‘List item Label’ checkbox to display the labels of Content type instead of Machine name.
  • Do some other configuration as per your requirement.
  • Click on ‘Save’ button

The facet provides a block that can be placed in the theme region from Block layout. Here I have placed that block in Sidebar First region on my website. To place the block, click on the ‘Place block’ button from that particular region and search the block there.

Here I have created Facet with the name the ‘Content-type’ so that you can find a block with the similar name as Facet. After placing the block in that particular region, visit the search page at ‘/solr-search/content’. Here the facet block looks similar to the below screenshot:

Facet block

Have you used Faceted search earlier? Is there anything you'd like to clarify or see further clarified about Faceted search? Let me know in the comments!

References:

Faceted Apache Solr Search with Drupal 8
Faceting - Apache Solr Reference Guide
Tutorialspoint - Apache Solr - Faceting
Lucidworks - Faceted Search with Solr