# Sort on missing field

**URL:** <https://discuss.elastic.co/t/sort-on-missing-field/4934>\
**Category:** Elasticsearch\
**Created:** [July 23, 2011, 5:42pm UTC](https://discuss.elastic.co/t/sort-on-missing-field/4934 "2011-07-23T17:42:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![erik\_seilnacht](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/erik_seilnacht/32/3139_2.png) [@erik\_seilnacht](https://discuss.elastic.co/u/erik_seilnacht)\
**Post date:** [July 23, 2011, 5:42pm UTC](https://discuss.elastic.co/t/sort-on-missing-field/4934/1 "2011-07-23T17:42:48Z")

</div>

Hi.

I have an index where I need to sort on a sequence number field that was recently added. The problem is that I must use the missing option to allow for sorting since only new entries in the index will have a value.

Even though a Long was specified ( using the java API ) when this field was introduced, the type defaulted to "string"... In order for the sort.missing to work, I understand the field must be numeric.

I've tried using the mapping rest API to change the type to "long" but it didn't work...

root@ec2-184-72-181-182:~# curl -XGET [http://search.server.com:9200/index/erik/\_mapping?pretty;echo](http://search.server.com:9200/index/erik/_mapping?pretty;echo)  
{  
"erik" : {  
"properties" : {  
"timestamp" : {  
"format" : "dateOptionalTime",  
"type" : "date"  
},  
"message" : {  
"dynamic" : "true",  
"properties" : {  
"text" : {  
"type" : "string"  
}  
}  
},  
"sequence" : {  
"type" : "string"  
},  
"priority" : {  
"type" : "string"  
},  
"fields" : {  
"dynamic" : "true",  
"properties" : {  
"domain" : {  
"type" : "string"  
},  
"serverId" : {  
"type" : "string"  
}  
}  
}  
}  
}  
}

curl -XPUT [http://search.server.com:9200/index/erik/\_mapping](http://search.server.com:9200/index/erik/_mapping) -d '{ "sequence" : { "properties" : { "sequence" : { "type" : "long" } } } }';echo  
{"error":"MergeMappingException[Merge failed with failures {[mapper [sequence] of different type, current\_type [string], merged\_type [long]]}]","status":400}

so I used the multi\_field option:

curl -XPUT [http://search.server.com:9200/index/erik/\_mapping](http://search.server.com:9200/index/erik/_mapping) -d '{ "sequence" : { "properties" : { "sequence" : { "type" : "multi\_field", "fields" : { "sequence" : { "type" : "long" } } } } } }';echo  
{"ok":true,"acknowledged":true}

and the mappings were changed to:

{  
"erik" : {  
"properties" : {  
"timestamp" : {  
"format" : "dateOptionalTime",  
"type" : "date"  
},  
"message" : {  
"dynamic" : "true",  
"properties" : {  
"text" : {  
"type" : "string"  
}  
}  
},  
"sequence" : {  
"type" : "multi\_field",  
"fields" : {  
"sequence" : {  
"type" : "long"  
}  
}  
},  
"priority" : {  
"type" : "string"  
},  
"fields" : {  
"dynamic" : "true",  
"properties" : {  
"domain" : {  
"type" : "string"  
},  
"serverId" : {  
"type" : "string"  
}  
}  
}  
}  
}  
}

However, my sort query still fails...

root@ec2-184-72-181-182:~# curl -XGET [http://search.server.com:9200/index/\_search?pretty=true](http://search.server.com:9200/index/_search?pretty=true) -d ' {"from" : 1, "size" : 2, "query" : { "range" : { "timestamp" : { "from" : "2011-07-22T07:00:45.012Z", "to" : null } } }, "sort" : [{"timestamp" : {} }, { "sequence" : { "missing" : 0 } }]} '

ElasticSearchIllegalArgumentException[Sorting on string type field does not support missing parameter]

Not sure what I can do at this point...

Any advice?

Thanks,

-Erik

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [July 26, 2011, 9:05am UTC](https://discuss.elastic.co/t/sort-on-missing-field/4934/2 "2011-07-26T09:05:04Z")

</div>

You can't change a field type, once its set. The reason for that is that it  
is indexed in a specific manner based on the type. Its strange that it  
became string if you indexed it as long though... You need to reindex the  
data to use it as long.

On Sat, Jul 23, 2011 at 8:42 PM, [erik.seilnacht@mulesoft.com](mailto:erik.seilnacht@mulesoft.com) \<  
[erik.seilnacht@mulesoft.com](mailto:erik.seilnacht@mulesoft.com)\> wrote:

> Hi.
> 
> I have an index where I need to sort on a sequence number field that was  
> recently added. The problem is that I must use the missing option to allow  
> for sorting since only new entries in the index will have a value.
> 
> Even though a Long was specified ( using the java API ) when this field was  
> introduced, the type defaulted to "string"... In order for the  
> sort.missing  
> to work, I understand the field must be numeric.
> 
> I've tried using the mapping rest API to change the type to "long" but it  
> didn't work...
> 
> root@ec2-184-72-181-182:~# curl -XGET  
> [http://search.server.com:9200/index/erik/\_mapping?pretty;echo](http://search.server.com:9200/index/erik/_mapping?pretty;echo)  
> {  
> "erik" : {  
> "properties" : {  
> "timestamp" : {  
> "format" : "dateOptionalTime",  
> "type" : "date"  
> },  
> "message" : {  
> "dynamic" : "true",  
> "properties" : {  
> "text" : {  
> "type" : "string"  
> }  
> }  
> },  
> "sequence" : {  
> "type" : "string"  
> },  
> "priority" : {  
> "type" : "string"  
> },  
> "fields" : {  
> "dynamic" : "true",  
> "properties" : {  
> "domain" : {  
> "type" : "string"  
> },  
> "serverId" : {  
> "type" : "string"  
> }  
> }  
> }  
> }  
> }  
> }
> 
> curl -XPUT [http://search.server.com:9200/index/erik/\_mapping](http://search.server.com:9200/index/erik/_mapping) -d '{  
> "sequence" : { "properties" : { "sequence" : { "type" : "long" } } }  
> }';echo  
> {"error":"MergeMappingException[Merge failed with failures {[mapper  
> [sequence] of different type, current\_type [string], merged\_type  
> [long]]}]","status":400}
> 
> so I used the multi\_field option:
> 
> curl -XPUT [http://search.server.com:9200/index/erik/\_mapping](http://search.server.com:9200/index/erik/_mapping) -d '{  
> "sequence" : { "properties" : { "sequence" : { "type" : "multi\_field",  
> "fields" : { "sequence" : { "type" : "long" } } } } } }';echo  
> {"ok":true,"acknowledged":true}
> 
> and the mappings were changed to:
> 
> {  
> "erik" : {  
> "properties" : {  
> "timestamp" : {  
> "format" : "dateOptionalTime",  
> "type" : "date"  
> },  
> "message" : {  
> "dynamic" : "true",  
> "properties" : {  
> "text" : {  
> "type" : "string"  
> }  
> }  
> },  
> "sequence" : {  
> "type" : "multi\_field",  
> "fields" : {  
> "sequence" : {  
> "type" : "long"  
> }  
> }  
> },  
> "priority" : {  
> "type" : "string"  
> },  
> "fields" : {  
> "dynamic" : "true",  
> "properties" : {  
> "domain" : {  
> "type" : "string"  
> },  
> "serverId" : {  
> "type" : "string"  
> }  
> }  
> }  
> }  
> }  
> }
> 
> However, my sort query still fails...
> 
> root@ec2-184-72-181-182:~# curl -XGET  
> [http://search.server.com:9200/index/\_search?pretty=true](http://search.server.com:9200/index/_search?pretty=true) -d ' {"from" : 1,  
> "size" : 2, "query" : { "range" : { "timestamp" : { "from" :  
> "2011-07-22T07:00:45.012Z", "to" : null } } }, "sort" : [ {"timestamp" : {}  
> }, { "sequence" : { "missing" : 0 } } ]} '
> 
> ElasticSearchIllegalArgumentException[Sorting on string type field does not  
> support missing parameter]
> 
> Not sure what I can do at this point...
> 
> Any advice?
> 
> Thanks,
> 
> -Erik
> 
> --  
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/sort-on-missing-field-tp3193903p3193903.html](http://elasticsearch-users.115913.n3.nabble.com/sort-on-missing-field-tp3193903p3193903.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<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, 3:59am UTC](https://discuss.elastic.co/t/sort-on-missing-field/4934/3 "2017-07-06T03:59:23Z")

</div>


