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.

Click to expand

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.

Click to expand

Click to expand

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

  1. 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.

    Click to expand

  2. Under ADD RELATED FIELDS find your paragraph and field collection fields and add them.

    1. 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.

    Click to expand

    Click to expand

  3. 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.

    Click to expand

    Click to expand

  4. (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.

    Click to expand

  5. Navigate to the View tab of your search index. Make sure to index all of your data.

    Click to expand

  6. Navigate to the Search API Search Page configuration page admin/config/search/search_api/page, and click Edit next to your search page.

  7. Under Searched fields make sure to select the paragraph and field collection field we added earlier.

    Click to expand

Conclusion and Next Steps

This workflow can be applied to other fields as well. The key is to do the following:

  1. Add the related field to the index.
    1. 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.
  2. Add the newly added related field to the Searched fields under your search page configuration.
  3. 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.