# How to map fields with dynamic keys

**URL:** <https://discuss.elastic.co/t/how-to-map-fields-with-dynamic-keys/76284>\
**Category:** Elasticsearch\
**Created:** [February 23, 2017, 6:21pm UTC](https://discuss.elastic.co/t/how-to-map-fields-with-dynamic-keys/76284 "2017-02-23T18:21:59Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![besson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/besson/32/15808_2.png) [@besson](https://discuss.elastic.co/u/besson)\
**Post date:** [February 23, 2017, 6:21pm UTC](https://discuss.elastic.co/t/how-to-map-fields-with-dynamic-keys/76284/1 "2017-02-23T18:21:59Z")

</div>

Hi guys, I need to add some dynamic fields into my documents but those fields have dynamic keys like this example:

[  
{  
"loc\_42": 0.5,  
"loc\_21": 0.7  
},  
{  
"loc\_42": 0.5,  
"loc\_15": 0.8,  
"loc\_18": 0.2,  
"loc\_2": 0.1  
},  
{  
"loc\_421": 0.5,  
"loc\_11": 0.8,  
"loc\_13": 0.2,  
"loc\_20": 0.1  
},  
....  
]

The nature of my keys is dynamic, it may include any id number. The problem of just adding these documents is to inflate my mapping schema with a very large number of different keys. I tried to use dynamic templates but I had the same problem.

Is possible to add those fields to my documents, keep them searchable but without having them on mappings?

Thank you!

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [February 24, 2017, 2:07am UTC](https://discuss.elastic.co/t/how-to-map-fields-with-dynamic-keys/76284/2 "2017-02-24T02:07:08Z")

</div>

You can use trailing wildcards in templates, so `"loc_*": "float"` or similar.

---

<div class="post-metadata">

**Author:** ![besson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/besson/32/15808_2.png) [@besson](https://discuss.elastic.co/u/besson)\
**Post date:** [February 26, 2017, 8:28am UTC](https://discuss.elastic.co/t/how-to-map-fields-with-dynamic-keys/76284/3 "2017-02-26T08:28:02Z")

</div>

Hey @warkolm, thank you for the reply. I tried to do it before, but with dynamic templates , I still have all the keys in mapping schema. For the dynamic template you suggested, after indexing my documents, I got it:

```
  GET my_index/_mappings

  {
  "my_index": {
    "mappings": {
      "my_type": {
        "dynamic_templates": [
          {
            "dynamic_keys": {
              "mapping": {
                "type": "float"
              },
              "match": "loc_*"
            }
          }
        ],
        "properties": {
          "loc_11": {
            "type": "float"
          },
          "loc_13": {
            "type": "float"
          },
          "loc_15": {
            "type": "float"
          },
          "loc_18": {
            "type": "float"
          },
          "loc_2": {
            "type": "float"
          },
          "loc_20": {
            "type": "float"
          },

```

I would like to avoid that because the number of dynamic keys I may generate in my real scenario is very large.

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [February 26, 2017, 8:32am UTC](https://discuss.elastic.co/t/how-to-map-fields-with-dynamic-keys/76284/4 "2017-02-26T08:32:57Z")

</div>

What are you after then?

---

<div class="post-metadata">

**Author:** ![besson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/besson/32/15808_2.png) [@besson](https://discuss.elastic.co/u/besson)\
**Post date:** [February 26, 2017, 8:50am UTC](https://discuss.elastic.co/t/how-to-map-fields-with-dynamic-keys/76284/5 "2017-02-26T08:50:08Z")

</div>

Indexing all this data on Elasticsearch but without flooding my mapping schema with a lot of keys/fields. In Solr, for instance, I could create a dynamic field loc\_\*: float without having each possible key value in my schema. I would like to have something like this.

I have total control over the indexing, so I can represent keys differently if necessary.

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [February 26, 2017, 8:51am UTC](https://discuss.elastic.co/t/how-to-map-fields-with-dynamic-keys/76284/6 "2017-02-26T08:51:03Z")

</div>

So you want to merge them all into one field name?

---

<div class="post-metadata">

**Author:** ![besson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/besson/32/15808_2.png) [@besson](https://discuss.elastic.co/u/besson)\
**Post date:** [February 26, 2017, 9:03am UTC](https://discuss.elastic.co/t/how-to-map-fields-with-dynamic-keys/76284/7 "2017-02-26T09:03:21Z")

</div>

It could be. What I need is to have these 'dynamic fields' in my documents, to boost by their values in queries or to filter by these keys. However, I want to have them mapped to only one field (or one field pattern) in mappings.

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [February 26, 2017, 9:04am UTC](https://discuss.elastic.co/t/how-to-map-fields-with-dynamic-keys/76284/8 "2017-02-26T09:04:54Z")

</div>

Ahh ok.

Well you can set one definition in the template, but ES will still keep all the individual fields in the mappings irrespectively. You cannot change this.

---

<div class="post-metadata">

**Author:** ![besson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/besson/32/15808_2.png) [@besson](https://discuss.elastic.co/u/besson)\
**Post date:** [February 26, 2017, 9:17am UTC](https://discuss.elastic.co/t/how-to-map-fields-with-dynamic-keys/76284/9 "2017-02-26T09:17:57Z")

</div>

Ok, thank you very much for your help.

---

<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:** [March 26, 2017, 9:18am UTC](https://discuss.elastic.co/t/how-to-map-fields-with-dynamic-keys/76284/10 "2017-03-26T09:18:01Z")

</div>

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