Вопрос

I'm trying to add a module wide search for my App and found haystack-search. I took whoosh to store the search information.

I've configured my search as told in the docs and already can find results.

The problem I have is a field which stores and email-address ("test@testdomain.com"). When I search for "testdomain" I will not get any results but when I search for "tesdomain.com" I'll get some results. Now I want to get the results (which I get when "testdomain.com" is entered) when I enter "testdomain". Any ideas how this can be done?

Did someone ever posted to the mailinglist of haystack? For me it is not possible to post even if I'm a member of the google-group.

regards Martin

Это было полезно?

Решение

Now I have the answer for the problem. The standard search shipped with haystack does not support searching by substrings.

I createt my own app (mysearch) for searching which will use haystacksearch

What you have to do is to create your own SearchForm (mysearch/forms.py)

from django import forms
from haystack.forms import ModelSearchForm
from haystack.query import SearchQuerySet

class AutocompleteSearchForm(ModelSearchForm):

  def search(self):
    if not self.is_valid():
      return self.no_query_found()

    if not self.cleaned_data.get('q'):
      return self.no_query_found()

    sqs = self.searchqueryset.filter(autocompletetext=self.cleaned_data.get('q'))

    sqs = sqs.models(*self.get_models())

    if self.load_all:
      sqs = sqs.load_all()

    return sqs

Then you have to create your own url-route using your own SearchForm (mysearch/urls.py)

from django.conf.urls.defaults import *
from haystack.forms import ModelSearchForm
from haystack.query import SearchQuerySet
from haystack.views import SearchView
from forms import AutocompleteSearchForm

sqs = SearchQuerySet()

# Without threading...
urlpatterns = patterns('haystack.views',
  url(r'^$', SearchView(searchqueryset=sqs, form_class=AutocompleteSearchForm), name='haystack_search'),
)

in your projects urls.py use

url(r"^search/", include("mysearch.urls", namespace="Search")),

instead of

url(r'^search/', include('haystack.urls', namespace="Search")),
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top