Elasticsearch JAVA API Client version 8 upsert Request

I am doing a migration from version 7 which is deprecated to version 8 java api client , but there is no documentation regarding upsert in version 8 , i have to write a updateRequest to update a document if it exists and if not create the document ,
like in my code i am using these for deprecated version

UpdateRequest()).index(this.Instance.getName())).id(entity.getId()).upsert(this.objectMapper.writeValueAsString(entity), XContentType.JSON).doc(updateFields).retryOnConflict(3);

Hi @durgesh_dp

Maybe this snippet help you.

UpdateRequest<Movie, Object> movieUpdateRequest = new UpdateRequest.Builder<Movie, Object>()
    .index("idx_movies")
    .id("mv_001")
    .doc(movie)
    .docAsUpsert(true)
    .retryOnConflict(3)
    .build();
    client.update(movieUpdateRequest, Movie.class);
1 Like

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