Creating a Faceted Search View in Drupal 8 using the Search API Modules
- Database Search.
- Apache Solr Search.
In this article, We are going to see about “Database Search” server. Enable the “facets”, “Database Search” that ships with Search API module.
After enabling modules, go to configuration →Under “Search And Metadata” section you’ll find “Search API” section. You’ll find default Database server details and index part. You can edit and use default database server or you can create new database server from scratch. we will see how to create new server.
Creating Database Server:
On Search API page, Click on the edit link within “Default Content Index”.
Select the datasources from the list which you want to index on database server. Below that, under “configure the content source”, select the “All” option to be indexed. select the bundles whose content you don’t want to index.
Rest of the options keep it as it is and Save.
After that, Click on the “Fields” tab on top. Click on add fields section and add the fields for indexing and click on “done”.
On processor tab, you can select settings related to indexed data search. after you have saved all the changes, click on the “view” tab on top.
on view page, check if index status is showing 100%.if not, scroll down to the section called “Start Indexing Now”. click on the “Index Now” and you data will be index in database.
Once you done with indexing, now its time to create facets for your website using indexed data.
For this, I have created a product list using “views” as shown below:
Now we want to show a facet on the left side of the product page as shown below:
Go to configuration →Facets(under Search and Metadata), Click on “Add Facet” , Select the views page for which you want to create facets . Here we are creating for product list.
after that, you’ll get the list of fields which we added using search API. Select the field you want to use for facets. we are going to select “Brand”.
Click “save” and You will get settings for facets where you can customize your facets using Widgets, Sorting and Behaviour .
Once you create facets , Drupal will automatically create a block of the facets. go to structure →Block layout →choose region where you want to add facet.
Click “place block” to add block to that specific region.
At this point, you should have a fully operational Faceted block on your Drupal 8 site.
From here, you can customize the facets further, work on adding more facets (date, author, taxonomy term, etc.) to make the search work exactly as you’d like!