Django Haystack& Whoosh搜索工作,但SearchQuerySet返回0结果 [英] Django Haystack & Whoosh Search Working, But SearchQuerySet Return 0 Results
问题描述
编辑:在帖子底部的更多信息
原始问题:
我似乎有与这个(未解决的)问题相同的问题:
django-haystack + Whoosh SearchQuerySet()。all()always None
I seem to be having the same problem as in this (unresolved) question: django-haystack + Whoosh SearchQuerySet().all() always None
我已经在我的Django上设置了Haystack项目和所有工作正常(SearchQuerySet用于返回结果),但是在中止尝试创建一个新的自定义搜索表单(从git回滚)后,似乎索引和原始搜索页面仍然可以正常工作,但现在SearchQuerySet()总是返回0个结果!
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!
运行:
manage.py rebuild_index --verbosity=2
正确显示:
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.
但是,打开一个Django shell并运行:
However, opening a Django shell and running:
from haystack.query import SearchQuerySet
SearchQuerySet().all().count()
始终返回 0
!
相关点击冻结
:
- Python 3.5.2
- Django 1.9.3
- django-haystack 2.5.0
- Whoosh 2.7.4
- Python 3.5.2
- Django 1.9.3
- django-haystack 2.5.0
- Whoosh 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')),
....
]
编辑:
所以在Haystack源代码中挖掘出来,我发现0来自哪里,但不是为什么!
So digging in the Haystack source code I've found out where the 0 is coming from, but not why!
/ 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
....
将 0
更改为任何int使SearchQuerySet始终返回该int,但是我仍然不知道为什么 if not self._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...
推荐答案
看起来这是一个已经报告的干草堆中的错误尚未解决:
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
不幸的是,如果文本索引字段是Ngram或EdgeNgram SearchQuerySet()。count()
和 SearchQuerySet()。all()。count()
将返回 0
除非您指定过滤器,例如 SearchQuerySet()。all()。exclude(content ='thisshouldnotmatchanythingintheindex')count()
返回索引对象的总数。
Unfortunately if the "text" index field is Ngram or EdgeNgram SearchQuerySet().count()
and SearchQuerySet().all().count()
will return 0
unless you specify a filter, e.g. SearchQuerySet().all().exclude(content='thisshouldnotmatchanythingintheindex').count()
returns the total number of indexed objects.
这篇关于Django Haystack& Whoosh搜索工作,但SearchQuerySet返回0结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!