Create index with JSON XContentBuilder get a error

Hi all , i met a weird issue , when i use JSON type XContentBuilder to create index's mapping ,it always return "Failed to close the XContentBuilder Caused by: java.io.IOException: Unclosed object or array found
at org.elasticsearch.common.xcontent.json.JsonXContentGenerator.close(JsonXContentGenerator.java:469)
at org.elasticsearch.common.xcontent.XContentBuilder.close(XContentBuilder.java:1000)
... 36 common frames omitted".

This is the code :

private XContentBuilder getMappingBuilder() throws IOException {
XContentBuilder builder = XContentFactory.jsonBuilder()
.startObject()
.startObject("properties")
.startObject("@timestamp").field("type", "date")
.startObject("timestamp").field("format", Constants.DateFormatSuper).field("type", "date")
.endObject()
.startObject("nation")
.field("type", "keyword")
.endObject()
.startObject("app")
.field("type", "keyword")
.endObject()
.startObject("order_no")
.field("type", "keyword")
.endObject()
.startObject("loan_type")
.field("type", "keyword")
.endObject()
.startObject("loan_scenario")
.field("type", "keyword")
.endObject()
.startObject("product_type")
.field("type", "keyword")
.endObject()
.startObject("installments").field("type", "integer").endObject()
.startObject("platform")
.field("type", "keyword")
.endObject()
.startObject("amount").field("type", "double").endObject()
.startObject("latitude").field("type", "double").endObject()
.startObject("longitude").field("type", "double").endObject()
.startObject("location")
.field("type", "text")
.startObject("fields")
.startObject("raw")
.field("type", "keyword")
.endObject()
.endObject()
.endObject()
.startObject("geohash")
.field("type", "geo_point")
.endObject()
.startObject("city_geohash")
.field("type", "geo_point")
.endObject()
.endObject()
.endObject();
return builder;
}

restHighLevelClient.indices().create(new CreateIndexRequest(indexName).mapping(getMappingBuilder())
.settings(Settings.builder()
.put("index.number_of_shards", shards)
.put("index.number_of_replicas", replicas))
, RequestOptions.DEFAULT);

You have 20 startObject() and 19 endObject(), which is probably the reason that "IOException: Unclosed object ".

果然是这个问题,非常感谢

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