# ElasticSearch Java API Client - Create index with type mapping

**URL:** <https://discuss.elastic.co/t/elasticsearch-java-api-client-create-index-with-type-mapping/363736>\
**Category:** Elasticsearch\
**Created:** [July 24, 2024, 4:57pm UTC](https://discuss.elastic.co/t/elasticsearch-java-api-client-create-index-with-type-mapping/363736 "2024-07-24T16:57:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sweller](https://avatars.discourse-cdn.com/v4/letter/s/aeb1de/32.png) [@sweller](https://discuss.elastic.co/u/sweller)\
**Post date:** [July 24, 2024, 4:57pm UTC](https://discuss.elastic.co/t/elasticsearch-java-api-client-create-index-with-type-mapping/363736/1 "2024-07-24T16:57:44Z")

</div>

I am migrating some code which uses the old rest high level client to the Java API Client (Starting at 7.17.22 with plans to migrate to 8.X immediately after), and am trying to recreate the mapping for a CreateIndexRequest that was originally built with the now removed XContentBuilder class

My old code:

```auto
XContentBuilder mapping = null;
            mapping = jsonBuilder()
                    .startObject()
                        .startObject("settings").value(esIndexSettings).endObject()
                        .startObject("mappings")
                            .startObject(DEFAULT_TYPE).field("date_detection", false)
                                .startObject("properties")
                                    .startObject("action").field("type", "text").endObject()
                                    .startObject("deltas")
                                        .startObject("properties")
                                            .startObject("current").field("type", "text").endObject()
                                            .startObject("previous").field("type", "text").endObject()
                                            .startObject("propertyName").field("type", "text").endObject()
                                        .endObject()
                                    .endObject()
                                    .startObject("entityId").field("type", "long").endObject()
                                    .startObject("entityType").field("type", "text").endObject()
                                    .startObject("parentId").field("type", "long").endObject()
                                    .startObject("parentLevel").field("type", "text").endObject()
                                    .startObject("timestamp").field("type", "date").endObject()
                                    .startObject("type").field("type", "text").endObject()
                                    .startObject("userId").field("type", "long").endObject()
                                    .startObject("userName").field("type", "text").endObject()
                                    .startObject("userLogin").field("type", "text").endObject()
                                .endObject()
                            .endObject()
                        .endObject()
                    .endObject();

```

My current attempt:

```auto
CreateIndexRequest request = new CreateIndexRequest.Builder()
                        .index(indexName)
                        .mappings(new TypeMapping.Builder()
                                .dateDetection(false)
                                .properties(Map.of(
                                        // I get stuck here. Not sure how to properly configure fields
                                        "action", new Property.Builder()
                                                .text(new TextProperty.Builder().build())
                                                .build(),
                                        "deltas", new AggregateMetricDoubleProperty.Builder().build()._toProperty()
                                ))
                                .build())
                        .settings(new IndexSettings.Builder()
                                .build())
                        .build();

```

---

<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:** [July 24, 2024, 9:14pm UTC](https://discuss.elastic.co/t/elasticsearch-java-api-client-create-index-with-type-mapping/363736/2 "2024-07-24T21:14:35Z")

</div>

Welcome!

Have a look at this [blog post](https://www.elastic.co/blog/switching-from-the-java-high-level-rest-client-to-the-new-java-api-client) which describes a bit how to use the new client and some tips on migrating from the old one.

Also [this repository](https://github.com/dadoonet/elasticsearch-java-client-demo) contains lot of tests demoing the Java client in action.

For example:

> <https://github.com/dadoonet/elasticsearch-java-client-demo/blob/49011c27dbbb5b989441df3c45a3ff9cf1200a6e/src/test/java/fr/pilato/test/elasticsearch/hlclient/EsClientIT.java#L576>
