Ignore fields when indexing using JsonData.of

I'm using the Java SDK to update a document, like so:

UpdateResponse<Void> response = elasticsearchService.getClient().update(u -> u.index(INDEX)
    .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?

@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

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?

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

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

        this.client = new ElasticsearchClient(new RestClientTransport(restClient, new JacksonJsonpMapper()));

Assuming where I'm using that new JacksonJsonpMapper()...?

Yes. There is an additional constructor where you can provide a Jackson ObjectMapper, which you can configure at will.