[英]django haystack whoosh not showing any errors also no results
[英]Haystack with Whoosh not returning any results
我已經安裝了Django-Haystack和Whoosh,並按照干草堆文檔進行了全部設置,但是無論我進行什么搜索,我總會得到“未找到結果”。 在搜索頁面上,盡管索引顯然可以。
運行“ manage.py rebuild_index”時,它正確指出:
Indexing 12 assets
indexed 1 - 12 of 12 (worker PID: 1234).
當在Django shell中運行時:
from whoosh.index import open_dir
ix = open_dir('mysite/whoosh_index')
from pprint import pprint
pprint(list(ix.searcher().documents()))
它可以正確返回12個索引資產的所有詳細信息,因此索引看起來不錯,但是無論我搜索什么,我都無法獲得任何結果,只有“未找到結果”!
我在StackOverflow(以及在Google上彈出的所有其他地方)上的所有其他類似問題中都遵循了建議,但是沒有用。
有沒有人有什么建議?
使用的文件(為簡潔起見進行了編輯):
settings.py
INSTALLED_APPS = [
....
'haystack',
'assetregister',
....
]
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine',
'PATH': os.path.join(os.path.dirname(__file__), 'whoosh_index'),
},
}
models.py
class Asset(models.Model):
asset_id = models.AutoField(primary_key=True)
asset_description = models.CharField(max_length=200)
asset_details = models.TextField(blank=True)
search_indexes.py
from haystack import indexes
from .models import Asset
class AssetIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
asset_description = indexes.CharField(model_attr='asset_description')
def get_model(self):
return Asset
def no_query_found(self):
# The .exclude is a hack from another stackoverflow question that prevents it returning an empty queryset
return self.searchqueryset.exclude(content='foo')
def index_queryset(self, using=None):
return self.get_model().objects
/templates/search/indexes/assetregister/asset_text.txt
{{ object.asset_description }}
{{ object.asset_details }}
urls.py
urlpatterns = [
url(r'^search/', include('haystack.urls')),
]
search.html
<h2>Search</h2>
<form method="get" action=".">
<table>
{{ form.as_table }}
<tr>
<td> </td>
<td>
<input type="submit" value="Search">
</td>
</tr>
</table>
{% if query %}
<h3>Results</h3>
{% for result in page.object_list %}
<p>
<a href="{{ result.object.get_absolute_url }}">{{ result.object.asset_description }}</a>
</p>
{% empty %}
<p>No results found.</p>
{% endfor %}
{% else %}
{# Show some example queries to run, maybe query syntax, something else? #}
{% endif %}
</form>
為了以防萬一,我的“凍結點數”:
Django==1.9.3
django-cleanup==0.4.2
django-haystack==2.5.0
django-pyodbc-azure==1.9.3.0
Pillow==3.2.0
pyodbc==3.0.10
Whoosh==2.7.4
為了將來遇到同樣問題的人們的利益,我找到了一種非常規的解決方案...
因此,我根據干草堆文檔仔細檢查了所有內容,一切看上去都很好。 我發現了一個名為“ django-haystackbrowser”的pip軟件包,該軟件包一旦正確安裝,便可以通過django管理界面查看索引。
出於某種原因,一旦我在管理界面中查看了索引(並確認所有內容都已經存在),然后使用重新啟動了服務器
python manage.py runserver
我終於開始找回搜索結果!
不知道是什么導致了問題,當然也不知道如何僅使用該包查看索引即可解決該問題,但是現在看來它正在返回應有的結果!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.