0

I'm a Django newbie doing a primitive website. I installed haystack and Whoosh as its search engine cause it was the simplest thing to do. It works fine, but there is a problem and I don't know how to Google it. I have some categories on my site and I have indexed their names to search. So, when a user enters "Computing" it finds the computing category and links to it. But there is a problem. If a user enters "Comp" into search field, it doesn't find "Computing" at all. Is this something that can be configured and how?

EDIT:

What else have I tried? Installing haystack 2.0, following this tutorial, installing solr instead of whoosh, trying Ngram fields, rebuilding indexes 10 times, rewriting search_indexes.py. Everything. Doesn't work. If I type in Comp, it doesn't find Computing. Is there anything else I could do? I have noticed that in the tutorial above, everything works like a charm instantly.

darxsys
  • 1,560
  • 4
  • 20
  • 34

3 Answers3

3

When you do the usual:

SearchQuerySet().filter(title='Computing')

in Haystack 1.x, it filters on everything exactly matching 'Computing'.

You can change that behaviour by using Haystack's Field Lookups, for example, using 'contains' will filter on anything containing the given string (Computing, Utingcomp, Comp):

SearchQuerySet().filter(title__contains='Comp')

In Haystack 2.x, the default filter is 'contains', so it should behave as you would expect it to "out-of-the-box"

Tom
  • 5,835
  • 4
  • 25
  • 30
  • I really don't know how to setup this. I just went by tutorial to make it work and have no idea how to do this. – darxsys Jan 15 '13 at 15:38
  • The simplest possible thing that should work is upgrading to Haystack 2.x (pip install -e git+https://github.com/toastdriven/django-haystack.git@master#egg=django-haystack) - it's still in beta but I've found it stable enough for use. The next best thing, but not really covered by the tutorial (although you'll end up doing this sort of stuff as you get used to Django development), is to create your own search form (http://django-haystack.readthedocs.org/en/latest/views_and_forms.html#creating-your-own-form) with the above change. – Tom Jan 15 '13 at 16:16
  • Nah, this doesnt work. I installed haystack 2, installed whoosh, doesn't work. Installed solr, doesn't work. Ah, well. – darxsys Jan 16 '13 at 14:32
  • 2
    I've tried using the __contains field lookup. It always returns 0 results. I can do SearchQuerySet.all() and get results, but not with filter. It's killing me! Any suggestions? – AndrewSmiley Dec 11 '13 at 14:03
2

Check out the documentation on autocomplete. You need to setup your indices to support Ngram's, but this should be exactly what you need.

from haystack.query import SearchQuerySet

SearchQuerySet().autocomplete(content_auto='old')
# Result match things like 'goldfish', 'cuckold' & 'older'.
Shawn H
  • 1,227
  • 1
  • 12
  • 18
0

So, if I'm understanding, what you're looking for is the equivalent of 'LIKE' in SQL. The problem is search engines that back Haystack aren't like an RDBMS.

The low level implementation of this filter will involve using wildcard characters but most of the Haystack backends don't support a leading wildcard, something required for an icontains/endswith filter. However, since most backends support trailing wildcards, Haystack 2.x includes a startswith filter. The only case this doesn't handle is searching for the end of a word, which doesn't look to be possible.

So, if you have indexed:

"Look at our great discounts in Computer section"

Then the following Haystack query DO match:

SearchQuerySet().filter(title__startswith='comp')
# match!

Notice the difference between Django vs. Haystack startswith filters. Django startswith will match at the beginning of the complete sentence (i.e. a CharField), but the Haystack one will match at the beginning of a token (i.e. each word in a complete sentence).

Hope it helps!

Cartucho
  • 3,257
  • 2
  • 30
  • 55