Skip to content

Impossible to set dynamic templates in mappings when creating an index template #8520

Open
@lucacremonesi

Description

@lucacremonesi

Elastic.Clients.Elasticsearch version:
8.18.0

Elasticsearch version:
8.18.0

.NET runtime version:
.NET Framework 4.8.9310.0

Operating system version:
Microsoft Windows 10.0.19045

Description of the problem including expected versus actual behavior:
When creating an index template with mappings that contain dynamic templates you will get the following server error:
Type: illegal_argument_exception Reason: "composable template [template_name] template after composition is invalid" CausedBy: "Type: illegal_argument_exception Reason: "invalid composite mappings for [template_name]" CausedBy: "Type: mapper_parsing_exception Reason: "Failed to parse mapping: A dynamic template must be defined with a name" CausedBy: "Type: mapper_parsing_exception Reason: "A dynamic template must be defined with a name""""

The client generates the request as follows:

{
    "index_patterns": "pattern*",
    "template": {
        "mappings": {
            "dynamic_templates": [
                {
                    "key": "copy_to_all_fields",
                    "value": {
                        "match_mapping_type": "string",
                        "unmatch": "all_fields",
                        "mapping": {
                            "copy_to": "all_fields",
                            "type": "text"
                        }
                    }
                }
            ],
            ...

Instead the client should generate the request as follows:

{
    "index_patterns": "pattern*",
    "template": {
        "mappings": {
            "dynamic_templates": [
                {
                    "copy_to_all_fields": {
                        "match_mapping_type": "string",
                        "unmatch": "all_fields",
                        "mapping": {
                            "copy_to": "all_fields",
                            "type": "text"
                        }
                    }
                }
            ],
            ...

Steps to reproduce:

  1. Define DynamicTemplates: Dictionary<string, DynamicTemplate> DynamicTemplates;
  2. Initialize DynamicTemplates:
var dynamicTemplate = DynamicTemplate.Mapping(new TextProperty() { CopyTo = "all_fields" });
dynamicTemplate.MatchMappingType = new[] { "string" };
dynamicTemplate.Unmatch = new[] { "all_fields" };

DynamicTemplates = new Dictionary<string, DynamicTemplate>
    {
        { "copy_to_all_fields", dynamicTemplate }
    };
  1. Start creating an index template with dynamic templates in mappings:
    client.Indices.PutIndexTemplateAsync("template_name", pitr => pitr
        .IndexPatterns(new[] { "pattern*" })
        .Template(itm => itm
            .Settings(s => s
                ...
            )
            .Mappings(tm => tm
                .DynamicTemplates(DynamicTemplates)
                ...
            )
        )
    )

Expected behavior
The index template should be successfully created with the correct dynamic templates in mappings.

Provide ConnectionSettings (if relevant):

Provide DebugInformation (if relevant):

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions