问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/5059301/how-do-i-add-related-data-to-a-haystack-model-index