How To Implement Faceted Search With Solr in Drupal 8? - Valuebound
How To Implement Faceted Search With Solr in Drupal 8? - Valuebound
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 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.
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.
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.
<img src=”1.png”>
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.
<img src=”2.png”>
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.
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.
<img src=”3.png”>
So I did some changes in configuration for default “Solr Server” by clicking on Edit link.
Configuration changes similar to this:
<img src=”4.png”>
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.
<img src=”5.png”>
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”.
<img src=”6.png”>
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 reindexing, you need to click on “Clear all indexed data” and then click on “Index now”
button.
<img src=”7.png”>
<img src=”8.png”>
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:
<img src=”9.png”>
After creating Facet, you can do some basic configuration like:
a. Select ‘Show the amount of results’ checkbox to display the count of results.
b. Select ‘List item Label’ checkbox to display the labels of Content type instead of Machine
name.
c. Do some other configuration as per your requirement.
d. 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 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:
<img src=”10.png”>
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!