# Get key field mapping of flatten field

**URL:** <https://discuss.elastic.co/t/get-key-field-mapping-of-flatten-field/287783>\
**Category:** Elasticsearch\
**Created:** [October 27, 2021, 10:01am UTC](https://discuss.elastic.co/t/get-key-field-mapping-of-flatten-field/287783 "2021-10-27T10:01:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ewolfman](https://avatars.discourse-cdn.com/v4/letter/e/5f8ce5/32.png) [@ewolfman](https://discuss.elastic.co/u/ewolfman)\
**Post date:** [October 27, 2021, 10:01am UTC](https://discuss.elastic.co/t/get-key-field-mapping-of-flatten-field/287783/1 "2021-10-27T10:01:20Z")

</div>

Hi,

I have a use case for storing a map of key-value items. The keys are not likely to vary from one document to another, but I do not know them in advance. Therefore I am using [dynamic\_templates](https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-templates.html) in order to index them. It works well and also allows me to fetch the dynamic field names using the [field](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-field-mapping.html) API.

However I have been reading about 'mapping explosions' and I thought to try a different approach. The alternatives I was considering are 'nested' types and 'flattened' type. While '[nested](https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html)' works, the doc recommends considering using the flattened type instead for key-value pairs.

Looking into using the flattened type, it seems to serve its purpose. But I could not find a way to get the actual keys that were indexed.

For example:

```auto
PUT test
{
  "mappings": {
      "dynamic_templates": [
        {
          "custom_dynamic": {
            "path_match": "custom_dynamic.*",
            "mapping": {
              "type": "keyword"
            }
          }
        }
      ],
    "properties": {
      "custom_flat": {
        "type": "flattened"
      }
    }
  }
}

PUT test/_doc/111
{
  "custom_flat": {
    "custom1": "some value",
    "custom2": "other value"
  },
  "custom_dynamic": {
    "custom1": "some value",
    "custom2": "other value"
  }
}

GET test/_mapping/field/custom_dynamic.* // this will fetch the fields 'custom1' and 'custom2'
GET test/_mapping/field/custom_flat.* // this fetches nothing

```

Is there a way to fetch the keys (e.g. 'custom1', 'custom2') when using the flattened type?

---

<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:** [November 24, 2021, 10:01am UTC](https://discuss.elastic.co/t/get-key-field-mapping-of-flatten-field/287783/2 "2021-11-24T10:01:24Z")

</div>

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