Error creating index with mappings and dynamic_templates using java ElasticsearchClient 8.5.3el

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

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

My mappings with dynamic_templates are the following:

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:

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

Then I received the following Exception:

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:

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:

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:

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

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