I need to store documents into Elasticsearch indexes and therefor I defined a mapping. From my java client I need to supply it with pojo's which looks like the Compony class. It has a lot of duplicated values. I could use object composition pattern to solve this, but Elasticsearch can't handle that kind of structure and therefor it expects a flatten structure.
class Company {
private String postalstreetName;
private String postalHouseNumer;
private String postalHouseLetter;
private String postalHouseNumberAddition;
private String postalZipCode;
private String postalCity;
private String postalCountry;
private String visitorstreetName;
private String visitorHouseNumer;
private String visitorHouseLetter;
private String visitorHouseNumberAddition;
private String visitorZipCode;
private String visitorCity;
private String visitorCountry;
private String establishmentstreetName;
private String establishmentHouseNumer;
private String establishmentHouseLetter;
private String establishmentHouseNumberAddition;
private String establishmentZipCode;
private String establishmentCity;
private String establishmentCountry;
}
I actually want pojo's like below and somehow auto generate the pojo above with the same naming structure.
class Address {
private String streetName;
private String houseNumer;
private String houseLetter;
private String houseNumberAddition;
private String zipCode;
private String city;
private String country;
}
class Company {
private Address postalAddress;
private Address visitorAddress;
private Address establishmentAddress;
}
Does someone know if something like this is possible, to auto generate a flatten pojo from a pojo with object composition with a name prefix for all the fields?
I got the solution for my problem. Elasticsearch is expecting a json object with the structure what I have in the first example. So what I did was adding the @JsonUnwrapped
to the field with a prefix value, so my Company class looks like this:
class Company {
@JsonUnwrapped(prefix = "postal")
private Address postalAddress;
@JsonUnwrapped(prefix = "visitor")
private Address visitorAddress;
@JsonUnwrapped(prefix = "Establishment")
private Address establishmentAddress;
}
And the result will be:
{
"postalStreetName":"",
"postalHouseNumer":"",
"postalHouseLetter":"",
"postalHouseNumberAddition":"",
"postalCity":"",
"postalCountry":"",
"visitorStreetName":"",
"visitorHouseNumer":"",
"visitorHouseLetter":"",
"visitorHouseNumberAddition":"",
"visitorCity":"",
"visitorCountry":"",
"establishmentStreetName":"",
"establishmentHouseNumer":"",
"establishmentHouseLetter":"",
"establishmentHouseNumberAddition":"",
"establishmentCity":"",
"establishmentCountry":""
}
So I don't need to somehow magically create those pojo's because jackson can create the json files which I need.