I am using Search autocomplete with an exposed filter from Views. For autocomplete, I want to restrict the search to just the title field even though the actual search uses more fields than that.

It seems that all fields configured (in Views) for the exposed filter are searched. It does not matter what configuration I use on admin/config/search/search_api/index/*/autocomplete/search_api_views_*/edit: I can select a different "Views display" or I can select different fields in "Override used fields" but these settings seem to be ignored.

Am I doing something wrong or is this a bug?

Looking at the code from search_api_autocomplete_form_views_exposed_form_alter() I do not see that the configuration is being used anywhere. I suppose I can use hook_search_api_autocomplete_views_fulltext_fields_alter() to fix this. Is that what I am supposed to do?

Tested with 7.x-1.4 and 7.x-dev.

Comments

benjifisher created an issue. See original summary.

benjifisher’s picture

In case someone else finds it useful, here is the code I used:

/**
 * Implements hook_search_api_autocomplete_views_fulltext_fields_alter().
 *
 * Get Search API autocomplete to respect settings when used with an exposed
 * filter from Views.
 *
 * @see https://www.drupal.org/node/2773659
 */
function mymodule_search_api_autocomplete_views_fulltext_fields_alter(array &$fields, SearchApiAutocompleteSearch $search, view $view) {
  if (!empty($search->options['suggester_configuration']['fields'])) {
    $view->filter['search_api_views_fulltext']->options['fields']
      = $search->options['suggester_configuration']['fields'];
  }
}
drunken monkey’s picture

If you look at the beginning of search_api_autocomplete_autocomplete() (around line 30), you'll see that as long as fields are set in the autocomplete options, those passed from somewhere else will be completely ignored.

However, are you maybe using an older (more than a year) version of the "Database search" module? See #2483123: Narrowing autocomplete suggestions to one field – we had the problem there that the fields were completely ignored.

iamweird’s picture

Component: Code » General code

Yes, but it's $search->options['suggester_configuration']['fields'], not $search->options['fields'] for me (I've got 7.x-1.5).

marinex’s picture

Hi I have same issue:
search_api_db 1.7
search_api_autocomplete 1.6

It does not respect settings on a field in the views
fix.:My issue is related to the getAutocompleteSuggestions

After some digging I notice that code below override views field options:
File: SearchApiAutocompleteServerSuggester.php

  public function getAutocompleteSuggestions(SearchApiQueryInterface $query, $incomplete_key, $user_input) {
    if ($this->configuration['fields']) {
      $query->fields($this->configuration['fields']); // <----override field option because fields are set in the search_api_autocomplete_autocomplete - file:search_api_autocomplete.pages.inc
    }
    return $query->getIndex()->server()->getAutocompleteSuggestions($query, $this->getSearch(), $incomplete_key, $user_input);
  }

It is needed to fix!

After setting of configuration on administration all works as designed! Sorry!

marinex’s picture

Status: Active » Fixed

All works as designed!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.