2

エンジンとして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

次に、whoosh をエンジンとして使用するために、次の構成を mycms/settings.py に追加しました。

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

最後に、チュートリアルの手順に従って、最初の 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.

私は解決策を探していましたが、結果はかなり古くなっているようです。何か助けてください。

アップデート:

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 に答える