2014-01-22 26 views
3

使用https://gist.github.com/wingdspur/2026107其在http://localhost:9200开始的elasticsearch集群在我的机器上安装elasticsearch后,我试图创建与设置的指标,用这个代码:IndexPrimaryShardNotAllocatedException错误,而试图关闭一个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 

当我执行此代码,我得到一个错误时,我试图执行“关闭”方法(上述第3行)

Elasticsearch::Transport::Transport::Errors::Conflict: 
    [409] {"error":"IndexPrimaryShardNotAllocatedException[[test_1] primary not allocated post api]","status":409} 

我尝试添加刷新方法(上面的第2行)有时工作,有时没有,我有'有时工作'的感觉意味着我做错了什么。谢谢您的帮助!

回答

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' 

希望这可以帮助别人!

+1

正如仅供参考,您不需要关闭索引来更改设置/映射。打开/关闭实际上仅用于归档您想要保留的旧数据,但不需要主动搜索。 – Zach

+4

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

+0

@MartijnLaarman啊,很好,谢谢澄清! – Zach