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