Using autocomplete with the solr backend. Since moving to Facet API I noticed the autocomplete stopped working:

ResponseText: Warning: Invalid argument supplied for foreach() in getAutocompleteSuggestions() (line 1162 of C:\Inetpub\wwwroot\123.com\sites\all\modules\contrib\search_api_solr\service.inc). =>

It seems that in service.inc $search_fields = $query->getFields(); returns no fields.
In function search_api_autocomplete_autocomplete() I checked and indeed [fields:protected] is empty.

The field in my search view is "Search: Fulltext search" with a "contains" query and use as Search keys"

CommentFileSizeAuthor
#6 1286682--preprocess-query-6.patch684 bytesdrunken monkey

Comments

Anonymous’s picture

I have "hack" to solve my problem, but I don't understand why.

In search_api_autocomplete.search_api_views.inc, see the // new line

function search_api_autocomplete_views_query(SearchApiAutocompleteSearch $search, $complete, $incomplete) {
  $views_id = substr($search->machine_name, 17);
  $view = views_get_view($views_id);
  // @todo Let users select display
  $view->set_display();
  // @todo Determine arguments
  $view->pre_execute();
  $view->build();
  $query = $view->query->getSearchApiQuery();
  $query->keys($complete);
  // @morningtime: new line here
  $query->fields(array('search_api_aggregation_1'));
  return $query;
}

(see new line, I have 1 aggregated fulltext field called search_api_aggregation_1)
If I try $query->fields(array('search_api_views_fulltext')); it says it's not an indexed fulltext field.

What I don't understand is why my aggregated & index field is not added to autocomplete's fields?

drunken monkey’s picture

Huh. Beats me …
For me, everything still works fine. I also don't see any way this would be influenced by the Facet API integration. This uses Views to build the query, after all, so as long as the associated view works this should also be correct. Is the relevant filter defined on the „Master“ display or only for a specific display?

Anonymous’s picture

Yes it's in the Master view, I tried both "search_api_views_fulltext" and my aggregated field. It didn't work, for some reason the field is not added to $query in search_api_autocomplete_views_query().

Maybe it is related to how I show the search textfield. I use the code from example_form_example_search_form_alter() and attach it to a textfield (shown on the search View page, replacing the exposed form.)

It used to work fine, until recently, don't know what changed.

function search_api_widgets_form_search_api_widgets_views_search_form_alter(&$form, &$form_state) {

  // use this search id for autocomplete
  $search_id = 'search_api_views_shopcircuit_search';
  $search = search_api_autocomplete_search_load($search_id);
  if (!empty($search->enabled)) {
    $search->alterElement($form['query']);
  }
}

The search id exists of course.

drunken monkey’s picture

Hm, OK, what I could imagine is that the filter handler for the fulltext search recognizes there were no keywords input and thus does nothing, not even set the fields. However, the query should then use all fulltext fields by default, so that can't really be it, either.
Maybe try to debug SearchApiQuery::fields() and print the call trace if the array is empty. Or …

In function search_api_autocomplete_autocomplete() I checked and indeed [fields:protected] is empty.

… do you mean an empty array, or NULL, or not present at all?

Anonymous’s picture

It seems that SearchApiQuery::fields() is not even called and [fields:protected] exists, but is null (not an array).

drunken monkey’s picture

Status: Active » Needs review
StatusFileSize
new684 bytes

Ah, OK, now I see where this is coming from! As we don't call the query's execute() method, we don't get all the automagic preprocessing there. We could just copy that in the autocomplete callback, but I think the cleaner variant is to expose the query's preprocessing and use that directly.

Please try the attached patch along with the one in #1291346: Expose SearchApiQuery::preExecute() and postExecute().

Anonymous’s picture

Status: Needs review » Reviewed & tested by the community

Wow, genius. Those patches work for me.

drunken monkey’s picture

Status: Reviewed & tested by the community » Fixed

Great, committed.

Status: Fixed » Closed (fixed)

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