简体   繁体   中英

Elastic search The _parent field's type option can't be changed: [null]->[metadata]

This is my scenario: I created two indexes in elasticsearch with type as "data" and "metadata". I am trying to establish a parent-child mapping between the data and metadata with the metadata being the parent of the data. I am using the elasticsearch transport client java api to do this.

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

but i get the following error-

   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)

I tried searching online but couldn't get a specific answer. I don't want to delete the existing index and add the index with mapping since i have existing info in the index that i need to use.(some people suggested doing that)

Is this is a bug in elasticsearch?

If not is my code incorrect and how do i solve it?

Whether it's a "bug" I can't say, it certainly is a limitation, see also https://github.com/elastic/elasticsearch/issues/9448 .

The only way to make this work is to add _parent already at index creation time and not after it (verified with 5.2.2 just now).

With tools available like aliasing it is possible to do these kind of things without having to remove the current index first, but it requires good planning of your steps and should be properly tested.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM