# Have field with variable type

**URL:** <https://discuss.elastic.co/t/have-field-with-variable-type/88426>\
**Category:** Elasticsearch\
**Created:** [June 6, 2017, 1:14pm UTC](https://discuss.elastic.co/t/have-field-with-variable-type/88426 "2017-06-06T13:14:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jacoby\_Thwaites](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jacoby_thwaites/32/18834_2.png) [@Jacoby\_Thwaites](https://discuss.elastic.co/u/Jacoby_Thwaites)\
**Post date:** [June 6, 2017, 1:14pm UTC](https://discuss.elastic.co/t/have-field-with-variable-type/88426/1 "2017-06-06T13:14:36Z")

</div>

I want to allow these two objects to be indexed:

```auto
{ "name": "foo", "value": 12}

{"name": "bar", "value": "twelve"}

```

where I want to store the object and index on name but not on value.

Is that possible?

---

<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:** [June 7, 2017, 10:17am UTC](https://discuss.elastic.co/t/have-field-with-variable-type/88426/2 "2017-06-07T10:17:58Z")

</div>

In the same index?

---

<div class="post-metadata">

**Author:** ![Jacoby\_Thwaites](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jacoby_thwaites/32/18834_2.png) [@Jacoby\_Thwaites](https://discuss.elastic.co/u/Jacoby_Thwaites)\
**Post date:** [June 7, 2017, 10:37am UTC](https://discuss.elastic.co/t/have-field-with-variable-type/88426/3 "2017-06-07T10:37:28Z")

</div>

Yes, same index.

The following incantation seems to work. If I index a document with a numeric 'value' property, it seems to retain its number type even though the mapping type is text.

```
{
    "settings": {
    },
    "mappings": {
        "_default_": {
            "dynamic_templates": [
                {
                    "value_is_any_type": {
                        "match": "value",
                        "mapping": {
                            "type": "text"
                        }
                    }
                },
                {
                    "timestamp": {
                        "match": "timestamp",
                        "mapping": {
                            "type": "date"
                        }
                    }
                }
            ]
        }
    }
}
```

---

<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:** [June 8, 2017, 9:11pm UTC](https://discuss.elastic.co/t/have-field-with-variable-type/88426/4 "2017-06-08T21:11:51Z")

</div>

> [@Jacoby\_Thwaites](#):
>
> The following incantation seems to work. If I index a document with a numeric 'value' property, it seems to retain its number type even though the mapping type is text.

It won't, it will convert it to text because that is the lowest common denominator.

---

<div class="post-metadata">

**Author:** ![Jacoby\_Thwaites](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jacoby_thwaites/32/18834_2.png) [@Jacoby\_Thwaites](https://discuss.elastic.co/u/Jacoby_Thwaites)\
**Post date:** [June 9, 2017, 8:21am UTC](https://discuss.elastic.co/t/have-field-with-variable-type/88426/5 "2017-06-09T08:21:11Z")

</div>

Inneresting. I was looking at the record which retains the integer. At least it actually stored the record (rather than rejecting it).

But as you rightly say the index doesn't see that property as integer.

I am avoiding the problem by adjusting my json prop names. The lesson is, I think, use a static type per (nested) prop name.

---

<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:** [July 7, 2017, 8:21am UTC](https://discuss.elastic.co/t/have-field-with-variable-type/88426/6 "2017-07-07T08:21:16Z")

</div>

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