Search code examples
pythondjangoelasticsearchdjango-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.

On running rebuild_index I get the following error and stack trace

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

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

settings.py looks like this (removed a few private parts)

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


Solution

  • DIRS in your TEMPLATES dictionary takes precedence over TEMPLATE_DIRS, which is deprecated as of Django 1.8.

    Set DIRS to [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.