使用https://gist.github.com/wingdspur/2026107在http://localhost:9200
上启动弹性搜索群集在我的计算机上安装elasticsearch后,我尝试使用设置创建索引并使用此代码:
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'
其中
search_indices = Elasticsearch::Client.new({host: 'http://localhost:9200'}).indices
# this module comes from the elasticsearch-ruby gem
当我执行此代码时,当我尝试执行'close'方法(上面第3行)时出现错误
Elasticsearch::Transport::Transport::Errors::Conflict:
[409] {"error":"IndexPrimaryShardNotAllocatedException[[test_1] primary not allocated post api]","status":409}
我尝试添加刷新方法(上面的第2行),有时可以工作,有时不工作,我有一种感觉,“有时工作”意味着我做错了什么。谢谢你的帮助!
答案 0 :(得分: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
然后我的代码如下:
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'
希望这有助于某人!