SearchBackendError:在任何search_index中都找不到任何字段

时间:2013-10-30 15:26:24

标签: python django django-haystack whoosh

我正在尝试使用飞快移动作为引擎来设置django-haystack。我按照here所述构建我的第一个索引时遇到了问题。

鉴于我的staff / 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)

我写了我的staff / 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'),
    },
}

后者是从the tutorial逐字逐句采用的。然后我为我的索引添加了一个简单的文本模板,mycms / templates / search / indexes / staff / person_text.txt

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

...更新了我的urlconf:

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

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

{% 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 %}

最后,按照本教程中的步骤,我尝试了build my first index。然后就是我收到以下错误:

$ 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.

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

更新:

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

1 个答案:

答案 0 :(得分:4)

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