localgovdrupal / localgov_directories

Searchable, filterable, directories of information, and locations.
1 stars 3 forks source link

Autocomplete bringing in directory entries that are not part of the channel #293

Open andybroomfield opened 1 year ago

andybroomfield commented 1 year ago

This might be a search / solr issue but... We are encountering issues whereby the autocomplete for a directory search will bring in suggestions for entiries in unrelated directories. Also seeing this on default install. Not on demo but then I think something is wrong with demo content.

Screenshot 2023-06-28 at 12 44 56 pm
finnlewis commented 1 year ago

Interesting! Looks like there are hooks in search_api_autocomplete that might help:

https://git.drupalcode.org/project/search_api_autocomplete/-/blob/8.x-1.x/search_api_autocomplete.api.php#L27

danchamp commented 1 year ago

@finnlewis @andybroomfield I've had a closer look at this but can't work out how to pass a parameter to make autocomplete aware of which directory channel it's being called from to limit results. All it knows is the target search index and the query, and autocomplete hooks don't have access to the page route.

Hard-coding the channel id in hook_search_api_query_search_api_autocomplete_alter to update the query works and proves the issue, but can't be abstracted since the channel id can't be derived in the hook:

function localgov_directories_search_api_query_search_api_autocomplete_alter($query) {
  $channel_id = 224573;
  $query->addCondition('localgov_directory_channels', [$channel_id], 'IN');
}

A custom autocomplete search plugin may be an option, but beyond me: https://www.drupal.org/docs/8/modules/search-api-autocomplete/developer-documentation/providing-search-plugins

andybroomfield commented 2 months ago

Would the suggestion hook have access to the request object to get the channel from the current node via the route parameters?