function deplate_autocomplete_search_api_solr_query_alter(SolrQueryInterface $solr_query, QueryInterface $query) { if ($query->getOption('search_api_autocomplete')) { // Search ONLY in the clean custom field $solr_query->setQuery('ss_custom_value:*' . $query->getKeys() . '*'); // Group by the clean custom field (this removes duplicates + keeps exact text) $solr_query->addParam('group', 'true'); $solr_query->addParam('group.field', 'ss_custom_value'); $solr_query->addParam('group.limit', '1'); // Show the exact original phrase + the count (score) $solr_query->addParam('fl', 'ss_custom_value,score'); } }