# Sort over dynamic fields

**URL:** <https://discuss.elastic.co/t/sort-over-dynamic-fields/10484>\
**Category:** Elasticsearch\
**Created:** [January 24, 2013, 8:39am UTC](https://discuss.elastic.co/t/sort-over-dynamic-fields/10484 "2013-01-24T08:39:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tom\_4](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tom_4/32/1045_2.png) [@Tom\_4](https://discuss.elastic.co/u/Tom_4)\
**Post date:** [January 24, 2013, 8:39am UTC](https://discuss.elastic.co/t/sort-over-dynamic-fields/10484/1 "2013-01-24T08:39:45Z")

</div>

Hi,

when i try to sort over a dynamically created field the query crashes when  
the field does not exist cause there is no mapping for this field available.

1. is there a way to set something like a default mapping with wildcards  
for dyn fields like "myid\_\*" ?
2. is it possible to combine an exist check with the sorting?
3. other solutions?

thx & regards  
tom

--

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [January 24, 2013, 5:02pm UTC](https://discuss.elastic.co/t/sort-over-dynamic-fields/10484/2 "2013-01-24T17:02:02Z")

</div>

Answers inline.

On Thu, Jan 24, 2013 at 12:39 AM, Tom [t.oppelt@superreal.de](mailto:t.oppelt@superreal.de) wrote:

> Hi,
> 
> when i try to sort over a dynamically created field the query crashes when  
> the field does not exist cause there is no mapping for this field available.
> 
> 1. is there a way to set something like a default mapping with wildcards  
> for dyn fields like "myid\_\*" ?

You can use dynamic templates (scroll down to dynamic\_templates):

> **[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.

> 1. is it possible to combine an exist check with the sorting?

Not sure what your use case is, but can you use the Missing Values feature?  
You can put documents that do not have the field to the bottom, or give  
them a default value that can be filterered out on the client side.

> **[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.

1. other solutions?

> Depends on your use case. What is not working with the existing solutions?

> thx & regards  
> tom
> 
> --

Cheers,

Ivan

--

---

<div class="post-metadata">

**Author:** ![Tom\_4](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tom_4/32/1045_2.png) [@Tom\_4](https://discuss.elastic.co/u/Tom_4)\
**Post date:** [January 25, 2013, 2:49pm UTC](https://discuss.elastic.co/t/sort-over-dynamic-fields/10484/3 "2013-01-25T14:49:16Z")

</div>

Am Donnerstag, 24. Januar 2013 18:02:02 UTC+1 schrieb Ivan Brusic:

> Answers inline.
> 
> On Thu, Jan 24, 2013 at 12:39 AM, Tom \<[t.op...@superreal.de](mailto:t.op...@superreal.de) \<javascript:\>\>wrote:
> 
> > Hi,
> > 
> > when i try to sort over a dynamically created field the query crashes  
> > when the field does not exist cause there is no mapping for this field  
> > available.
> > 
> > 1. is there a way to set something like a default mapping with wildcards  
> > for dyn fields like "myid\_\*" ?
> 
> You can use dynamic templates (scroll down to dynamic\_templates):  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/root-object-type.html)

It didn't work for my sorting problem:  
Test Mapping:

curl -XPUT '[http://localhost:9200/test/item/\_mapping?pretty=true](http://localhost:9200/test/item/_mapping?pretty=true)' -d   
'  
{  
"item" : {  
"dynamic\_templates" : [  
{  
"template\_1" : {  
"match" : "mysort\_\*",  
"mapping" : {  
"type" : "integer",  
"index" : "not\_analyzed",  
"store" : "yes"  
} } }  
],  
properties: {  
"message" : {  
"type" : "string",  
"store" : "yes",  
"index" : "analyzed"  
}  
}  
}  
}  
'

Query:

curl -XGET '[http://localhost:9200/test/item/\_search?pretty=true](http://localhost:9200/test/item/_search?pretty=true)' -d   
'  
{  
"sort":[  
{ "test\_notexist":{ "order":"asc" } }  
],  
"query":{  
(...)  
},  
"filter":{  
"exists":{ "field":"test\_notexist" }  
}  
}  
'

This query crashes with:

Parse Failure [No mapping found for [test\_notexists] in order to sort on  
...

Did i miss something?

--

---

<div class="post-metadata">

**Author:** ![Tom\_4](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tom_4/32/1045_2.png) [@Tom\_4](https://discuss.elastic.co/u/Tom_4)\
**Post date:** [January 25, 2013, 2:51pm UTC](https://discuss.elastic.co/t/sort-over-dynamic-fields/10484/4 "2013-01-25T14:51:13Z")

</div>

sorry i pasted wrong query in my last post of course i query for  
mysort\_notexists field. but it makes no difference.

Am Donnerstag, 24. Januar 2013 09:39:45 UTC+1 schrieb Tom:

> Hi,
> 
> when i try to sort over a dynamically created field the query crashes when  
> the field does not exist cause there is no mapping for this field available.
> 
> 1. is there a way to set something like a default mapping with wildcards  
> for dyn fields like "myid\_\*" ?
> 2. is it possible to combine an exist check with the sorting?
> 3. other solutions?
> 
> thx & regards  
> tom

--

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [January 25, 2013, 4:02pm UTC](https://discuss.elastic.co/t/sort-over-dynamic-fields/10484/5 "2013-01-25T16:02:52Z")

</div>

Sorry! I answered your questions, but forgot to address the main issue.

Try using the ignore\_unmapped option when sorting against on unmapped  
field. Might not be the best solution, but it will stop the exception from  
occurring.

--  
Ivan

On Fri, Jan 25, 2013 at 6:51 AM, Tom [t.oppelt@superreal.de](mailto:t.oppelt@superreal.de) wrote:

> sorry i pasted wrong query in my last post of course i query for  
> mysort\_notexists field. but it makes no difference.
> 
> Am Donnerstag, 24. Januar 2013 09:39:45 UTC+1 schrieb Tom:
> 
> > Hi,
> > 
> > when i try to sort over a dynamically created field the query crashes  
> > when the field does not exist cause there is no mapping for this field  
> > available.
> > 
> > 1. is there a way to set something like a default mapping with wildcards  
> > for dyn fields like "myid\_\*" ?
> > 2. is it possible to combine an exist check with the sorting?
> > 3. other solutions?
> > 
> > thx & regards  
> > tom
> 
> --

--

---

<div class="post-metadata">

**Author:** ![Tom\_4](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tom_4/32/1045_2.png) [@Tom\_4](https://discuss.elastic.co/u/Tom_4)\
**Post date:** [January 25, 2013, 4:11pm UTC](https://discuss.elastic.co/t/sort-over-dynamic-fields/10484/6 "2013-01-25T16:11:32Z")

</div>

hm, ok, but actually this field is mapped by dynamic\_template.  
so there is a mapping available actually, but the specific dynamic field  
does not exist.  
disturbing.

Am Freitag, 25. Januar 2013 17:02:52 UTC+1 schrieb Ivan Brusic:

> Sorry! I answered your questions, but forgot to address the main issue.
> 
> Try using the ignore\_unmapped option when sorting against on unmapped  
> field. Might not be the best solution, but it will stop the exception from  
> occurring.
> 
> --  
> Ivan
> 
> On Fri, Jan 25, 2013 at 6:51 AM, Tom \<[t.op...@superreal.de](mailto:t.op...@superreal.de) \<javascript:\>\>wrote:
> 
> > sorry i pasted wrong query in my last post of course i query for  
> > mysort\_notexists field. but it makes no difference.
> > 
> > Am Donnerstag, 24. Januar 2013 09:39:45 UTC+1 schrieb Tom:
> > 
> > > Hi,
> > > 
> > > when i try to sort over a dynamically created field the query crashes  
> > > when the field does not exist cause there is no mapping for this field  
> > > available.
> > > 
> > > 1. is there a way to set something like a default mapping with wildcards  
> > > for dyn fields like "myid\_\*" ?
> > > 2. is it possible to combine an exist check with the sorting?
> > > 3. other solutions?
> > > 
> > > thx & regards  
> > > tom
> > 
> > --

--

---

<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, 2:54am UTC](https://discuss.elastic.co/t/sort-over-dynamic-fields/10484/7 "2017-07-06T02:54:34Z")

</div>


