【问题标题】:Django Haystack: responsibilities of the template and model indexesDjango Haystack:模板和模型索引的职责
【发布时间】: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


【解决方案1】:

ProductIndex 类是这里的主要内容。 Haystack 将使用此配置根据您选择索引的字段以及以何种方式索引您的Product 模型。你可以阅读更多关于它的信息here

您创建的模板将被此字段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)

【讨论】:

    猜你喜欢
    • 2019-04-09
    • 1970-01-01
    • 2013-12-09
    • 2013-04-03
    • 1970-01-01
    • 2015-09-21
    • 2013-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多