0

Lets say I have a record with this string 'hairdresser doing great job' in the search index.

How do I make a search query 'hairdresser in Auckland' still return the record above in the search result?

I tried this but I feel it's not the right way to do it:

for word in query.split(' '):
  result = SearchQuerySet().filter_or(content=word)

And I don't want to use SOLR I feel it's overkill to install SOLR just to solve this kind of search that don't happen all the time.

j0k
  • 22,600
  • 28
  • 79
  • 90
James Lin
  • 25,028
  • 36
  • 133
  • 233

2 Answers2

0

An alternative, more DRY approach is to specify your own input type that handles converting a string to a series of or filters. You can also pass a Whoosh-specific query using the Raw input type.

If you only have to use this in a few places, I would stick with the way you do it now. Otherwise I would specify an input type for it.

EDIT: Whoosh's default parser uses simple boolean operators. As you can see in the documentation, the default operator is AND. To get the right query for Whoosh, simply do the following:

query = ' OR '.join(query.split(' '))
result = SearchQuerySet().filter(content=Raw(Clean(query)))

Clean auto-escapes the input to prevent malicious code injections.

knbk
  • 52,111
  • 9
  • 124
  • 122
  • Do you know the whoose syntax off your head? – James Lin Jul 28 '13 at 23:04
  • @JamesLin I updated my answer with the right code for the `Raw` input type. – knbk Jul 29 '13 at 10:26
  • I have reverted back to use 1.2.7 as whoosh won't narrow models and xapian is not well supported. Now I can only use raw_search(), but when I use it I get `unbound method Query_empty() must be called with Query instance as first argument (got str instance instead)` – James Lin Jul 29 '13 at 21:49
  • this is what I have `results = SearchQuerySet().raw_search('listing: "photo"').models(Listing)` – James Lin Jul 29 '13 at 22:48
  • @James You can't use default method chaining when you use `raw_search` , that's why the `models(Listing)` throws an error. That's the limitation of `raw_search`, it's a raw search query and nothing more. Either include the `models()` constraint in your raw search (if that's possible, I don't know the specifics of Whoosh's query parsing) or go with the `filter_or` method. – knbk Jul 30 '13 at 15:25
  • it's throwing the error even without the models() method. I have raised another question specifically for it http://stackoverflow.com/questions/17937641/haystack-xapian-raw-search-error BTW, how do I query only a specific model? I tried looking online but couldn't find any examples. – James Lin Jul 30 '13 at 19:22
0

I managed to achieve (sort of) this by using auto_query()

Either whoosh or xapian is capable of searching discard of order of the words in query, but won't match anything if the search query has words that don't exist in the index.

James Lin
  • 25,028
  • 36
  • 133
  • 233