django-haystack - Django - 两个不同应用程序中的 Haystack

标签 django-haystack

我在一个应用程序中使用 Haystack,它非常完美。它正在索引我需要的一切。但是,现在我创建了另一个应用程序,具有不同的模型和内容,我想用 Haystack 索引它。我的想法是在我的网站上创建两个不同的“搜索”链接,每个应用程序一个。

但是,当我将第二个配置添加到 haystack 索引它时,我遇到了一些问题......

我使用以下内容创建了一个新的 search_index.py(在我的新应用程序中):

import datetime
from haystack.indexes import *
from haystack import site
from oportunity.models import Oportunity


class OportunityIndex(SearchIndex):
    title = CharField(document=True, use_template=True)
    body = CharField()
    date= DateTimeField()

    def index_queryset(self):
        return Oportunity.objects.filter(date=datetime.datetime.now())


site.register(Oportunity, OportunityIndex)

但是,当我运行 python manage.py rebuild_index 我收到以下错误:

line 94, in all_searchfields raise SearchFieldError("All SearchIndex fields with 'document=True' must use the same fieldname.") haystack.exceptions.SearchFieldError: All SearchIndex fields with 'document=True' must use the same fieldname.

最佳答案

这是 Haystack 的一个已知限制,已在 few different places 中讨论过。底层文档存储需要在所有搜索模型中以一致的方式命名文档字段。

documented in the haystack docs推荐的文档字段名称是什么。最重要的是,您不能在一个索引上定义 title = CharField(document=True) 而在另一个索引上定义 content = CharField(document=True),它们必须是同名。

最佳实践:将索引字段命名为text。这是 recommended by the haystack docs并将为您提供与第 3 方应用程序的最大兼容性。

关于django-haystack - Django - 两个不同应用程序中的 Haystack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8965775/

相关文章:

django - taggit 和 haystack+whoosh

Django Haystack子串搜索

python - Haystack 使用的 ElasticSearch 索引因未知原因损坏

python - 如何通过相关模型过滤 Haystack SearchQuerySets

python - Django-haystack 返回后端为 "simple"的结果,而不是 "whoosh"

使用http身份验证的Django Haystack连接错误

python - 安装 django-haystack

elasticsearch - Elasticsearch Analyzer从索引设置中忽略,仅在直接在查询中指定时才起作用

python - 使用 haystack 索引和搜索相关对象

python - Django 干草堆/Solr : Show Solr field that is added in the prepare method of SearchIndex