4

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

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

我的问题是 Haystackrebuild_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]

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

    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()方法不正确。我不会关闭它,因为它可能对其他人有用。

4

1 回答 1

5

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

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

因此,新签名应为:

def index_queryset(self, using=None):
    pass
于 2013-04-29T10:05:47.507 回答