# Dynamic template in very dynamic document

**URL:** <https://discuss.elastic.co/t/dynamic-template-in-very-dynamic-document/234392>\
**Category:** Elasticsearch\
**Created:** [May 26, 2020, 5:56pm UTC](https://discuss.elastic.co/t/dynamic-template-in-very-dynamic-document/234392 "2020-05-26T17:56:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![psteh](https://avatars.discourse-cdn.com/v4/letter/p/f05b48/32.png) [@psteh](https://discuss.elastic.co/u/psteh)\
**Post date:** [May 26, 2020, 5:56pm UTC](https://discuss.elastic.co/t/dynamic-template-in-very-dynamic-document/234392/1 "2020-05-26T17:56:28Z")

</div>

I have SQL queries that would be indexed into ElasticSearch.  
The problem I encounter is that the fields in documents are very dynamic, **it could be text, object, integer, etc**.

Sample of body (Note that this is not the same for others)  
As you see, the values on key 0, 1 ,2 is text/integer but this is not the case for another document.  
**How do I force it to be text? For example, on indexing time, convert the value of key `1` to `text`**

```auto
"bindings": {
    "0": "41615",
    "1": 42,
    "2": "2"
}

```

Created template with `Dynamic Template`

```auto
PUT _template/cwl_template
{
  "order": 0,
  "index_patterns": ["cwl*"],
  "mappings": {
    "dynamic_templates": [
      {
        "bindings": {
          "path_match": "bindings.*",
          "match_pattern": "regex",
          "match": "^.*$",
          "mapping": {
            "type": "text",
            "fields": {
              "keyword": {
                "ignore_above": 256,
                "type": "keyword"
              }
            }
          }
        }
      }
    ]
  }
}

```

Receiving error on logstash

```auto
Could not index event to Elasticsearch. {:status=>400, :action=>["index", {:_id=>nil, :_index=>"cwl-2020.05.22", :_type=>"_doc", :routing=>nil}, #<LogStash::Event:0x646f085a>], :response=>{"index"=>{"_index"=>"cwl-2020.05.22", "_type"=>"_doc", "_id"=>"zG0mUnIBuw9XhmjOhULr", "status"=>400, "error"=>{"type"=>"illegal_argument_exception", "reason"=>"mapper [properties.bindings.5] of different type, current_type [text], merged_type [long]"}}}}

```

---

<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:** [May 26, 2020, 10:49pm UTC](https://discuss.elastic.co/t/dynamic-template-in-very-dynamic-document/234392/2 "2020-05-26T22:49:56Z")

</div>

My regex is not the best, and I may be misreading the error, but don't you want to `match_pattern` on `properties.bindings.*`?

---

<div class="post-metadata">

**Author:** ![psteh](https://avatars.discourse-cdn.com/v4/letter/p/f05b48/32.png) [@psteh](https://discuss.elastic.co/u/psteh)\
**Post date:** [May 27, 2020, 3:09am UTC](https://discuss.elastic.co/t/dynamic-template-in-very-dynamic-document/234392/3 "2020-05-27T03:09:49Z")

</div>

You're right, I need more sleep.  
Been days trying to get data into ES due to the very dynamic documents structure

---

<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:** [May 27, 2020, 10:03am UTC](https://discuss.elastic.co/t/dynamic-template-in-very-dynamic-document/234392/4 "2020-05-27T10:03:54Z")

</div>

Happens to all of us 🙂

---

<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:** [June 24, 2020, 10:09am UTC](https://discuss.elastic.co/t/dynamic-template-in-very-dynamic-document/234392/5 "2020-06-24T10:09:01Z")

</div>

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