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

3

7

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.

Parabolic answered 23/1, 2013 at 12:0 Comment(2)
I know this is an old question, but did you ever figure this out?Deprivation
No. I didn't work on the problem for long though.Parabolic
D
3

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.

Deprivation answered 5/3, 2014 at 19:1 Comment(0)
P
1

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)
Pare answered 13/3, 2017 at 9:51 Comment(0)
H
0

model_attr is used to reference an existing model attribute, callable or relation:

http://django-haystack.readthedocs.org/en/v1.2.7/searchfield_api.html#SearchField.model_attr http://django-haystack.readthedocs.org/en/v2.1.0/searchfield_api.html#model-attr

Holophrastic answered 5/3, 2014 at 14:1 Comment(1)
Yes, but see this: django-haystack.readthedocs.org/en/v2.1.0/autocomplete.html They say you just need to use 'content', but that doesn't work. They must have changed it and not updated the docs.Deprivation

© 2022 - 2024 — McMap. All rights reserved.