我在一个应用程序中使用 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/