django - 使用 django-haystack rebuild_index 命令重建 ElasticSearch 索引

标签 django elasticsearch django-haystack

我正在尝试在我的本地开发环境(运行 Ubuntu 12.04 的 vagrant VM)上设置 ElasticSearch/Haystack,但我无法完成重新索引过程。

ES 正在运行,我已经创建了一个新索引(我正在使用 elasticsearch-head 在浏览器中查看索引状态)。我可以创建一个新索引并查询它,所以我知道 ES 正在工作。

我的问题是 Haystack rebuild_index 命令:

(.venv)vagrant@precise32:/app$ foreman run ./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.
DEBUG Making a request equivalent to this: curl -XDELETE 'http://127.0.0.1:9200/test_app' -d '""'
INFO Starting new HTTP connection (1): 127.0.0.1
DEBUG "DELETE /test_app HTTP/1.1" 200 31
DEBUG response status: 200
DEBUG got response {u'acknowledged': True, u'ok': True}
DEBUG Making a request equivalent to this: curl -XPOST 'http://127.0.0.1:9200/test_app/_refresh' -d '""'
DEBUG "POST /test_app/_refresh HTTP/1.1" 404 66
DEBUG response status: 404
Failed to clear Elasticsearch index: (404, u'IndexMissingException[[test_app] missing]')
ERROR Failed to clear Elasticsearch index: (404, u'IndexMissingException[[test_app] missing]')
All documents removed.

查看此日志记录 - 似乎 Haystack 正在尝试刷新它刚刚删除的索引 - 这总是会失败。

我做错了什么?

[更新 1]

如果我拆分 POST 请求,我可以让它运行:

(.venv)vagrant@precise32:/app$ curl -XPOST 'http://127.0.0.1:9200/test_app/'
{"ok":true,"acknowledged":true}

(.venv)vagrant@precise32:/app$ curl -XPOST 'http://127.0.0.1:9200/test_app/_refresh' -d    '""'
{"ok":true,"_shards":{"total":10,"successful":5,"failed":0}}

[更新 2]

深入代码,运行clear_index时调用的ES后端方法是:

    def clear(self, models=[], commit=True):
        [...]
        if not models:
            self.conn.delete_index(self.index_name)
        else:
            [...]
        if commit:
            self.conn.refresh(index=self.index_name)

哪个看起来不对,因为它会在刚刚删除的索引上调用 conn.refresh

[更新 3]

我认为上面的错误可能是转移注意力,因为管理命令会忽略错误并继续,给出这个错误,我认为这是更严重的:

(.venv)vagrant@precise32:/app$ foreman run ./manage.py update_index --verbosity=3
Skipping '<class 'django.contrib.auth.models.Permission'>' - no index.
Skipping '<class 'django.contrib.auth.models.Group'>' - no index.
Skipping '<class 'django.contrib.auth.models.User'>' - no index.
Skipping '<class 'django.contrib.contenttypes.models.ContentType'>' - no index.
Skipping '<class 'django.contrib.sessions.models.Session'>' - no index.
Skipping '<class 'django.contrib.sites.models.Site'>' - no index.
Skipping '<class 'django.contrib.admin.models.LogEntry'>' - no index.
Skipping '<class 'django.contrib.flatpages.models.FlatPage'>' - no index.
ERROR Error updating test_app using default
Traceback (most recent call last):
  File "/home/vagrant/.venv/src/django-haystack/haystack/management/commands/update_index.py", line 210, in handle_label
    self.update_backend(label, using)
  File "/home/vagrant/.venv/src/django-haystack/haystack/management/commands/update_index.py", line 239, in update_backend
    end_date=self.end_date)
  File "/home/vagrant/.venv/src/django-haystack/haystack/indexes.py", line 157, in build_queryset
    index_qs = self.index_queryset(using=using)
TypeError: index_queryset() got an unexpected keyword argument 'using'
Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/vagrant/.venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
    utility.execute()
  File "/home/vagrant/.venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/vagrant/.venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/vagrant/.venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute
    output = self.handle(*args, **options)
  File "/home/vagrant/.venv/src/django-haystack/haystack/management/commands/update_index.py", line 184, in handle
    return super(Command, self).handle(*items, **options)
  File "/home/vagrant/.venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 341, in handle
    label_output = self.handle_label(label, **options)
  File "/home/vagrant/.venv/src/django-haystack/haystack/management/commands/update_index.py", line 210, in handle_label
    self.update_backend(label, using)
  File "/home/vagrant/.venv/src/django-haystack/haystack/management/commands/update_index.py", line 239, in update_backend
    end_date=self.end_date)
  File "/home/vagrant/.venv/src/django-haystack/haystack/indexes.py", line 157, in build_queryset
    index_qs = self.index_queryset(using=using)
TypeError: index_queryset() got an unexpected keyword argument 'using'

[更新 4]

好的 - 所以这是我的错,我使用的是旧的 search_indexes.py 文件,而且我的 index_queryset() 方法不正确。我不会关闭它,因为它可能对其他人有用。

最佳答案

我自己回答这个问题——尽管这只是承认我自己在这个问题上的愚蠢。

我将 1.x 版 Haystack 中的 search_indexes.py 文件带到了我们项目的一个新分支中,该分支使用的是 2.x 版的 Haystack,其配置略有不同。在新版本中,index_queryset() 方法现在需要一个新的 using 参数(默认为 None)。旧版本不需要这个。

因此新签名应该是:

def index_queryset(self, using=None):
    pass

关于django - 使用 django-haystack rebuild_index 命令重建 ElasticSearch 索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16261913/

相关文章:

python - Paypal 为 IPN 发送了警告邮件

python - 日期的默认格式

elasticsearch - 如何在 Elasticsearch 中获取整个索引的术语向量信息?,而不是在文档级别

memory - 如何检查 indices.memory.index_buffer_size 参数在 elasticsearch 中是否有效?

python - haystack 多字段搜索

python - 覆盖 Haystack 中的用户查询

python - 如何从外部脚本更新 haystack 的索引?

python - django - 获取模型方法结果的模型字段

python - 单个 Django 项目的两个虚拟环境?

elasticsearch - Elasticsearch :使用字段时查询字符串的结果不同