【发布时间】:2014-05-06 20:56:16
【问题描述】:
我已经浏览了文档,甚至创建了一些搜索后端,但我仍然对这些东西在 haystack 中的作用感到困惑。搜索后端是否搜索您放入的字段继承indexes.SearchIndex、indexes.Indexable的类,还是后端搜索模板内的文本?有人能解释一下吗?
在 django haystack 中,您将创建一个类,该类定义应该查询哪些字段(这就是我的理解),如下所示:
class ProductIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
name = indexes.CharField(model_attr='title', boost=1.75)
description = indexes.CharField(model_attr='description')
short_description = indexes.CharField(model_attr='short_description')
def get_model(self):
return Product
def index_queryset(self, using=None):
"""Used when the entire index for model is updated."""
return self.get_model().objects.filter(active=True,
published_at__lte=datetime.now())
您还将创建一个模板 txt,它会做一些事情 - 我不确定是什么。我知道搜索后端会在搜索算法期间检查这个模板。
{{ object.name }}
{{ object.description }}
{{ object.short_description }}
{% for related in object.related %}
{{ related.name }}
{{ related.description }}
{% endfor %}
{% for category in object.categories.all %}
{% if category.active %}
{{ category.name }}
{% endif %}
{% endfor %}
您可以看到模板有一些我的索引类没有的字段,但是,这些将由搜索后端搜索。那么为什么在索引中还要有字段呢?索引类的卷和索引模板是什么?有人可以向我解释一下吗。
【问题讨论】:
-
我不确定我是否完全理解你。您能否添加一些此类后端的示例(最好在代码中),并详细解释究竟是什么让您感到困惑?您是在问 Haystack 如何在后台工作,您是否对如何使用某些功能感到困惑?
-
我尽我所能 yuvi。我真的很困惑索引类和索引模板的确切目的是什么,以及后端正在搜索什么。
-
这样就清楚多了。我不确定我能帮助你,但现在问题好多了。希望干草堆忍者很快就会出现!
标签: django django-templates django-haystack