简体   繁体   中英

How do I add related data to a haystack model index?

I have added haystack search to my fledgling django app and managed to create an index for a model, using the template feature. For some reason I am having trouble adding related data to this template index. I am trying the following:

{{object.name}}
{% for tag in object.tags.all %}
{{tag.name}}
{% endfor %}

The indexes are added correctly and I get search results on the object.name property, but not on the related tags. I have verified that the relationships are correct by using the same template structure in a normal page template and verifying that the tag.name values are output to the screen.

How do I go about debugging the index creation? I am using the simple search backend for the moment so I believe the index exists in memory.

Here is my search_indexes.py

from data.models import VendingMachine
from haystack.indexes import *
from haystack import site


class VendingMachineIndex(SearchIndex):
    text = CharField(document=True, use_template=True)

site.register(VendingMachine, VendingMachineIndex)

And the file in question is called vendingmachine_text.txt and lives at templates/search/indexes/data/ where data is the app name.

I think the problem is in "simple" search backend. It's new and is only good for faking search functionality. Try with whoosh, sorl or xapian.

I've looked at haystack/backends/simple.py . This backend is ORM-based and it has no in-memory search index. Only search by model fields will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM