How to dynamically add fields to index using the Java Client API?


I'd like to index some data in a modular way using the new Java Client API and I don't see how to achieve this by passing a Java object. The issue is that this object needs to contain all fields and that doesn't make it modular. I'd like that several modules/component contribute fields dynamically.

This is what I have ATM:

            Ping ping = new Ping();
            IndexResponse response = client.index(builder -> builder

In the past, I was writing the following where each module was contributing some JSON part:

            Index index = new Index.Builder(constructIndexJSON())
            JestResult result = client.execute(index);
    private String constructIndexJSON()
        Map<String, Object> jsonMap = new HashMap<>();
        for (PingDataProvider pingDataProvider : this.pingDataProviderProvider.get()) {
        return JSONObject.fromObject(jsonMap).toString();

Any idea?

Thanks a lot!

Maybe to make my question more clear, the Ping class has to contain all the fields for the index creation. I'd like that this Ping class definition be somehow constructed dynamically by different pieces of code (in different maven modules). Or that the indexing be done on several such Java objects instead than one. Is there some kind of builder API that I could use?


@swallez might know...

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