Configure Drupal's Search API to Search Field Collections and Paragraphs
Intro
This tutorial will assume that you are somewhat familiar with the Search API module. It also assumes you are using the Search API with the Search API Database Search and Search API Pages modules. The goal here is to ensure paragraph and field collection fields are being indexed by Search API and associated with their host node.
Above is the node we will be referencing. I kept the field labels visible on purpose so we can clearly distinguish between the paragraph and field collection fields.
Above you can see that unique text from both fields has been indexed and is searchable.
Add Paragraph and Field Collections Fields To Your Search Index
-
If you have the Default node index enabled (this comes by default with Search API) navigate to
admin/config/search/search_api/index/default_node_index/fields
and enable any paragraph or field collection fields. If not, simply create an index and make sure to include the content type that hosts your paragraph and field collection fields. -
Under ADD RELATED FIELDS find your paragraph and field collection fields and add them.
- For the field collection field, you will need to first add the field collection field. Once added, you will be able to relate the field collection to the content type it’s associated with.
-
Once added, there will be additional fields available on the index page relating to your paragraph and field collection fields. Some of these fields are not helpful in a site search, such as the Field collection item ID or Revision_id. In my case, both my paragraph and field collections fields are long text fields, so I only enable the fields that pertain to the text field.
-
(Optional) Navigate to the Filters section of your search index and enable Highlighting. This is so you can see the specific string you searched highlighted on the results page.
-
Navigate to the View tab of your search index. Make sure to index all of your data.
-
Navigate to the Search API Search Page configuration page admin/config/search/search_api/page, and click Edit next to your search page.
-
Under Searched fields make sure to select the paragraph and field collection field we added earlier.
Conclusion and Next Steps
This workflow can be applied to other fields as well. The key is to do the following:
- Add the related field to the index.
- Add the related field to the host entity as demonstrated with field collection fields. This only needs to happened for fields such as field collection fields.
- Add the newly added related field to the Searched fields under your search page configuration.
- Re-index your data,
I have experienced issues creating the relationship between a field collection and its host entity when using Search API. If you run into this issue, try applying this patch to the field collection module.