简体   繁体   English

Django Haystack和Whoosh搜索正常,但SearchQuerySet返回0个结果

[英]Django Haystack & Whoosh Search Working, But SearchQuerySet Return 0 Results

Edit: More info at bottom of post... 编辑:更多信息在帖子的底部...

Original Question: 原始问题:

I seem to be having the same problem as in this (unresolved) question: django-haystack + Whoosh SearchQuerySet().all() always None 我似乎遇到了与这个(未解决的)问题相同的问题: django-haystack + Whoosh SearchQuerySet()。all()始终无

I've set up Haystack with Whoosh on my Django project and all was working fine at first (SearchQuerySet used to return results), but after an aborted attempt to create a new custom search form (rolled back from git) it appears that indexing and the original search page still all work fine, but now SearchQuerySet() always returns 0 results! 我已经在我的Django项目中使用Whoosh设置了Haystack,起初一切都很好(SearchQuerySet用来返回结果),但是在尝试创建新的自定义搜索表单(从git回滚)中止尝试之后,索引和原始搜索页面仍然可以正常运行,但是现在SearchQuerySet()始终返回0个结果!

Running: 运行:

manage.py rebuild_index --verbosity=2

Correctly shows: 正确显示:

Indexing 14 assets
    indexed 1 - 14 of 14 (worker PID: 1234).

These indexed assets can then all be correctly searched on from the original search form. 然后,可以从原始搜索表单中正确搜索所有这些索引资产。

However, opening a Django shell and running: 但是,打开Django shell并运行:

from haystack.query import SearchQuerySet
SearchQuerySet().all().count()

Always returns 0 ! 总是返回0

Relevant pip freeze : 相关pip freeze

  • Python 3.5.2 Python 3.5.2
  • Django 1.9.3 的Django 1.9.3
  • django-haystack 2.5.0 django-干草堆2.5.0
  • Whoosh 2.7.4 飞快移动2.7.4

/myapp/search_indexes.py: /myapp/search_indexes.py:

from haystack import indexes
from .models import Asset

class AssetIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.NgramField(document=True, use_template=True)
    asset_description = indexes.CharField(model_attr='asset_description')
    manufacturer = indexes.CharField(model_attr='asset_manufacturer')

    def get_model(self):
        return Asset

    def no_query_found(self):
        return self.searchqueryset.exclude(content='foo')

    def index_queryset(self, using=None):
        return self.get_model().objects.all()

/myapp/templates/search/indexes/myapp/asset_text.txt: /myapp/templates/search/indexes/myapp/asset_text.txt:

{{ object.asset_description }}
{{ object.asset_details }}
{{ object.asset_manufacturer }}
{{ object.asset_model }}
... etc.

/myapp/urls.py: /myapp/urls.py:

urlpatterns = [
    ....
    url(r'^search/', include('haystack.urls')),
    ....
]

EDIT: 编辑:

So digging in the Haystack source code I've found out where the 0 is coming from, but not why! 因此,深入研究Haystack源代码,我发现了0的来源,而不是为什么!

/myvenv/Lib/site-packages/hackstack/query.py /myvenv/Lib/site-packages/hackstack/query.py

class SearchQuerySet(object):
    ...

    def __len__(self):
        if self._result_count is None:
            self._result_count = self.query.get_count()

        # Some backends give weird, false-y values here. Convert to zero.
        if not self._result_count:
            self._result_count = 0

    # This needs to return the actual number of hits, not what's in the cache.
    return self._result_count - self._ignored_result_count

    ....

Changing the 0 to any int makes SearchQuerySet always return that int, but I still don't know why if not self._result_count would be true... 0更改为任何int会使SearchQuerySet始终返回该int,但我仍然不知道为什么if not self._result_count将为真...

It looks like this a bug in haystack that has already been reported but has yet to be addressed: 看起来这是干草堆中的一个错误,该错误已被报告,但尚未得到解决:

https://github.com/django-haystack/django-haystack/issues/1021 https://github.com/django-haystack/django-haystack/issues/1021

Unfortunately if the "text" index field is Ngram or EdgeNgram SearchQuerySet().count() and SearchQuerySet().all().count() will return 0 unless you specify a filter, eg SearchQuerySet().all().exclude(content='thisshouldnotmatchanythingintheindex').count() returns the total number of indexed objects. 不幸的是,如果“文本”索引字段是Ngram或EdgeNgram, SearchQuerySet().count()除非您指定过滤器,例如SearchQuerySet().all().exclude(content='thisshouldnotmatchanythingintheindex').count() ,否则SearchQuerySet().count()SearchQuerySet().all().count()将返回0 SearchQuerySet().all().exclude(content='thisshouldnotmatchanythingintheindex').count()与索引中的任何SearchQuerySet().all().exclude(content='thisshouldnotmatchanythingintheindex').count()返回已索引对象的总数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM