# Restrict dynamic template path\_match to 1 level

**URL:** <https://discuss.elastic.co/t/restrict-dynamic-template-path-match-to-1-level/195426>\
**Category:** Elasticsearch\
**Created:** [August 16, 2019, 4:49am UTC](https://discuss.elastic.co/t/restrict-dynamic-template-path-match-to-1-level/195426 "2019-08-16T04:49:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Welsh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/welsh/32/52405_2.png) [@Welsh](https://discuss.elastic.co/u/Welsh)\
**Post date:** [August 16, 2019, 4:49am UTC](https://discuss.elastic.co/t/restrict-dynamic-template-path-match-to-1-level/195426/1 "2019-08-16T04:49:10Z")

</div>

Hey,

I'm trying to use a common mapping template to set a field to a nested type but to not set fields under it to also nested. The trouble I'm running into is the dynamic mapping template I'm using is setting the field, and all sub fields as nested. (Which makes sense based off how I'm doing it and the documentation)

The template I'm trying to use is:

```
POST _template/entity_record_template
{
  "index_patterns": [
    "entity_*"
  ],
  "mappings": {
    "_doc": {
      "dynamic_templates": [
        {
          "entity_contents": {
            "path_match": "entityContent.*",
            "mapping": {
              "type": "nested",
              "dynamic": "true"
            }
          }
        }
      ],
      "properties": {
        "entityDestination": {
          "type": "keyword"
        },
        "entityType": {
          "type": "keyword"
        },
        "entityContent": {
          "type": "object"
        }
      }
    }
  }
}

```

And then adding an object:

```
POST /entity_a/_doc
{
  "entityDestination": "all",
  "entityType": "a",
  "entityContent": {
    "entityA": [
      {
        "valOne": "A",
        "valTwo": "B"
      },
      {
        "valOne": "B",
        "valTwo": "A"
      }
    ]
  }
}

```

However it fails because:

```
{
  "error": {
    "root_cause": [
      {
        "type": "mapper_parsing_exception",
        "reason": "object mapping for [entityContent.entityA.valOne] tried to parse field [valOne] as object, but found a concrete value"
      }
    ],
    "type": "mapper_parsing_exception",
    "reason": "object mapping for [entityContent.entityA.valOne] tried to parse field [valOne] as object, but found a concrete value"
  },
  "status": 400
}

```

Which I understand makes sense so I could remove the **dynamic\_templates** section however then when I'm trying to query like so:

```
POST /entity_a/_search
{
  "_source" : false,
  "query": {
    "nested": {
      "path": "entityContent.entityA",
      "query": {
        "bool": {
          "must": [
            { "match": { "entityContent.entityA.valOne": "A" }}
          ]
        }
      },
      "inner_hits" : {}
    }
  }
}

```

It then fails because the **entityA** isn't set to be nested, now I could add into the **entityContent** to implicity set **entityA** however I was trying to make this generic so if I get **entityB** then no update to the template would be required.

Is there a way to say for the **path\_match** to be restricted to the first level? So **entityContent.entityA** is matched but **entityContent.entityA.\*** is not? (The names also don't follow the pattern of **entityX** so I couldnt do **entityContent.entity\*** )

Any suggestions, alternatives or recommendations would be most welcomed.

---

<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:** [September 13, 2019, 4:49am UTC](https://discuss.elastic.co/t/restrict-dynamic-template-path-match-to-1-level/195426/2 "2019-09-13T04:49:17Z")

</div>

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