search_api_autocomplete_improved
About
This module provides enhanced autocomplete functionality with optimized caching and accurate result counting for Search API Solr autocomplete. It automatically discovers views and search_api configuration from search_api_autocomplete entities, eliminating the need for manual configuration.
Features
-
Automatic views and search_api discovery: Dynamically extracts views and search_api index information from
search_api_autocompleteSearch entities - Automatic duplicate removal: Eliminates duplicate suggestions based on normalized text
- Accurate result counts: Calculates real result counts for each suggestion
- Optimized caching: Uses Drupal's cache system with cache tags for proper invalidation
-
Automatic cache invalidation:
- When search index is updated (via event subscriber)
- On via generic
drush cr(cache rebuild)
- Zero configuration: Works out of the box with any Solr with Views autocomplete search
The problems we try to solve
- The count of the suggestions results is not correct (see option "Display result count estimates"). This is because the Solr suggester uses
suggesterinstead ofquery. - The number of the results in a suggester Solr component does not show the documents found (
df) but a term frequency number (tf). - The module search_api_solr (or any related) allows suggesting new Solr terms without any validation.
- We may end up with no results on Views even if the autocomplete keyword says it has results (usually a multi word result).
- The Solr suggester adds any new token at the end of the prefix without checking if the whole phrase has results. For example on the prefix
product originalit may suggest the suffix2026so the final keyword value isireland original2026which has no results. But it has results forproduct originaland2026individually. - The autocomplete shows duplicate keywords. This is happening when using multiple search_api_autocomplete Suggester plugins.
- The suggester adds suffixes that generate words which do not exist on the indexed values. For example it can suggest "peoples" when we type "people" in autocomplete.
Related issues
Most of the issues are coming from the search_api_solr module.
- Autocomplete doesnt show exact match, 2020 (https://www.drupal.org/project/search_api_solr/issues/2978909)
- Multiple keywords autocomplete issue, 2021 (https://www.drupal.org/project/search_api_autocomplete/issues/3202985)
- Duplicate suggestions not correctly filtered from autocomplete, 2018 (https://www.drupal.org/project/search_api_solr/issues/2943668)
How it works
The module uses hook_search_api_autocomplete_suggestions_alter() to alter the suggestions provided by search_api_autocomplete. The hook implementations are available in src/Hook/SearchApiAutocompleteImprovedHooks.php.
For each suggestion we do an additional call on Solr or Views to count (but with a cache on it to avoid performance issues).
For backward compatibility with Drupal 10, procedural hooks are maintained in the .module file. Drupal 11 will automatically use the new OOP hooks.
Plans
- Add tests similar to these on search_api_solr
- Optimize caching further with more advanced cache tags
- Create a new search_api_solr Suggester that fixes the issues described above