【发布时间】:2017-02-21 07:12:39
【问题描述】:
我已经安装了 Django-Haystack 和 Whoosh,并按照 haystack 文档进行了设置,但无论我搜索什么,我总是得到“未找到结果”。在搜索页面上,尽管索引显然没问题。
当运行“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')),
]
搜索.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
【问题讨论】:
标签: python django python-3.x django-haystack whoosh