How to build a JsonData object (JsonGenerator, etc.) to be used in new elasticsearch java api

Anybody here encountered RHLC XContentBuilder?

How is it in migrating to new ElasticSearch java API client?...

Particular implementation is when creating an index.
In RHLC, we use XContentBuilder to get information from object, and feed this XContentBuilder to a IndexRequest.

How is it done in the new ES java APi client?

Are any sample snippets out there just to guide me in the right direction...

Hi @ALX_DM .

I understand that you use xContentBuilder to create the index. Why with the new client you don't use a file instead of xContentBuilder?

@RabBit_BR

Yes, I dont use a file in this case.

I have an entity object, and from it, I will create a JsonData.

Im thinking of creating JsonData by:

      JsonpMapper jsonpMapper = esClient.jsonpMapper();
      JsonProvider jsonProvider = jsonpMapper.jsonProvider();

      StringWriter jsonObjectWriter = new StringWriter();
      try {
          JsonGenerator generator = new JacksonJsonpGenerator(factory.createGenerator(jsonObjectWriter));
          generator.writeStartObject();
          generator.write("name", "ALX DM");
          generator.write("path", "c:/JSON/SAMPLE");
          generator.writeEnd();      
      } catch (IOException e) {
         return null;
      }

      String jsonString = sw.toString();       // convert to json string
      Reader reader = new StringReader(jsonString ); // read the the string

      JsonData jdata = JsonData.from(jsonProvider.createParser(reader), jsonpMapper);  //parse

      final var request =
          co.elastic.clients.elasticsearch.core.IndexRequest.of(
              r -> r.index(getIndexName()).id(getAssetId(asset)).document(jdata ));

      esClient.index(request);

But as you can see, its kinda inefficient since you have to make a json string, then read it again, then parse it, then put in in IndexRequest

Im thinking if there is a way more efficient way of doing this.

@RabBit_BR

Another way I think is creating JsonData by the object.

Say:

@Builder
@Data
public class Product {
  private String name;
  private String path;
}

// from other class to create JsonData
:
:

public class IndexDocService {
:
:
   public void index(){

         Product testProduct = Product.builder()
                                                   .name("ALX DM")
                                                   .path("C:/ttest")
                                                   .build();

         JsonpMapper jsonpMapper = esClient.jsonpMapper();
        JsonData jData2 = JsonData.of(testProduct , jsonpMapper);

        final var request =
          co.elastic.clients.elasticsearch.core.IndexRequest.of(
              r -> r.index(getIndexName()).id(getAssetId(asset)).document(jdata ));

      esClient.index(request);
   }

}

This I think is ok as it minimizes the process of converting to json, read, parse...

But, maybe there`s another better way...

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