Django-haystack在“simple”后端返回结果,但在“whoosh”中不返回

4 投票
1 回答
1681 浏览
提问于 2025-04-17 11:00

我正在尝试将搜索功能与django-haystack结合起来,
在使用“示例”后端时一切正常,但当我把后端换成whoosh时,总是返回0个结果。

settings.py:

HAYSTACK_DEFAULT_OPERATOR = 'AND'
HAYSTACK_SITECONF = 'search_sites'
HAYSTACK_SEARCH_ENGINE = 'whoosh'
HAYSTACK_SEARCH_RESULTS_PER_PAGE = 20
HAYSTACK_WHOOSH_PATH = os.path.join(PROJECT_ROOT, 'search_index')

search_sites.py

import haystack
haystack.autodiscover()

profiles/search_indexes.py:

from haystack import indexes
from haystack import site

from profiles.models import Profile


class ProfileIndex(indexes.SearchIndex):
    text = indexes.CharField(document=True, use_template=True)

    def index_queryset(self):
        """Used when the entire index for model is updated."""
        return Profile.objects.all()

site.register(Profile, ProfileIndex)

templates/search/indexes/profiles/profile_text.txt:

{{ profile.name }}
{{ profile.description }}

运行 python manage.py rebuild_index 的结果是:

All documents removed.
Indexing 60 profiles.

在命令行中运行以下内容时:

>>> from haystack.query import SearchQuerySet
>>> sqs = SearchQuerySet().all()
>>> sqs.count()
0

当把whoosh换成“简单”后端时,一切都正常,返回了60个结果。

根据 Haystack入门指南Haystack调试指南,一切似乎都设置正确。
我尝试安装whoosh的旧版本,但没有成功。

此时我感觉很愚蠢,任何帮助都将非常感激。

软件包版本:

python==2.7  
Django==1.3.1  
Whoosh==2.3.2  
django-haystack==1.2.6  

更新:

  • 将Whoosh降级到1.8.4没有帮助。
  • 当使用基本搜索模板,如Haystack教程中所描述的那样,对于1个字母的查询返回了所有结果,而其他搜索则返回0个结果。

1 个回答

7

好的,我找到了,结果比我想的还要简单...

templates/search/indexes/profiles/profile_text.txt 应该是:

{{ object.name }}
{{ object.description }}

而不是:

{{ profile.name }}
{{ profile.description }}

让我困惑的是,这个“简单”的后端会直接和数据库对比,似乎完全不管数据模板。

撰写回答