I've gone over the docs, and I've even created a few search back ends, but I"m still really confused on what these things do in haystack. Is the search back end searching the fields you put in your class that inherits indexes.SearchIndex, indexes.Indexable, or is the back end searching the text inside your template? Can someone explain this to me?
我已经浏览了文档,我甚至创建了一些搜索后端,但我仍然对这些事情在haystack中做了什么感到困惑。搜索结束时搜索你在类中继承的字段indices.SearchIndex,indexes.Indexable,或者是后端搜索模板中的文本?有人可以向我解释一下吗?
In django haystack you will create a class that defines what fields should be queried (well that's how I understand it) like so:
在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())
You'll also create a template txt that will do something - I"m not sure what. I know that the search backend will go over this template during the searching algorithm.
你还将创建一个可以做某事的模板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 %}
As you can see the template has some fields that my index class doesn't have, however, these will be searched by the search backend. So why even have fields in the index? What are the rolls of the index class, and the index template? Can someone please explain this to me.
正如您所看到的,模板有一些我的索引类没有的字段,但搜索后端会搜索这些字段。那么为什么甚至在索引中都有字段呢?索引类的卷和索引模板是什么?有人可以向我解释一下。
1 个解决方案
#1
7
The ProductIndex
class is the main thing here. Haystack will use this configuration to index your Product
model according to the fields you have chosen to be indexed and in what way. You can read more about it here.
ProductIndex类是这里的主要内容。 Haystack将使用此配置根据您选择的索引字段以及以何种方式索引您的产品模型。你可以在这里读更多关于它的内容。
The template which you have created will be used by this field text = indexes.CharField(document=True, use_template=True)
. In this template we include every important data from model or related models, why? because this is used to perform search query on all data if you don't want to lookup in just one field.
您创建的模板将由此字段使用text = indexes.CharField(document = True,use_template = True)。在此模板中,我们包含模型或相关模型中的所有重要数据,为什么?因为如果您不想只在一个字段中查找,这用于对所有数据执行搜索查询。
# filtering on single field
qs = SearchQuerySet().models(Product).filter(name=query)
# filtering on multiple fields
qs = SearchQuerySet().models(Product).filter(name=query).filter(description=query)
# filtering on all data where ever there is a match
qs = SearchQuerySet().models(Product).filter(text=query)
#1
7
The ProductIndex
class is the main thing here. Haystack will use this configuration to index your Product
model according to the fields you have chosen to be indexed and in what way. You can read more about it here.
ProductIndex类是这里的主要内容。 Haystack将使用此配置根据您选择的索引字段以及以何种方式索引您的产品模型。你可以在这里读更多关于它的内容。
The template which you have created will be used by this field text = indexes.CharField(document=True, use_template=True)
. In this template we include every important data from model or related models, why? because this is used to perform search query on all data if you don't want to lookup in just one field.
您创建的模板将由此字段使用text = indexes.CharField(document = True,use_template = True)。在此模板中,我们包含模型或相关模型中的所有重要数据,为什么?因为如果您不想只在一个字段中查找,这用于对所有数据执行搜索查询。
# filtering on single field
qs = SearchQuerySet().models(Product).filter(name=query)
# filtering on multiple fields
qs = SearchQuerySet().models(Product).filter(name=query).filter(description=query)
# filtering on all data where ever there is a match
qs = SearchQuerySet().models(Product).filter(text=query)