# Searching and sorting dynamic fields

**URL:** <https://discuss.elastic.co/t/searching-and-sorting-dynamic-fields/44134>\
**Category:** Elasticsearch\
**Created:** [March 11, 2016, 10:14am UTC](https://discuss.elastic.co/t/searching-and-sorting-dynamic-fields/44134 "2016-03-11T10:14:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![BennoSI](https://avatars.discourse-cdn.com/v4/letter/b/67e7ee/32.png) [@BennoSI](https://discuss.elastic.co/u/BennoSI)\
**Post date:** [March 11, 2016, 10:14am UTC](https://discuss.elastic.co/t/searching-and-sorting-dynamic-fields/44134/1 "2016-03-11T10:14:45Z")

</div>

Hi,  
I'm new to elasticsearch.  
At the moment I'm checking whether elasticsearch does fulfill our requirements for searching and sorting on quiet dynamic (broad schema) data. One requirement is, that we need to index arbitrary keys and values. This means, a user can define for example a field called "temperature" and set it to an int value, or he can define a field called "description" and set it to a string value. The first approach I was thinking of was to save the data in the following form:  
{  
"temperature/num": 29,  
"description/str": "something"  
}

(the suffixes are there because in another document "desctription" could be an integer..)

The problem with this approach is, that the schema can grow very fast which can get a real problem as I read.

Another approach would be the following:  
{  
"attributes": [  
{ "k": "temperature", "v/num":29 },  
{ "k": "description", "v/str": "something" }  
]  
}

But a problem I see with this approach is, that I cannot sort for the temperature anymore, as far as I can see.

Do you maybe have an advice for me how I can index this kind of data without making the mapping explode but still be able to sort on such fields?

Thanks for your help.  
Benno

---

<div class="post-metadata">

**Author:** ![BennoSI](https://avatars.discourse-cdn.com/v4/letter/b/67e7ee/32.png) [@BennoSI](https://discuss.elastic.co/u/BennoSI)\
**Post date:** [March 11, 2016, 3:24pm UTC](https://discuss.elastic.co/t/searching-and-sorting-dynamic-fields/44134/2 "2016-03-11T15:24:31Z")

</div>

ah cool seems like this should even work two, e.g. by something like this ?

{  
"query": {  
"match\_all": {}  
},  
"sort": [  
{  
"attributes.v/num": {  
"order": "asc",  
"nested\_path": "attributes",  
"nested\_filter": {  
"term": {  
"attributes.k": "model"  
}  
}  
}  
}  
]  
}

---

<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:** [March 12, 2016, 8:52am UTC](https://discuss.elastic.co/t/searching-and-sorting-dynamic-fields/44134/3 "2016-03-12T08:52:10Z")

</div>

Be careful of this, you could run into a mapping explosion. You'd be better off normalising things if you can.

---

<div class="post-metadata">

**Author:** ![BennoSI](https://avatars.discourse-cdn.com/v4/letter/b/67e7ee/32.png) [@BennoSI](https://discuss.elastic.co/u/BennoSI)\
**Post date:** [March 12, 2016, 3:46pm UTC](https://discuss.elastic.co/t/searching-and-sorting-dynamic-fields/44134/4 "2016-03-12T15:46:46Z")

</div>

thanks for your reply.  
You are right with the first approach I mentioned, where there could be introduced a huge amount of new mapping fields.

But my second approach should not have this problem I guess, because there I save the data in nested array objects of the form:

{  
"attributes": [  
{ "k": "temperature", "v/num":29 },  
{ "k": "description", "v/str": "something" }  
]  
}

so there shouldn't be too many mapping entries, only attributes and a nested element with fields: k, v/num, v/str, and v/bool.

Or do you see a probem here too?

regards,  
Benno

---

<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 5, 2017, 11:08pm UTC](https://discuss.elastic.co/t/searching-and-sorting-dynamic-fields/44134/5 "2017-07-05T23:08:52Z")

</div>


