There is a certain model. It stores movie titles. Here is the index class:

from haystack import indexes from kino.films.models import Films class FilmsIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.CharField(document=True, use_template=True) rusname = indexes.CharField(model_attr='rusname') origname = indexes.CharField(model_attr='origname') year = indexes.IntegerField(model_attr='year') def get_model(self): return Films 

The index is created and everything almost works, but not quite as it should. Request view

 SearchQuerySet().filter(content='avatar') 

returns what you need

 SearchQuerySet().filter(content='avat') 

returns nothing. What am I doing wrong?

  • updated answer - ChehoV

1 answer 1

Nothing. Or rather, you're doing everything right. Itself on it was pricked, when did through autoscope haystack in search. Whoosh is just a full text search with minimal morphology. To search for occurrences you need to use classic like.

UPDATE I repent, I deceived you. I decided to google all the same, so as not to lose face. And that's what was found. Haystack has a special autocomplete method just for that.

 import datetime from haystack import indexes from myapp.models import Note class NoteIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.CharField(document=True, use_template=True) author = indexes.CharField(model_attr='user') pub_date = indexes.DateTimeField(model_attr='pub_date') # We add this for autocomplete. content_auto = indexes.EdgeNgramField(model_attr='content') def get_model(self): return Note def index_queryset(self): """Used when the entire index for model is updated.""" return Note.objects.filter(pub_date__lte=datetime.datetime.now()) 

And actually the search itself

 from haystack.query import SearchQuerySet SearchQuerySet().autocomplete(content_auto='old') # Result match things like 'goldfish', 'cuckold' & 'older'. 

A source

  • Thank. As I understand the chip in using EdgeNgramField (). And if the search is on several columns, then you probably have to paint a large footcloth: SearchQuerySet (). Filter (content_auto1 = 'old'). Filter (content_auto2 = 'old') ... - ReklatsMasters
  • Yes, in your case, you need to use filter() , and not autocomplete() . In theory, it should work like a native filter. those. construction should be simpler SearchQuerySet (). filter (content_auto1 = 'old', content_auto2 = 'old') - ChehoV