Build IndexRequest with elastic in 8.1.0

Hi, I am migrating from HLRC to Elasticsearch Java API Client. I need to transform below code

private IndexRequest buildIndex(String indexName, CacheObject cacheObject) {
        IndexRequest indexRequest = new IndexRequest(indexName);
        indexRequest.source(new String(cacheObject.getValueAsByteArr()), XContentType.JSON);
        indexRequest.id(buildIdFromCacheObject(cacheObject.getKey()));
        return indexRequest;
}

I tried

 private IndexRequest buildIndex(String indexName, CacheObject cacheObject) {
        IndexRequest indexRequest = new IndexRequest.Builder<CVDocument>()
                .index(indexName)
                .id(buildIdFromCacheObject(cacheObject.getKey()))
                .build();
        // **HOW TO SET SOURCE**
        //indexRequest.source(new String(cacheObject.getValueAsByteArr()), XContentType.JSON);
        return indexRequest;
    }

where my CVDocument is

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

@Data
public class CVDocument {
    @JsonProperty("_id")
    private Integer id;

    @JsonProperty("cvFR")
    private Integer cvFR;

    @JsonProperty("cvEN")
    private Integer cvEN;
}

you can set the source document using document() as a POJO and a JSON library is doing the serialization.

Would that work for you, or do you only have the byte array representation?

we have complex json in byte array representation

With the next release this will be possible. See Raw json support by swallez · Pull Request #200 · elastic/elasticsearch-java · GitHub

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