Unifying Search in Drupal: How to Use UNION Aggregated Fields with Search API
Capellic tackles a common Drupal search challenge by demonstrating how to unify date fields across content types using the UNION operator in Search API aggregated fields.
The blog post starts with a scenario where events from a content type and a custom entity need to appear together in search results, sorted by date. The problem arose because each entity stored its event date in a different field. This mismatch made sorting ineffective, prompting a deeper dive into how the Search API could resolve the issue.
The solution Capellic adopted was to use aggregated fields with the union configuration in the Search API. This approach enables combining multiple source fields into a single virtual field—useful for facets, filters, and consistent sorting. Crucially, this method works regardless of the search backend—Database, Solr, or Elasticsearch—and doesn’t require custom code.
The guide walks through configuration via the Drupal admin UI and provides YAML configuration snippets for various use cases: unified text search, date filters, taxonomies, and more. It emphasises best practices like using semantic field names, reindexing after changes, and keeping configurations version-controlled. Capellic recommends trying this approach before using Views Combine or custom hooks for a more maintainable, scalable solution.
