Inner Object Java API

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

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:

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()));
1 Like

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.

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

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