It’s clear that when we want a professional and powerful search solution while using Drupal, the Search API module is the key. If you want to know more about how Search API can improve your website search, read our previous article Improving Drupal Search with Apache Solr to fully understand the benefits of applying this solution.
Search API has a fantastic interface to configure all fields we want to index along with pre-and post-processors to enhance the search results. However, sometimes we need to cover our requirements with complex logic or granularity on the information that we will be sending to the search server. In this situation, Search API allows us to create custom processors to apply custom logic during the indexation process.
The Search API is a replacement for the older Drupal search function(s). It is not a stand-alone feature. There needs to be a search engine which Search API interfaces with.
What’s a Processor on Search API?
A Processor is used to modify search queries and search results. In addition, a Processor can modify the data that will be indexed.
The processor works directly with the Search API, preparing the entities that will be indexed and sent to the Server. This preparation can involve different treatments of the data. The Search API module has documentation on all of the processors available.
To create our custom processors, Search API makes available a ProcessorPluginBase class based on the Drupal core Plugin API to create our plugin object-oriented way.
When we create a Node with Paragraphs, a simple interface mapping will help us to add these items to the index. When we add complexity to our workflow by adding Revisions on Nodes, we need to expand our solution.
In this scenario, we are indexing Paragraph entities with Search API, for this reason we need to know if this Paragraph is on the current Node Revision. In this case, we need to tell our Search API to prevent indexing of the Paragraphs NOT referenced on published revisions.
1- Create a custom processor plugin
To create a custom plugin to run our custom logic we’ll need to extend the ProcessorPluginBase. This plugin delivers a lot of options to customize indexes by adding, changing, or removing items. In our case, we’ll need to use the alterIndexedItems() function to evaluate paragraphs.
2- Enabled custom processor plugin
After creating the custom plugin we need to clear Drupal caches and navigate to the Processors configuration tab from the Search Index, then enable the processor. Re-index after applying these changes.
After re-indexing all items, our custom processor will skip Paragraphs not referenced on published versions of the Node.
Check out our article related to Search API How to merge Drupal Facets with Search API SOLR which shows the capability of Search API to merge some data before sending it to the search server.
Are you looking for a solution to improve your website search? Contact Us!
- “Available Plugin Types.” Drupal.org, 25 Mar. 2017
- Cyrwus, Oksana. “Drupal 8 + Search API: Exclude Content from Index Based on Field Value.” Forelgroup.com, 6 Feb. 2020.
- Processors.” Drupal.org, 25 Mar. 2017.