【问题标题】:Ensuring settings and mapping are set on index in Elasticsearch确保在 Elasticsearch 中的索引上设置设置和映射
【发布时间】:2012-09-11 12:46:28
【问题描述】:

为了确保我的 Elasticsearch 索引具有正确的设置和映射,我有以下代码:

if (client.admin().indices().prepareExists(Index).execute().actionGet().exists()) {            
    client.admin().indices().prepareClose(Index).execute().actionGet();
    client.admin().indices().prepareUpdateSettings(Index).setSettings(settings.string()).execute().actionGet();
    client.admin().indices().prepareOpen(Index).execute().actionGet();
    client.admin().indices().prepareDeleteMapping(Index).setType(Type).execute().actionGet();
    client.admin().indices().preparePutMapping(Index).setType(Type).setSource(mapping).execute().actionGet();
} else {
    client.admin().indices().prepareCreate(Index).addMapping(Type, mapping).setSettings(settings).execute().actionGet();
}

更新设置和映射似乎有点傻,即使它们已经很好了。不过,我不知道如何以更聪明的方式做事。有什么建议吗?

非常感谢,

斯汀

【问题讨论】:

  • 在创建索引时不能只传递一次设置和映射吗?
  • 我的问题是索引已经创建并正在使用。

标签: elasticsearch


【解决方案1】:

您可以检索当前设置和映射,并在尝试更新它们之前确保它们是正确的。

ClusterStateResponse response = client.admin().cluster().prepareState()                
    .setFilterAll()
    .setFilterMetaData(false)
    .setFilterIndices(index)
    .execute().actionGet();
IndexMetaData indexMetaData = response.state().metaData().index(index);
Settings settings = indexMetaData.settings();
// Verify settings
MappingMetaData mapping = indexMetaData.mappings().get(type);
// Verify mapping

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-04
    • 2016-01-12
    • 2017-02-23
    • 2014-11-27
    • 1970-01-01
    • 2017-07-04
    • 2014-09-24
    • 1970-01-01
    相关资源
    最近更新 更多