2

I'm using haystack 1.2.7 + whoosh 2.4.0 in Django 1.4 (Python is 2.7)

Example: Search query "sear" should match items containing "search" and "sear" and "searching" (etc).

my settings:

HAYSTACK_SITECONF = 'verticalsoftware.search.search_sites'
HAYSTACK_SEARCH_ENGINE = 'whoosh'
HAYSTACK_WHOOSH_PATH = 'C:/whoosh/prodeo_index'
HAYSTACK_INCLUDE_SPELLING = True

search index:

class GalleryIndex(SearchIndex):
    text = indexes.CharField(document=True, use_template=True)
    content_auto = indexes.NgramField(model_attr='title') 
    def index_queryset(self):
        """Used when the entire index for model is updated."""
        return Gallery.objects.filter(date_added__lte=datetime.datetime.now())

also tried with EdgeNgramField and/or RealTimeSearchIndex

custom urlCONF:

from django.conf.urls.defaults import *
from verticalsoftware.search.views import SearchWithRequest

urlpatterns = patterns('haystack.views',
    url(r'^$', SearchWithRequest(), name='haystack_search'), 
)

custom view:

from haystack.views import SearchView
import operator
from haystack.query import SearchQuerySet, SQ

class SearchWithRequest(SearchView):

    __name__ = 'SearchWithRequest'

    def build_form(self, form_kwargs=None):
        if form_kwargs is None:
            form_kwargs = {}

        if self.searchqueryset is None:
            sqs = SearchQuerySet().filter(reduce(operator.__or__, [SQ(text=word.strip()) for word in self.request.GET.get("q").split(' ')]))
            form_kwargs['searchqueryset'] = sqs

        return super(SearchWithRequest, self).build_form(form_kwargs)

for sqs I've tried everything imaginable, using filter and autocomplete as seen in the docs and every relevant forum post I could find; using __startswith and __contains in combination with my content_auto or text field didn't help at all (the latter would not match anything at all; while the former only matched 1 character or the complete string)

the variant pasted above at least has the benefit of returning results for strings with spaces (each word still has to fully match the corresponding database entry, ergo the need for this post)

any help will be IMMENSELY appreciated

EarlGrey
  • 2,514
  • 4
  • 34
  • 63

1 Answers1

0

late to the party, but suggesting to change your main document field (text) to an EdgeNgramField or NgramField, otherwise the searched index is not capable of matching word fragments, only complete word matching is possible with the CharField.

also, playing in the django shell is sometimes usefull, when debugging haystack:

./manage.py shell
from haystack.query import SearchQuerySet
s = SearchQuerySet()
s.auto_query('sear')
s.auto_query('sear').count()
...

benzkji
  • 1,718
  • 20
  • 41