Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django-haystack: rebuild_index fails (haystack.exceptions.SearchFieldError) after adding `content_auto` line needed for autocomplete

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

Example:

Search "gol"

Result "goldfish"

What have I tried?

I did as asked in step 1 of the docs, I added the following line in my Index class:

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

Then did python manage.py rebuild_index.

Rebuilding the index however produced an error haystack.exceptions.SearchFieldError: The model '<Person: Reginald>' does not have a model_attr 'content'. With Reginald being the first entry in my indexed table and Person being the model I indexed.

Now indeed my model doesn't have a field called content but as it is shown in the docs it should not need to have such a field.

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

like image 991
Bentley4 Avatar asked Jan 23 '13 12:01

Bentley4


2 Answers

So this is how I'm solving this right now.

Instead of:

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

Use:

content_auto = indexes.EdgeNgramField(use_template=True)

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

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

This seems to be functioning how I want it to. A little more tedious than if 'content' worked, but it should suffice.

like image 104
Mike Lentini Avatar answered Sep 28 '22 05:09

Mike Lentini


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)
like image 39
user42488 Avatar answered Sep 28 '22 04:09

user42488