Skip to main content
 首页 » 编程设计

python之带有 Elasticsearch 后端的 Django Haystack

2024年10月24日35linjiqin

我正在尝试在 Django 1.8.2 上使用 Haystack,以 Elasticsearch 1.6 作为后端,但无法让 haystack 教程为我工作。

在运行 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'), 
        ) 

对于问题所在的任何提示,我将不胜感激。

谢谢, 丹尼尔

请您参考如下方法:

TEMPLATES 字典中的

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

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


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