ndtreviv
(Nathan Trevivian)
March 25, 2024, 9:11am
1
I'm using the Java SDK to update a document, like so:
UpdateResponse<Void> response = elasticsearchService.getClient().update(u -> u.index(INDEX)
.id(id)
.doc(JsonData.of(myObject)).refresh(Refresh.True), Void.class);
In this case myObject has a @Transient
property on it: id
, which I populate with _id
when I load it from elasticsearch.
Unfortunately, when I update like this, the @Transient
id
field gets serialised and written into elasticsearch.
How can I prevent that?
swallez
(Sylvain Wallez)
March 25, 2024, 10:12am
2
@Transient
is a JPA annotation that has no impact on JSON serialization. You should probably add @JsonIgnore
on that property.
See https://www.baeldung.com/java-jsonignore-vs-transient
ndtreviv
(Nathan Trevivian)
March 25, 2024, 11:14am
3
Thanks, but that's not going to work in this situation. I actually want the id property to be serialised for my user interface.
Literally the only time I don't want it serialised is when it's being written into elasticsearch.
Are there other options? Perhaps something using a Jackson Mixin?
swallez
(Sylvain Wallez)
March 25, 2024, 11:35am
4
You should look at Jackson's views and configure a view on the ObjectMapper
used to create the JacksonJsonpMapper
in your Elasticsearch client.
See https://www.baeldung.com/jackson-json-view-annotation
ndtreviv
(Nathan Trevivian)
March 25, 2024, 11:47am
5
This looks good - do you have an example for how one might configure the ObjectMapper using the Java SDK?
this.restClient = RestClient.builder(new HttpHost(elasticsearchHost, elasticsearchPort, elasticsearchScheme))
.setHttpClientConfigCallback(createHttpClientConfigCallback(elasticsearchUsername, elasticsearchPassword, Boolean.TRUE.equals(allowSelfSignedCert)))
.setRequestConfigCallback(createRequestConfigCallback())
.build();
this.client = new ElasticsearchClient(new RestClientTransport(restClient, new JacksonJsonpMapper()));
Assuming where I'm using that new JacksonJsonpMapper()
...?
swallez
(Sylvain Wallez)
March 25, 2024, 12:41pm
6
Yes. There is an additional constructor where you can provide a Jackson ObjectMapper
, which you can configure at will.
system
(system)
Closed
April 22, 2024, 12:42pm
7
This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.