Lemuel Santos 16 Feb, 2023
How to merge Drupal Facets with Search API SOLR


The Drupal Search API SOLR module, combined with the Facets module, has incredibly powerful search capabilities. I find it fascinating how these two modules pair together to provide an efficient solution for both simple and complex searches. I’d like to pay tribute to the people who maintain and service these modules. They provide excellent support and feedback within the acclaimed Drupal community.

https://www.drupal.org/project/search_api_solr
https://www.drupal.org/project/facets 

Here at Twel, we frequently use both modules and some “extra sauce” (custom code) to power tailored search capabilities. See the examples below:


https://www.roadmapwriters.com/collections/consultations 
https://nicholas.duke.edu/search
 

In several instances, we had to merge two or more facets into just one facet item. This can be achieved with a little bit of custom code, using the Search API Processor Plugin, found in your custom module directory. For example:

"docroot/modules/custom/my_site_search/src/Plugin/search_api/processor/SiteSearchCustomContentTypes.php"

In the following code, we merge facets for the "course" and "programs" content types into a single facet item, "courses_programs". We also merge "publication" and "center_publication" content types into the single facet item, "publications".

Using the Processor Plugin, your Search API should index the new value set for this custom indexed property as defined in the "getPropertyDefinition", named "site_search_custom_content_types" in our example above. Cool, right? 

Here’s another exercise: go to your Search API Solr index (e.g. “/admin/config/search/search-api/index/your_search_index/fields”) and add this "custom field" (new property from your custom code) to the list of fields to be indexed as shown in the image below:



Now, re-index your search and you will be able to use the new field/property as your new merged facets at "/admin/config/search/facets". 



If your website is content-heavy, this potent Drupal Search API module with SOLR can help. It’s able to retrieve lightning-fast results with facets, which are extraordinarily useful for searching through categorized content. Connect with us and we can help build or upgrade your website search!