我正在尝试使用RestHighLevelClient为弹性创建动态模板。 getMapping()
中带注释的代码段适用于有效的静态映射。但是,在尝试创建动态映射时遇到了问题。
我的意图是将任何以_id
结尾的属性和任何以_message
结尾的属性当作文本。
有人可以指出我在做什么错吗?
":{"root_cause":[{"type":"class_cast_exception","reason":"class_cast_exception: java.util.LinkedHashMap cannot be cast to java.util.List"}],"type":"mapper_parsing_exception","reason":"Failed to parse mapping [_doc]: java.util.LinkedHashMap cannot be cast to java.util.List","caused_by":{"type":"class_cast_exception","reason":"class_cast_exception: java.util.LinkedHashMap cannot be cast to java.util.List"}},"status":400}
我的代码段:
主要方法:
CreateIndexRequest request = new CreateIndexRequest(indexName);
request.settings(Settings.builder()
.put("index.number_of_shards", 3)
.put("index.number_of_replicas", 2)
);
request.mapping("_doc", getMapping());
CreateIndexResponse createIndexResponse = esClient.indices().create(request, RequestOptions.DEFAULT);
帮助方法:
private static XContentBuilder getMapping() throws IOException {
XContentBuilder mappingBuilder = XContentFactory.jsonBuilder()
.startObject()
.startObject("_doc")
.startObject("dynamic_templates")
.startObject("id")
.field("match", "*_id")
.field("match_mapping_type", "long")
.endObject()
.startObject("message")
.field("match", "*_message")
.field("match_mapping_type", "string")
.endObject()
.endObject()
.endObject()
.endObject();
/*XContentBuilder mappingBuilder = XContentFactory.jsonBuilder()
.startObject()
.startObject("_doc")
.startObject("properties")
.startObject("user")
.field("type", "text")
.field("index", true)
.endObject()
.startObject("message")
.field("type", "text")
.field("index", true)
.endObject()
.endObject()
.endObject()
.endObject();*/
return mappingBuilder;
}
参考指南:https://www.elastic.co/guide/en/elasticsearch/guide/master/custom-dynamic-mapping.html
答案 0 :(得分:-1)
动态模板是数组。 例如,Elasticsearch 7.1.0
private XContentBuilder buildDynamicTemplates() throws IOException {
XContentBuilder dynamicTemplate = XContentFactory.jsonBuilder();
dynamicTemplate.startObject();
{
dynamicTemplate = DynamicTemplateFactory.templateBuilder(dynamicTemplate);
}
dynamicTemplate.endObject();
return dynamicTemplate;
}
private static XContentBuilder builderTemplate() throws IOException {
XContentBuilder dynamicTemplate = XContentFactory.jsonBuilder();
dynamicTemplate.startObject();
dynamicTemplate.startArray("dynamic_templates");
{
dynamicTemplate.startObject();
{
dynamicTemplate.startObject("strings_as_keywords");
{
dynamicTemplate.field("match", "*");
dynamicTemplate.field("match_mapping_type", "string");
dynamicTemplate.startObject("mapping");
{
dynamicTemplate.field("type", "keyword");
}
dynamicTemplate.endObject();
}
dynamicTemplate.endObject();
}
dynamicTemplate.endObject();
}
dynamicTemplate.endArray();
dynamicTemplate.endObject();
return dynamicTemplate;
}