# How to dynamic template a mapping for a dictionary object with strict\_allow\_templates on OpenSearch

**URL:** <https://forum.opensearch.org/t/how-to-dynamic-template-a-mapping-for-a-dictionary-object-with-strict-allow-templates-on-opensearch/23038>\
**Category:** OpenSearch\
**Created:** [January 8, 2025, 12:54pm UTC](https://forum.opensearch.org/t/how-to-dynamic-template-a-mapping-for-a-dictionary-object-with-strict-allow-templates-on-opensearch/23038 "2025-01-08T12:54:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sousu](https://avatars.discourse-cdn.com/v4/letter/s/e95f7d/32.png) [@sousu](https://forum.opensearch.org/u/sousu)\
**Post date:** [January 8, 2025, 12:54pm UTC](https://forum.opensearch.org/t/how-to-dynamic-template-a-mapping-for-a-dictionary-object-with-strict-allow-templates-on-opensearch/23038/1 "2025-01-08T12:54:10Z")

</div>

I’m creating an index that should be strict\_allow\_templates to avoid introduction of errors on the mapping, one of the fields should be an object representing a dictionary with IDs as key with the following json representation as example:

```json
"dict": {
    "146": "lorem ipsum dolum",
    "23297030": "",
    "20454962": "ipsum dolor sit amet"
}

```

The dictionary may have any number of “long” type keys with string values.

With this objective in mind I wrote the following dynamic template, using path\_match (that I understood to be the tool for this kind of inner object scenario):

```javascript
{
    fields: {
        mapping: {
            type: 'text'
        },
        match_mapping_type: 'string',
        path_match: 'dict.\\d{1,19}$',
    }
}

```

but when trying to index it I get an error:

```auto
strict_dynamic_mapping_exception: [strict_dynamic_mapping_exception] Reason: mapping set to strict_allow_templates, dynamic introduction of [dict] within [_doc] is not allowed

```

I fear that introducing the object on the mapping properties will open the possibility for malformed objects on the document.

What would be the correct way to build a dynamic template for accepting objects with dynamic fields that follow a naming pattern?

---

<div class="post-metadata">

**Author:** ![sousu](https://avatars.discourse-cdn.com/v4/letter/s/e95f7d/32.png) [@sousu](https://forum.opensearch.org/u/sousu)\
**Post date:** [January 8, 2025, 12:59pm UTC](https://forum.opensearch.org/t/how-to-dynamic-template-a-mapping-for-a-dictionary-object-with-strict-allow-templates-on-opensearch/23038/2 "2025-01-08T12:59:15Z")

</div>

I spotted an issue that did not solve the problem but is related, the dynamic template was missing the field `match_pattern` setting the pattern to regex, the correct version follows:

```javascript
fields: {
    mapping: {
        type: 'text'
    },
    match_mapping_type: 'string',
    match_pattern: 'regex',
    path_match: 'dict.*',
    match: '^\\d{1,19}$'
}

```

in addition to this dynamic template correction I needed to introduce the following to my mapping.properties:

```js
dict: { type: 'object' },

```

in my tests this accepts the digit fields and reject non-digit ones, solving the problem, but also accept empty dict, which is not ideal.
