Django Haystack与Elasticsearch后端

时间:2015-07-03 14:42:06

标签: python django elasticsearch django-haystack

我试图在Django 1.8.2上使用Haystack,将Elasticsearch 1.6作为后端但不能让干草堆教程为我工作。

在运行rebuild_index时,我收到以下错误和堆栈跟踪

./manage.py rebuild_index

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.
All documents removed.
Indexing 388 regestes
GET /haystack/_mapping [status:404 request:0.002s]
ERROR:root:Error updating regeste using default 
Traceback (most recent call last):
  File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/management/commands/update_index.py", line 188, in handle_label
    self.update_backend(label, using)
  File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/management/commands/update_index.py", line 233, in update_backend
    do_update(backend, index, qs, start, end, total, verbosity=self.verbosity, commit=self.commit)
  File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/management/commands/update_index.py", line 96, in do_update
    backend.update(index, current_qs, commit=commit)
  File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/backends/elasticsearch_backend.py", line 166, in update
    prepped_data = index.full_prepare(obj)
  File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/indexes.py", line 212, in full_prepare
    self.prepared_data = self.prepare(obj)
  File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/indexes.py", line 203, in prepare
    self.prepared_data[field.index_fieldname] = field.prepare(obj)
  File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/fields.py", line 159, in prepare
    return self.convert(super(CharField, self).prepare(obj))
  File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/fields.py", line 79, in prepare
    return self.prepare_template(obj)
  File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/fields.py", line 136, in prepare_template
    t = loader.select_template(template_names)
  File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/django/template/loader.py", line 76, in select_template
    raise TemplateDoesNotExist(', '.join(template_name_list))
django.template.base.TemplateDoesNotExist: search/indexes/regeste/regeste_text.txt
Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/django/core/management/base.py", line 390, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/django/core/management/base.py", line 441, in execute
    output = self.handle(*args, **options)
  File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/management/commands/rebuild_index.py", line 26, in handle
    call_command('update_index', **options)
  File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/django/core/management/__init__.py", line 120, in call_command
    return command.execute(*args, **defaults)
  File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/django/core/management/base.py", line 441, in execute
    output = self.handle(*args, **options)
  File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/management/commands/update_index.py", line 183, in handle
    return super(Command, self).handle(*items, **options)
  File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/django/core/management/base.py", line 619, in handle
    label_output = self.handle_label(label, **options)
  File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/management/commands/update_index.py", line 188, in handle_label
    self.update_backend(label, using)
  File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/management/commands/update_index.py", line 233, in update_backend
    do_update(backend, index, qs, start, end, total, verbosity=self.verbosity, commit=self.commit)
  File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/management/commands/update_index.py", line 96, in do_update
    backend.update(index, current_qs, commit=commit)
  File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/backends/elasticsearch_backend.py", line 166, in update
    prepped_data = index.full_prepare(obj)
  File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/indexes.py", line 212, in full_prepare
    self.prepared_data = self.prepare(obj)
  File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/indexes.py", line 203, in prepare
    self.prepared_data[field.index_fieldname] = field.prepare(obj)
  File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/fields.py", line 159, in prepare
    return self.convert(super(CharField, self).prepare(obj))
  File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/fields.py", line 79, in prepare
    return self.prepare_template(obj)
  File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/haystack/fields.py", line 136, in prepare_template
    t = loader.select_template(template_names)
  File "/home/user/.virtualenvs/imperii-viz/lib/python3.4/site-packages/django/template/loader.py", line 76, in select_template
    raise TemplateDoesNotExist(', '.join(template_name_list))
django.template.base.TemplateDoesNotExist: search/indexes/regeste/regeste_text.txt

项目根目录如下:

    .
    ├── config
    ├── lib
    ├── manage.py
    ...
    ├── regeste
    ├── requirements.txt
    ├── search
    ├── templates <-- search/indexes/regeste/regeste_text.txt lies here
    └── untitled

regeste_txt.txt

{{ object.title }}
{{ object.abstract }}

settings.py看起来像这样(删除了一些私有部分)

import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))



INSTALLED_APPS = (
        'elasticsearch',
        'haystack',
        'regeste',
        )

MIDDLEWARE_CLASSES = (
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.middleware.common.CommonMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware',
        'django.middleware.security.SecurityMiddleware',
        )

ROOT_URLCONF = 'imperii_viz.urls'

TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [],
            'APP_DIRS': True,
            'OPTIONS': {
                'context_processors': [
                    'django.template.context_processors.debug',
                    'django.template.context_processors.request',
                    'django.contrib.auth.context_processors.auth',
                    'django.contrib.messages.context_processors.messages',
                    ],
                },
            },
        ]

WSGI_APPLICATION = 'imperii_viz.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases

DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
            'NAME': 'imperii-viz',                      # Or path to database file if using sqlite3.
            # The following settings are not used with sqlite3:
            'USER': 'postgres',
            'PASSWORD': 'postgres',
            'HOST': 'localhost',                      # Empty for localhost through domain sockets or           '127.0.0.1' for localhost through TCP.
            'PORT': '',                      # Set to empty string for default.
            },
        'search': {
            'BACKEND': 'django_redis.cache.RedisCache',
            'LOCATION': 'redis://127.0.0.1:6379/1',
            'OPTIONS': {
                'CLIENT_CLASS': 'django_redis.client.DefaultClient',
            }
        }
        }


HAYSTACK_CONNECTIONS = {
    'default': {
        'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
        'URL': 'http://127.0.0.1:9200/',
        'INDEX_NAME': 'haystack',
    },
}

# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


#celery settings

BROKER_URL = 'redis://localhost:6379/0'

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/

STATIC_URL = '/static/'
SHELL_PLUS = "ipython"
TEMPLATE_DIRS = (
        os.path.join(BASE_DIR,  'templates'),
        )

我很欣赏任何有关问题所在的提示。

谢谢, 丹尼尔

2 个答案:

答案 0 :(得分:2)

DIRS词典中的

TEMPLATES优先于TEMPLATE_DIRS, which is deprecated as of Django 1.8

DIRS设为[os.path.join(BASE_DIR, 'templates')]

更好的是,您应该将此模板放在应用程序的模板目录中(使用定义模型的应用程序),因为它可能存在于应用的位置。

答案 1 :(得分:0)

只需在应用内创建模板目录,然后将搜索目录移至此模板目录。现在它会起作用。