In the module we have several drupal_alter() functions to alter search results, field mappings, queries and so on. But no one for altering the returned autocomplete suggestions.

I have made a tiny little patch which adds such an hook, and we can alter the suggestions to have whatever info we want, and not only the hardcoded data from the module.

Any support for this ??

Comments

ramlev’s picture

Added the patch

ramlev’s picture

StatusFileSize
new1.17 KB

New patch, sends more data from the alter hook.

ramlev’s picture

StatusFileSize
new1.17 KB

New patch, sends more data from the alter hook.

arnested’s picture

Issue summary: View changes
StatusFileSize
new1.28 KB

Just a reroll of the patch for 7.x-1.x.

drunken monkey’s picture

Status: Active » Needs work

Thanks for the suggestion and for re-rolling, and sorry I didn't see this earlier.
Makes a lot of sense, of course, especially in combination with the recent and upcoming feature additions to the Search API Autocomplete module. However, what's missing from the patch is documentation for the new hook in search_api_solr.api.php.
Also, I think we shouldn't call the hook twice, once for each search request (if there are two), but once with the final suggestions. We can just save both queries and responses in array keys. I.e., $alter_data['queries'][] = $query;.
Lastly, I think it would be more in line with Drupal's usual standards to have the suggestions you want to alter as the first parameter, and the additional data/context in the second.

If someone fixes these, I'll gladly commit the patch.

(Also, please set the status to "Needs review" if there is a patch.)

n20’s picture

Would i be able to alter search_api_autocomplete with this patch? Im a little bit confused since this patch is for search_api_solr

drunken monkey’s picture

Would i be able to alter search_api_autocomplete with this patch? Im a little bit confused since this patch is for search_api_solr

The thing is that the search backend you are using is responsible for creating the suggestions you're seeing (at least by default). Therefore, to be useful, an alter hook has to be placed in the search backend's code (Solr, in this case) to be useful.
So, with this patch, if you are using a Solr backend for your Search API server, you'll be able to alter the returned suggestions, yes.

arnested’s picture

Hi @drunken monkey,

I am looking in to your suggestions right now. Stay tuned.

arnested’s picture

Status: Needs work » Needs review
StatusFileSize
new2.85 KB

Another go at this. Includes @drunken monkeys suggestions from #5.

drunken monkey’s picture

Thanks a lot for revising the patch, looks much better!
However, I was of course mistaken in writing that multiple queries should be passed in the alter data – the query is always the same. Sorry about that. Both responses should be enough, I'd say, as they always occur in the same order anyways. If someone really has to know the exact Solr parameters, they can just alter the Solr query as well to remember it or include echoParams=true.

Anyways, here is a revised patch which is RTBC from my point of view. Would be great if you could still review it, though, and/or try it out.

arnested’s picture

Status: Needs review » Reviewed & tested by the community

Looks fine!

drunken monkey’s picture

Status: Reviewed & tested by the community » Fixed

Good to hear, thanks for reviewing!
Committed.
Thanks again, everyone!

Status: Fixed » Closed (fixed)

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