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

Tho*_*mas 4 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)
Run Code Online (Sandbox Code Playgroud)

但是,当我运行 python manage.py rebuild_index 时,出现以下错误:

第 94 行,在 all_searchfields 中引发 SearchFieldError("所有具有 'document=True' 的 SearchIndex 字段必须使用相同的字段名。") haystack.exceptions.SearchFieldError:所有具有 'document=True' 的 SearchIndex 字段必须使用相同的字段名。

Tho*_*mas 5

这是 Haystack 的一个已知限制,已经在几个不同的地方进行了讨论,其中底层文档存储需要文档字段在所有搜索模型中保持一致的命名。

haystack 文档中记录了推荐的文档字段名称。最重要的是,您不能title = CharField(document=True)在一个索引和content = CharField(document=True)另一个索引上定义,它们必须命名相同。

最佳实践:将索引字段命名为text。这是haystack 文档推荐的,将为您提供与第 3 方应用程序的最大兼容性。