这是我的场景:我在 elasticsearch 中创建了两个索引,类型为“数据”和“元数据”。我正在尝试在数据和元数据之间建立父子映射,元数据是数据的父级。我正在使用 elasticsearch 传输客户端 java api 来执行此操作。
//parent mapping json
String parentMapping = XContentFactory.jsonBuilder()
.startObject()
.startObject(parentType)
.endObject()
.string();
//child mapping json
String childMapping = XContentFactory.jsonBuilder()
.startObject()
.startObject(childType)
.startObject("_parent")
.field("type", parentType)
.endObject()
.endObject()
.endObject()
.string();
System.out.println("childMapping="+childMapping);
System.out.println("parentMapping="+parentMapping);
client.admin().indices().preparePutMapping(indexName).setType(parentType)
.setSource(parentMapping).execute().actionGet();
//This does the mapping
PutMappingRequestBuilder putMappingRequestBuilder = client.admin().indices().preparePutMapping(indexName).setType(childType);
putMappingRequestBuilder.setSource(childMapping);
PutMappingResponse response = putMappingRequestBuilder.execute().actionGet();
if(!response.isAcknowledged()) {
LogManager.log("Could not define mapping for type ["+indexName+"]/["+childType+"]",LogManager.DEBUG);
tries=tries+1;
} else {
mapLoop=true;
LogManager.log("Successfully put mapping for ["+indexName+"]/["+childType+"]",LogManager.DEBUG);
}
但我收到以下错误-
java.lang.IllegalArgumentException: The _parent field's type option can't be changed: [null]->[metadata]
at org.elasticsearch.index.mapper.internal.ParentFieldMapper.doMerge(ParentFieldMapper.java:389)
at org.elasticsearch.index.mapper.FieldMapper.merge(FieldMapper.java:364)
at org.elasticsearch.index.mapper.MetadataFieldMapper.merge(MetadataFieldMapper.java:75)
at org.elasticsearch.index.mapper.Mapping.merge(Mapping.java:120)
at org.elasticsearch.index.mapper.DocumentMapper.merge(DocumentMapper.java:376)
我尝试在网上搜索,但无法得到具体答案。我不想删除现有索引并添加带有映射的索引,因为我在索引中有需要使用的现有信息。(有些人建议这样做)
这是弹性搜索中的错误吗?
如果不是,我的代码不正确,我该如何解决?