当前位置: 首页 > 知识库问答 >
问题:

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

东郭海阳
2023-03-14

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

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

我的问题是Haystackrebuild\u 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.

看看这个日志记录——似乎干草堆正试图刷新它刚刚删除的索引——它总是会失败。

我做错了什么?

[更新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个答案

蒋永宁
2023-03-14

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

我带了一个search\u索引。py来自1的文件。将Haystack的x版本引入到我们项目的一个新分支中,该分支使用2。Haystack的x版本,配置略有不同。在新版本中,index\u queryset()方法现在需要一个使用参数的新的(默认为None)。旧版本不需要这个。

因此,新的签字应为:

def index_queryset(self, using=None):
    pass

 类似资料:
  • 问题内容: 我正在使用带有NEST的C#.NET应用程序来创建索引。 我创建了一个Elasticsearch索引,客户可以查询该索引,称为index_1。然后,我使用应用程序的不同实例构建索引的另一个版本,并将其称为index_1_temp。 我将index_1_temp重命名为index_1然后删除原始index_1的最安全方法是什么? 我知道ES具有别名,但是我不确定如何将其用于此任务 编辑:

  • 问题内容: 我是ElasticSearch的新手。我正在尝试为索引重新索引以便对其重命名。我正在使用NEST API v5.4。我看到了这个例子: 资料来源 :http : //thomasardal.com/elasticsearch-migrations-with-c-and- nest/ 但是,我无法使用NEST 5.4重现此内容。我认为这是2.4版。我检查ElasticSearch的重大更

  • 问题内容: 我正在努力完成索引创建这一简单任务,目标是使用分析器和字段映射创建索引。当我使用分析器创建索引时,我可以通过分析api调用与分析器通信,但是当我添加映射信息时,创建索引调用失败,并显示“字段[$ field]]找不到Analyzer [analyzer1]”,我创建了一个脚本来显示问题: 问题答案: 我相信您的问题是这些设置需要嵌套在JSON的一个节点内,而不是您所拥有的嵌套在一个节点

  • 当我跑的时候 我得到以下错误: 我试着改变我的ElasticSearch的版本。我的当前版本是: $curl-xget'localhost:9200'{“name”:“mokbeeq”,“cluster_name”:“elasticsearch”,“cluster_uuid”:“pf_z62bbtl-jq31hsuahqa”,“version”:{“number”:“5.6.8”,“build_h

  • 本文向大家介绍MySQL使用命令创建、删除、查询索引的介绍,包括了MySQL使用命令创建、删除、查询索引的介绍的使用技巧和注意事项,需要的朋友参考一下 MySQL数据库表可以创建、查看、重建和删除索引,索引可以提供查询速度。索引根据分类,分为普通索引和唯一索引;有新建索引、修改索引和删除。但是索引不是到处都可以创建,需要根据具体的条件。下面利用实例说明索引创建到销毁的过程,操作如下: 熟悉使用My

  • “adb root”命令的描述说,它使用root权限重新启动adbd。 我在我的手机(Note 2 Android 4.3)上试了一下,它没有root,也没有用。我在网上搜索,发现即使在root设备上,它也会给出错误消息,例如“adbd无法在生产版本中以root身份运行”。 我的问题是在什么情况下'adb root'会以root权限成功重启adbd? 谢谢