yN. yN. - 4 months ago 35
Java Question

Elasticsearch: Index arrays which contain nested objects with the Java API without predefined mapping?

Is there a way to index arrays which contain nested objects with the Java API without predefined mapping?

I would like to create the following structure:

"amazingArray": [
{ "uId": "123", "cars": ["fastCar", "slowCar"]},
{ "uId": "234", "cars": ["niceCar", "badCar"]},
...
]

yN. yN.
Answer
XContentBuilder object = XContentFactory.jsonBuilder()
 .startArray("amazingArray")
  .startObject()
   .field("uId","123")
   .startArray("cars")
    .value("fastCar")
    .value("slowCar")
   .endArray()
  .endObject()

  .startObject()
   ...
  .endObject()
 .endArray();