尝试关闭elasticsearch集群时IndexPrimaryShardNotAllocatedException错误

shi*_*las 3 ruby elasticsearch

用我的机器上安装elasticsearch后https://gist.github.com/wingdspur/2026107它开始于一个elasticsearch集群http://localhost:9200,我试图创建设置的指标,并使用此代码:

  search_indices.create index: 'test'
  search_indices.refresh index: 'test'
  search_indices.close index: 'test'
  search_indices.put_settings(index: 'test', **my_index_settings)
  search_indices.put_mapping(index: 'test', **my_mapping_settings)
  search_indices.open index: 'test'
Run Code Online (Sandbox Code Playgroud)

哪里

search_indices = Elasticsearch::Client.new({host: 'http://localhost:9200'}).indices
# this module comes from the elasticsearch-ruby gem
Run Code Online (Sandbox Code Playgroud)

当我执行此代码时,当我尝试执行'close'方法时,我收到错误(上面的第3行)

Elasticsearch::Transport::Transport::Errors::Conflict:
   [409] {"error":"IndexPrimaryShardNotAllocatedException[[test_1] primary not allocated post api]","status":409}
Run Code Online (Sandbox Code Playgroud)

我尝试添加刷新方法(上面的第2行),有时可以工作,有时不工作,我有一种感觉,"有时工作"意味着我做错了什么.谢谢您的帮助!

shi*_*las 6

所以我偶然发现了使用ruby API的集群运行状况选项,它似乎正常工作.

这是我解决它的方式:

def search_client
  # I should memoize this so I don't have to keep creating new indices
  @search_client ||= Elasticsearch::Client.new({host: 'http://localhost:9200'})
end

def search_indices
  search_client.indices
end
Run Code Online (Sandbox Code Playgroud)

然后我上面的代码看起来像:

  search_indices.create index: 'test'
  search_client.cluster.health wait_for_status: 'green'
  search_indices.close index: 'test'
  search_indices.put_settings(index: 'test', **my_index_settings)
  search_indices.put_mapping(index: 'test', **my_mapping_settings)
  search_indices.open index: 'test'
Run Code Online (Sandbox Code Playgroud)

希望这有助于某人!

  • @Zach除非您在put设置调用中注册新的分析器,在这种情况下,您需要关闭并重新打开索引.http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-update-settings.html (4认同)