How to write json array to elasticsearch's objetct field by es rest client?

I use elasticsearch6.8 and jdk8. The key problem is hot to write json array string to elasticsearch's object type field. Following code show how I tried to write to es.

My mapping is as follows:
{ "mapping": { "_doc": { "properties": { "c_json": { "type": "object" } } } } }

My c_json value is :

{ "key1": "val1", "key2": "val2", "key3": "val3" }

My java code is :

String json="{
  "key1": "val1",
  "key2": "val2",
  "key3": "val3"
}";
String keyName="c_json";
Map<String,String> myDataMap = new HashMap();
myDataMap.put(keyName,json);

IndexRequest indexRequest = new IndexRequest("my_index").id(esIdValue)
                        .type("_doc")
                        .source(XContentType.JSON,convertMapToEsValues(myDataMap));
           BulkRequest bulkRequest = new BulkRequest();
            BulkResponse bulkResponse = es6Client.bulk(bulkRequest, RequestOptions.DEFAULT);


    public static Object[] convertMapToEsValues(Map<String, Object> valueMap) {
        List<Object> kvList = Lists.newArrayList();
        for (String fieldName : valueMap.keySet()) {
            kvList.add(fieldName);
            kvList.add(valueMap.get(fieldName));
        }
        return kvList.toArray();
    }

When I run the code, es server give me the exception:
ElasticsearchException[Elasticsearch exception [type=mapper_parsing_exception, reason=object mapping for [c_json] tried to parse field [c_json] as object, but found a concrete value]],action id is 9, Index name is my_index

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.