elasticsearch - 更改 Elasticsearch 中现有索引的设置和映射

标签 elasticsearch

我想在 Elasticsearch 中的现有索引上设置以下设置和映射:

{
    "analysis": {
        "analyzer": {
            "dot-analyzer": {
                "type": "custom",
                "tokenizer": "dot-tokenizer"
            }
        },
        "tokenizer": {
            "dot-tokenizer": {
                "type": "path_hierarchy",
                "delimiter": "."
            }
        }
    }
}

{
    "doc": {
        "properties": {
            "location": {
                "type": "string",
                "index_analyzer": "dot-analyzer",
                "search_analyzer": "keyword"
            }
        }
    }
}

我尝试添加这两行代码:

client.admin().indices().prepareUpdateSettings(Index).setSettings(settings).execute().actionGet();
client.admin().indices().preparePutMapping(Index).setType(Type).setSource(mapping).execute().actionGet();

但这是结果:

org.elasticsearch.index.mapper.MapperParsingException: Analyzer [dot-analyzer] not found for field [location]

有人吗?非常感谢,

斯坦


这似乎可行:

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();
}

最佳答案

如果您在发送更改后查看您的设置,您会发现分析器不存在。事实上,您无法更改实时索引设置的分析部分。最好使用所需的设置创建它,否则您可以关闭它:

curl -XPOST localhost:9200/index_name/_close

当索引关闭时,您可以发送新设置。之后您可以重新打开索引:

curl -XPOST localhost:9200/index_name/_open

当索引关闭时它不使用任何集群资源,但它不可读也不可写。如果您想使用 Java API 关闭和重新打开索引,您可以使用以下代码:

client.admin().indices().prepareClose(indexName).execute().actionGet();
//TODO update settings
client.admin().indices().prepareOpen(indexName).execute().actionGet();

关于elasticsearch - 更改 Elasticsearch 中现有索引的设置和映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12367877/

相关文章:

elasticsearch - 如何在Elastic Search中增加具有动态属性的嵌套对象?

spring - 如何为父子索引创建映射以及如何搜索按父过滤的子

python - 如何从外部脚本更新 haystack 的索引?

elasticsearch - 如何启用内联(沙盒)groovy 脚本?

elasticsearch - 安装和配置Shield for Elasticsearch

elasticsearch - Elasticsearch高更新索引压缩

elasticsearch - 在使用 inner_hits 时指定要为 ElasticSearch 查询返回的结果的总大小

mysql - 使用 Node.js 搜索 PDF 文本并返回片段

elasticsearch - 如何在 Elasticsearch 中对多个索引的结果进行排序和限制

elasticsearch - Elasticsearch低于现有领域