【问题标题】:Haystack with Whoosh not returning any results干草堆与 Whoosh 不返回任何结果
【发布时间】: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>&nbsp;</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


    【解决方案1】:

    为了以后遇到同样问题的人的利益,我找到了一个非常规的解决方案...

    所以我根据 haystack 文档仔细检查了所有内容,看起来一切正常。我发现了一个名为“django-haystackbrowser”的 pip 包,一旦正确安装,您就可以通过 django 管理界面查看索引。

    由于某种原因,一旦我在管理界面中查看了索引(并确认所有内容都已经存在),然后使用

    重新启动服务器
    python manage.py runserver
    

    我终于开始返回搜索结果了!

    不知道是什么导致了这个问题,当然也不知道使用那个包查看索引是如何修复它的,但它现在似乎返回了它应该返回的结果!

    【讨论】:

    • 我试过这个。起初,haystackbrowser 与 Django1.10 存在兼容性问题。我分叉了应用程序,修复了兼容性问题,然后运行它。它显示了管理面板中已编入索引的所有项目。我无法重新启动服务器,所以也许这就是原因,但安装此应用后我仍然没有得到任何结果。
    猜你喜欢
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    • 2017-12-09
    • 2020-10-03
    • 2017-03-19
    • 2017-05-20
    相关资源
    最近更新 更多