# Error creating index with mappings and dynamic\_templates using java ElasticsearchClient 8.5.3el

**URL:** <https://discuss.elastic.co/t/error-creating-index-with-mappings-and-dynamic-templates-using-java-elasticsearchclient-8-5-3el/321785>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [December 21, 2022, 4:15pm UTC](https://discuss.elastic.co/t/error-creating-index-with-mappings-and-dynamic-templates-using-java-elasticsearchclient-8-5-3el/321785 "2022-12-21T16:15:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![massimiliano.ruffolo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/massimiliano.ruffolo/32/115034_2.png) [@massimiliano.ruffolo](https://discuss.elastic.co/u/massimiliano.ruffolo)\
**Post date:** [December 21, 2022, 4:15pm UTC](https://discuss.elastic.co/t/error-creating-index-with-mappings-and-dynamic-templates-using-java-elasticsearchclient-8-5-3el/321785/1 "2022-12-21T16:15:06Z")

</div>

Hi all, i tried to create an index with mappings and dynamic\_templates using Java ElasticsearchClient.  
My maven-dependency is the following:

```auto
		<dependency>
			<groupId>co.elastic.clients</groupId>
			<artifactId>elasticsearch-java</artifactId>
			<version>8.5.3</version>
		</dependency>

```

My mappings with dynamic\_templates are the following:

```auto
final Map<String, DynamicTemplate> dynamic = new HashMap<>();
dynamic.put("telemetries_value", DynamicTemplate.of(tel -> tel
        .match(VALUE).mapping(value -> value.double_(d -> d))
));
dynamic.put("diagnostics_value", DynamicTemplate.of(diag -> diag
        .match(VALUE).mapping(value -> value.keyword(k -> k))
));
dynamic.put("metadata_value", DynamicTemplate.of(meta -> meta
        .match(VALUE).mapping(value -> value.double_(d -> d))
));

final TypeMapping mappings = TypeMapping.of(map -> map
        .properties(GATEWAY_ID, gwt -> gwt.keyword(k -> k))
        .properties(DEVICE_ID, dev -> dev.keyword(k -> k))
        .properties(TIMESTAMP, time -> time.date(d -> d))
        .properties(POSITION, pos -> pos.object(o -> o
                .properties(LATITUDE, lat -> lat.double_(d -> d))
                .properties(LONGITUDE, lon -> lon.double_(d -> d))
                .properties(ALTITUDE, alt -> alt.double_(d -> d))
        ))
        .dynamicTemplates(Arrays.asList(dynamic))
);

```

I tried to create an index using the following code:

```auto
final ElasticsearchClient client = newRestClient();
final boolean acknowledged = client.indices().create(c -> c.index(indexName).mappings(mapping)).acknowledged();

```

Then I received the following Exception:

```auto
Caused by: co.elastic.clients.elasticsearch._types.ElasticsearchException: [es/indices.create] failed: [mapper_parsing_exception] Failed to parse mapping: A dynamic template must be defined with a name

```

The problem is TypeMapping bad serialization. In the following what i found in debug:

```auto
TypeMapping: {
    "dynamic_templates": [
        {
            "telemetries_value": {
                "mapping": {
                    "type": "double"
                },
                "match": "value"
            },
            "diagnostics_value": {
                "mapping": {
                    "type": "keyword"
                },
                "match": "value"
            },
            "metadata_value": {
                "mapping": {
                    "type": "double"
                },
                "match": "value"
            }
        }
    ],
    "properties": {
        "position": {
            "type": "object",
            "properties": {
                "altitude": {
                    "type": "double"
                },
                "latitude": {
                    "type": "double"
                },
                "longitude": {
                    "type": "double"
                }
            }
        },
        "deviceId": {
            "type": "keyword"
        },
        "gatewayId": {
            "type": "keyword"
        },
        "timestamp": {
            "type": "date"
        }
    }
}

```

This serialization is performed in the following method:

```auto
co.elastic.clients.elasticsearch._types.mapping.TypeMapping.serializeInternal(JsonGenerator, JsonpMapper)

```

I tried to fix the error overriding the previous method. In the following there is the piece of code that correct the bug:

```auto
if (ApiTypeHelper.isDefined(this.dynamicTemplates)) {
    generator.writeKey("dynamic_templates");
    generator.writeStartArray();
    for (Map<String, DynamicTemplate> item0 : this.dynamicTemplates) {
        //generator.writeStartObject();FIXME --
        if (item0 != null) {
            for (Map.Entry<String, DynamicTemplate> item1 : item0.entrySet()) {
                generator.writeStartObject();//FIXME ++
                generator.writeKey(item1.getKey());
                item1.getValue().serialize(generator, mapper);
                generator.writeEnd();//FIXME ++
            }
        }
        //generator.writeEnd();FIXME --

    }
    generator.writeEnd();

}

```

Please, can you fix the problem: using my solution or other best strategy?

Thanks for reply  
Thanks for reply

---

<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:** [January 18, 2023, 4:15pm UTC](https://discuss.elastic.co/t/error-creating-index-with-mappings-and-dynamic-templates-using-java-elasticsearchclient-8-5-3el/321785/2 "2023-01-18T16:15:28Z")

</div>

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