Django-haystack: rebuild_index fails with an error (haystack.exceptions.SearchFieldError) after adding the string `content_auto` necessary for autocomplete

I am trying to implement compliant results to search only for a part of a word (which is called autocomplete according to the Haystack docs, if I'm not mistaken).

Example:

Search "gol"

Result of "goldfish"

What have i tried?

I did as I set the documents in step 1 , I added the following line to my Index class:

 content_auto = indexes.EdgeNgramField(model_attr='content') 

Then did python manage.py rebuild_index .

Index recovery, however, caused the haystack.exceptions.SearchFieldError: The model '<Person: Reginald>' does not have a model_attr 'content'. With Reginald was the first entry in my indexed table and Person , which was indexed by the model.

Now my model does not have a field called content , but as shown in the docs, it should not have such a field.

I am using Whoosh 2.4.1 , Django-haystack 1.2.7 and Django 1.4 .

+6
source share
3 answers

So this is how I solve it now.

Instead:

content_auto = indexes.EdgeNgramField(model_attr='content')

Using:

content_auto = indexes.EdgeNgramField(use_template=True)

Then you can create a template for them. For example, I have ItemIndex in my catalog application, where I want to search for name and description . So, I created a file in templates/search/indexes/catalog/ called item_content_auto.txt , which has the following in it:

 {{ object.name }} {{ object.description }} 

It seems to work the way I want it. A bit more tedious than if 'content' worked, but that was enough.

+3
source

Here is an updated example for reference (see here ):

 #search_indexes.py class Book(indexes.SearchIndex, indexes.Indexable): text = indexes.EdgeNgramField(document=True, use_template=True) def get_model(self): return Book #template {{object.name}} #query SearchQuerySet().autocomplete(text=my_query) 
+1
source

All Articles