tags:

views:

167

answers:

3

I looked into the DP 6 search API and did not see a hook that would let me alter the search keys before they are passed into the search module to execute the search. I want to do keyword expansion on the string that the user entered. For instance, if the user entered 'foo', I want to execute a search for 'foo' and 'bar'. There should ultimately be a UI for these mappings, but for a quick prototype I can hardcode the strings.

Where would you start putting code that does this? Did I miss a hook in the search API?

A: 

The Synonyms module does this for taxonomy terms. I still may need to do this for full-text search terms. Any suggestions?

cdonner
A: 

I haven't tried this, but after reading the docs I think that you can implement hook__search() in your module, do the keword expansion there and then do a do__search() with the expanded keys.

By the looks of it you would also have to do hook__update__index() too.

Jeremy French
This is not entirely conclusive to me. I assume the Drupal calls do__search() for all the modules on its own, so wouldn't I be searching twice? I can imagine that I can call do_search only for the keys that I want programmatically added. Since the will be OR'd, I can merge the result sets. This would still be a complicated solution for a simple problem. What I rather need to do is 'override' the do_search() method, but I don't know how.
cdonner
Yes, I didn't read enough of the docs for hook search preprocess, thought it was just for indexing.
Jeremy French
+2  A: 

The hook to use in this case is hook_search_preprocess. It allows you to edit the keys a user enters before a search is done. Beneficially, it also does this for text being indexed so you get the advantage of expansions on those words for the text being indexed as well.

codeincarnate
Cool, thanks. Wouldn't it be helpful if this was incorporated into the search api reference? http://api.drupal.org/api/group/search/6
cdonner
It would be very helpful. The Search API reference and the state of Search in Drupal is somewhat woeful and certainly would be an area where contribution would be appreciated. For instance, there is even a search hook which is not documented at all.
codeincarnate