# Dynamic field type

**URL:** <https://discuss.elastic.co/t/dynamic-field-type/15418>\
**Category:** Elasticsearch\
**Created:** [January 25, 2014, 5:55pm UTC](https://discuss.elastic.co/t/dynamic-field-type/15418 "2014-01-25T17:55:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Herbert\_Bodner](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@Herbert\_Bodner](https://discuss.elastic.co/u/Herbert_Bodner)\
**Post date:** [January 25, 2014, 5:55pm UTC](https://discuss.elastic.co/t/dynamic-field-type/15418/1 "2014-01-25T17:55:09Z")

</div>

Hi,

I have the following (simplified) structure:

{  
"id": "571344ad-c234-4144-92c2-94efa991240c",  
"name": "MyDocument.pdf",  
"metaDataValueLst": [  
{  
"metaDataFieldID": "19a2c920-013c-4a9f-91c7-d1a16a168779",  
"metaDataFieldName": "BillNr",  
"metaDataValue": 5514  
},  
{  
"metaDataFieldID": "83d65910-70d5-4a97-9bfb-b21a6270e55c",  
"metaDataFieldName": "BillDate",  
"metaDataValue": "2012-08-30T22:49:00.927",  
},  
{  
"metaDataFieldID": "bdf56521-cd9c-47f5-a8c7-bc344c57e1f7",  
"metaDataFieldName": "Description",  
"metaDataValue": "The description"  
}  
]  
}

As you can see, the field "metaDataValueLst.metaDataValue" can contain  
values of different types (numeric, datetime or string).  
I could just use the type "string" for the metaDataValue field, but then I  
would not be able to apply range-searches...  
Is it possible to create a corresponding mapping in a way, that the value  
is indexed according to its specific type?

I think creating a Multifield with 3 fields (metaDataValue\_string,  
metaDataValue\_numeric, metaDataValue\_date) would solve the problem, but  
this would increase the search complexity and require additional space.  
Any better suggestions?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/5379f52c-62b7-4a6c-b025-06b8a176e14a%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5379f52c-62b7-4a6c-b025-06b8a176e14a%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [January 27, 2014, 11:28am UTC](https://discuss.elastic.co/t/dynamic-field-type/15418/2 "2014-01-27T11:28:46Z")

</div>

Hey,

Multifields are not the way to go here, as it basically indexes one field  
(like metaDataValue) into several fields in your indices.

depending on your type of search, you just need to configure your mapping  
appropriately or you may need nested documents. The simple solution is to  
make use of the object type in your mapping

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

This basically works, but as soon as you need to query for  
metaDataValueLst.id and metaDataValueLst.value in one query, you will need  
nested documents to make this work, as by default all field values form the  
nested objects are flattened into one array before being indexed. (Hope  
this makes sense with that short explanation).

Hope this helps as a start.

--Alex

On Sat, Jan 25, 2014 at 6:55 PM, Herbert Bodner [herbert.bodner@gmail.com](mailto:herbert.bodner@gmail.com)wrote:

> Hi,
> 
> I have the following (simplified) structure:
> 
> {  
> "id": "571344ad-c234-4144-92c2-94efa991240c",  
> "name": "MyDocument.pdf",  
> "metaDataValueLst": [  
> {  
> "metaDataFieldID": "19a2c920-013c-4a9f-91c7-d1a16a168779",  
> "metaDataFieldName": "BillNr",  
> "metaDataValue": 5514  
> },  
> {  
> "metaDataFieldID": "83d65910-70d5-4a97-9bfb-b21a6270e55c",  
> "metaDataFieldName": "BillDate",  
> "metaDataValue": "2012-08-30T22:49:00.927",  
> },  
> {  
> "metaDataFieldID": "bdf56521-cd9c-47f5-a8c7-bc344c57e1f7",  
> "metaDataFieldName": "Description",  
> "metaDataValue": "The description"  
> }  
> ]  
> }
> 
> As you can see, the field "metaDataValueLst.metaDataValue" can contain  
> values of different types (numeric, datetime or string).  
> I could just use the type "string" for the metaDataValue field, but then  
> I would not be able to apply range-searches...  
> Is it possible to create a corresponding mapping in a way, that the value  
> is indexed according to its specific type?
> 
> I think creating a Multifield with 3 fields (metaDataValue\_string,  
> metaDataValue\_numeric, metaDataValue\_date) would solve the problem, but  
> this would increase the search complexity and require additional space.  
> Any better suggestions?
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/5379f52c-62b7-4a6c-b025-06b8a176e14a%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5379f52c-62b7-4a6c-b025-06b8a176e14a%40googlegroups.com)  
> .  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAGCwEM98ceBtvrQGFYupC%2BvYyYhBRwrJVpeFGb7%2B46QRgxNyhA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAGCwEM98ceBtvrQGFYupC%2BvYyYhBRwrJVpeFGb7%2B46QRgxNyhA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [January 27, 2014, 11:46am UTC](https://discuss.elastic.co/t/dynamic-field-type/15418/3 "2014-01-27T11:46:11Z")

</div>

Concise version, single mapping:

\_index "test"  
\_type "bills"  
\_id "571344ad-c234-4144-92c2-94efa991240c"  
{  
"name": "MyDocument.pdf",  
"relations" : [  
{  
"id" : "19a2c920-013c-4a9f-91c7-d1a16a168779",  
"BillNr": 5514  
},  
{  
"id" : "83d65910-70d5-4a97-9bfb-b21a6270e55c",  
"BillDate": "2012-08-30T22:49:00.927"  
},  
{  
"id" : "bdf56521-cd9c-47f5-a8c7-bc344c57e1f7",  
"Description":"The description"  
}  
]  
}

With multiple mappings:

\_index "test"  
\_type "docs"  
\_id "571344ad-c234-4144-92c2-94efa991240c"  
{  
"name": "MyDocument.pdf",  
"relations" : [  
"19a2c920-013c-4a9f-91c7-d1a16a168779",  
"83d65910-70d5-4a97-9bfb-b21a6270e55c",  
"bdf56521-cd9c-47f5-a8c7-bc344c57e1f7"  
]  
}

\_index "test"  
\_type" "billnumbers"  
\_id "19a2c920-013c-4a9f-91c7-d1a16a168779"  
{  
"BillNr": 5514  
}

\_index "test"  
\_type "billdates"  
\_id "83d65910-70d5-4a97-9bfb-b21a6270e55c"  
{  
"BillDate": "2012-08-30T22:49:00.927"  
}

\_index "test"  
\_type "descriptions"  
\_id "bdf56521-cd9c-47f5-a8c7-bc344c57e1f7"  
{  
"Description":"The description"  
}

Using many documents, you also have the option for parent/child. It depends  
how complex the children docs are. Only one field, as shown here, is a bad  
example.

Jörg

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAKdsXoFoXY%3D4Kg%2Bi9w8cS3PH8Lj9HvtViXjwZ%2BSg5e53AiEzTQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAKdsXoFoXY%3D4Kg%2Bi9w8cS3PH8Lj9HvtViXjwZ%2BSg5e53AiEzTQ%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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 6, 2017, 1:54am UTC](https://discuss.elastic.co/t/dynamic-field-type/15418/4 "2017-07-06T01:54:37Z")

</div>


