# Inner Object Java API

**URL:** <https://discuss.elastic.co/t/inner-object-java-api/78177>\
**Category:** Elasticsearch\
**Created:** [March 10, 2017, 7:21pm UTC](https://discuss.elastic.co/t/inner-object-java-api/78177 "2017-03-10T19:21:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![missourian](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@missourian](https://discuss.elastic.co/u/missourian)\
**Post date:** [March 10, 2017, 7:21pm UTC](https://discuss.elastic.co/t/inner-object-java-api/78177/1 "2017-03-10T19:21:47Z")

</div>

Hello,

I am doing a proof of concept, need help on how to dynamically add an inner object.

Java Class:

```
public static class Person {

    String name;

    String address;

    int age;

    OffsetDateTime dob;

    Car car; // one to one
}

```

Junit Test to insert the person:

```
@Test
public void insertPerson() {
    final Person p1 = new Person();
    p1.setName("Mike");
    p1.setAge(26);
    p1.setAddress("123 Main Street");
    p1.setDob(OffsetDateTime.now());

    final JSONObject personJson = new JSONObject(p1);

    bulkProcessor.add(new IndexRequest("person", "data")
        .source(personJson.toString()));
}

```

After Insert Elasticsearch Document:

```
_id AVq5kcaMnKnyDTbOM5ey
_index person
_score - 
_type data
address 123 Main Street
age 26
dob March 10th 2017, 12:53:42.722
name Mike

```

Updating the above document

```
@Test
public void updatePerson() throws IOException {
    final Car mikesCar = new Car();
    mikesCar.setMake("Honda");
    mikesCar.setModel("Civic");
    mikesCar.setYear(2014);

    final JSONObject carObj = new JSONObject(mikesCar);
    final UpdateRequest updateRequest = new UpdateRequest();
    updateRequest.index("person");
    updateRequest.type("data");
    updateRequest.id("AVq5kcaMnKnyDTbOM5ey");

    updateRequest.doc(jsonBuilder()
        .map(carObj.toMap()));

    bulkProcessor.add(updateRequest);
}

```

After the update, the document looks like below

```
_id AVq5kcaMnKnyDTbOM5ey
_index person
_score - 
_type data
address 123 Main Street
age 26
dob March 10th 2017, 12:53:42.722
make Honda
model Civic
name Mike
year 2014

```

**I want this to be like below**

```
_id AVq5kcaMnKnyDTbOM5ey
_index person
 _score - 
 _type data
 address 123 Main Street
 age 26
 dob March 10th 2017, 12:53:42.722
 car.make Honda
 car.model Civic
 name Mike
 car.year 2014

```

Can someone help me how to achieve this without encapsulating Car into Person object? Any other efficient ways?

Solution working for me

```
 @Test
public void updatePerson() throws IOException {
    final Car mikesCar = new Car();
    mikesCar.setMake("Honda");
    mikesCar.setModel("Civic");
    mikesCar.setYear(2014);

    final JSONObject parObj = new JSONObject();

    final JSONObject carObj = new JSONObject(mikesCar);

    parObj.put("car", carObj);
    final UpdateRequest updateRequest = new UpdateRequest();
    updateRequest.index("person");
    updateRequest.type("data");
    updateRequest.id("AVq5z77RnKnyDTbOM5e4");

    updateRequest.doc(jsonBuilder()
        .map(parObj.toMap()));

    bulkProcessor.add(updateRequest);
}
```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 10, 2017, 10:15pm UTC](https://discuss.elastic.co/t/inner-object-java-api/78177/2 "2017-03-10T22:15:38Z")

</div>

Just a small remark as you found the solution by yourself. You can also send again the full person object instead of dealing with updates.

I'd write something like:

```auto
final Car mikesCar = new Car();
mikesCar.setMake("Honda");
mikesCar.setModel("Civic");
mikesCar.setYear(2014);

Person mike = // Read from your db or whatever;
mike.setCar(mikesCar);

bulkProcessor.add(new IndexRequest("person", "data", "AVq5kcaMnKnyDTbOM5ey")
        .source(new JSONObject(mike).toString()));

```

---

<div class="post-metadata">

**Author:** ![missourian](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@missourian](https://discuss.elastic.co/u/missourian)\
**Post date:** [March 11, 2017, 4:39am UTC](https://discuss.elastic.co/t/inner-object-java-api/78177/3 "2017-03-11T04:39:05Z")

</div>

Thank You for the response, I need to read the Mike from ES. It has few fields with huge text string. Do we still see any performance improvements?

I need to use **elasticClient**. **searchIndex** () and perform an index. My local tests shows update using bulk processor without need to use search seems faster.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 11, 2017, 2:38pm UTC](https://discuss.elastic.co/t/inner-object-java-api/78177/4 "2017-03-11T14:38:34Z")

</div>

> [@missourian](#):
>
> It has few fields with huge text string. Do we still see any performance improvements?

In that case it can make sense to use update API. It will reduce the network usage.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 8, 2017, 2:38pm UTC](https://discuss.elastic.co/t/inner-object-java-api/78177/5 "2017-04-08T14:38:35Z")

</div>

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