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

**URL:** <https://discuss.elastic.co/t/how-to-dynamically-add-fields-to-index-using-the-java-client-api/297342>\
**Category:** Elasticsearch\
**Created:** [February 16, 2022, 9:13am UTC](https://discuss.elastic.co/t/how-to-dynamically-add-fields-to-index-using-the-java-client-api/297342 "2022-02-16T09:13:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vmassol](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vmassol/32/17398_2.png) [@vmassol](https://discuss.elastic.co/u/vmassol)\
**Post date:** [February 16, 2022, 9:13am UTC](https://discuss.elastic.co/t/how-to-dynamically-add-fields-to-index-using-the-java-client-api/297342/1 "2022-02-16T09:13:27Z")

</div>

Hi,

I'd like to index some data in a modular way using the [new Java Client API](https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/7.16/introduction.html) 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:

```auto
            Ping ping = new Ping();
            provideData(ping);
            IndexResponse response = client.index(builder -> builder
                .index(ElasticsearchClientManager.INDEX)
                .document(ping)
            );

```

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

```auto
            Index index = new Index.Builder(constructIndexJSON())
                .index(JestClientManager.INDEX)
                .type(JestClientManager.TYPE)
                .build();
            JestResult result = client.execute(index);
...
    private String constructIndexJSON()
    {
        Map<String, Object> jsonMap = new HashMap<>();
        for (PingDataProvider pingDataProvider : this.pingDataProviderProvider.get()) {
            jsonMap.putAll(pingDataProvider.provideData());
        }
        return JSONObject.fromObject(jsonMap).toString();
    }

```

Any idea?

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![vmassol](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vmassol/32/17398_2.png) [@vmassol](https://discuss.elastic.co/u/vmassol)\
**Post date:** [February 22, 2022, 12:20pm UTC](https://discuss.elastic.co/t/how-to-dynamically-add-fields-to-index-using-the-java-client-api/297342/2 "2022-02-22T12:20:14Z")

</div>

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?

Thx

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 22, 2022, 1:08pm UTC](https://discuss.elastic.co/t/how-to-dynamically-add-fields-to-index-using-the-java-client-api/297342/3 "2022-02-22T13:08:48Z")

</div>

@swallez might know...

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 22, 2022, 1:09pm UTC](https://discuss.elastic.co/t/how-to-dynamically-add-fields-to-index-using-the-java-client-api/297342/4 "2022-03-22T13:09:04Z")

</div>

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