2

我正在尝试使用 whoosh 作为引擎来设置 django-haystack。我的问题发生在构建我的第一个索引时,如此所述。

鉴于我的员工/models.py:

class Person(models.Model):
    first_name = models.CharField(_('first name'), max_length=30, blank=True)
    last_name = models.CharField(_('last name'), max_length=30, blank=True)

    def __unicode__(self):
        return '%s %s' % ( self.first_name, self.last_name)

我已经写了我的人员/indexes.py:

from haystack import indexes
from staff.models import Person


class PersonIndex(indexes.SearchIndex, indexes.Indexable):
    first_name = indexes.CharField(model_attr='first_name')
    last_name = indexes.CharField(document=True, model_attr='last_name')

    def get_model(self):
        return Person

然后我在 mycms/settings.py 中添加了以下配置,以使用 whoosh 作为引擎:

import os
HAYSTACK_CONNECTIONS = {
    'default': {
        'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine',
        'PATH': os.path.join(os.path.dirname(__file__), 'whoosh_index'),
    },
}

后者是从教程中逐字提取的。然后我为我的索引添加了一个简单的文本模板,mycms/templates/search/indexes/staff/person_text.txt

{{ object.first_name }}
{{ object.last_name }}

...更新了我的 urlconf:

(r'^search/', include('haystack.urls')),

...并创建了我的初始搜索模板,该模板是从教程中复制的:

{% extends 'base.html' %}

{% block content %}
    <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.title }}</a>
                </p>
            {% empty %}
                <p>No results found.</p>
            {% endfor %}

            {% if page.has_previous or page.has_next %}
                <div>
                    {% if page.has_previous %}<a href="?q={{ query }}&amp;page={{ page.previous_page_number }}">{% endif %}&laquo; Previous{% if page.has_previous %}</a>{% endif %}
                    |
                    {% if page.has_next %}<a href="?q={{ query }}&amp;page={{ page.next_page_number }}">{% endif %}Next &raquo;{% if page.has_next %}</a>{% endif %}
                </div>
            {% endif %}
        {% else %}
            {# Show some example queries to run, maybe query syntax, something else? #}
        {% endif %}
    </form>
{% endblock %}

最后,按照教程中的步骤,我尝试构建我的第一个索引。然后是当我收到以下错误时:

$ python manage.py rebuild_index
/home/roberto/.virtualenvs/ve_master/local/lib/python2.7/site-packages/mptt/models.py:305: DeprecationWarning: Implicit manager CMSPlugin.tree will be removed in django-mptt 0.6.  Explicitly define a TreeManager() on your model to remove this warning.
  DeprecationWarning


WARNING: This will irreparably remove EVERYTHING from your search index in connection 'default'.
Your choices after this are to restore from backups or rebuild via the `rebuild_index` command.
Are you sure you wish to continue? [y/N] y
Removing all documents from your index because you said so.
SearchBackendError: No fields were found in any search_indexes. Please correct this before attempting to search.

我一直在寻找解决方案,但结果似乎已经过时了。请问有什么帮助吗?

更新:

Django          - 1.5.5        - active
Whoosh          - 2.5.4        - active
django-haystack - 2.1.0        - active
4

1 回答 1

5

haystack 模块会查找一个名为的文件search_indexes,因此它不会在indexes不更改默认行为的情况下找到您的模块。

于 2013-10-31T13:17:56.263 回答