简体   繁体   English

带有 Elasticsearch 后端的 Django Haystack

[英]Django Haystack with Elasticsearch backend

I'm trying to use Haystack on Django 1.8.2 with Elasticsearch 1.6 as the Backend but can't get the haystack tutorial to work for me.我正在尝试在 Django 1.8.2 上使用 Haystack,并将 Elasticsearch 1.6 作为后端,但无法让 haystack 教程对我有用。

On running rebuild_index I get the following error and stack trace在运行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

The project root looks like this:项目根目录如下所示:

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

regeste_txt.txt regeste_txt.txt

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

settings.py looks like this (removed a few private parts) 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'),
        )

I'd appreciate any hints on where the problem lies.我很感激有关问题所在的任何提示。

Thanks, Daniel谢谢,丹尼尔

DIRS in your TEMPLATES dictionary takes precedence over TEMPLATE_DIRS , which is deprecated as of Django 1.8 . DIRSTEMPLATES字典优先于TEMPLATE_DIRS ,这是不赞成的Django 1.8

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


Better yet, you should put this template in an app's template directory (use the app where the model is defined), because that's probably where it should live.更好的是,您应该将此模板放在应用程序的模板目录中(使用定义模型的应用程序),因为它可能应该放在那里。

just create the templates directory inside app and then move search directory to this templates directory.只需在应用程序中创建模板目录,然后将搜索目录移动到此模板目录。 Now it will work.现在它将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM