# Sorting on a GeoPoint throws a ClassCastException

**URL:** <https://discuss.elastic.co/t/sorting-on-a-geopoint-throws-a-classcastexception/12386>\
**Category:** Elasticsearch\
**Created:** [June 12, 2013, 3:48pm UTC](https://discuss.elastic.co/t/sorting-on-a-geopoint-throws-a-classcastexception/12386 "2013-06-12T15:48:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mehrez\_Marouani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mehrez_marouani/32/1218_2.png) [@Mehrez\_Marouani](https://discuss.elastic.co/u/Mehrez_Marouani)\
**Post date:** [June 12, 2013, 3:48pm UTC](https://discuss.elastic.co/t/sorting-on-a-geopoint-throws-a-classcastexception/12386/1 "2013-06-12T15:48:54Z")

</div>

Hi everybody,

I'm trying to add a sort on a geo\_point field but get a ClassCastException.

* * *

## The query:

{  
"query": {  
"match\_all": {}  
},  
"size": 10,  
"sort": {  
"dealerLocation": {  
"order": "asc"  
}  
}  
}

* * *

## the mapping

{  
"usedcar": {  
"properties": {  
"dealerId": {  
"type": "string"  
},  
"dealerLocation": {  
"type": "geo\_point"  
}  
}  
}  
}

* * *

## the result

{  
"error": "SearchPhaseExecutionException[Failed to execute phase  
[query], total failure; shardFailures {[6qz3pf-nTp-raW4PxwknnQ][en\_gb][3]:  
QueryPhaseExecutionException[[en\_gb][3]:  
query[ConstantScore(NotDeleted(cache(\_type:usedcar)))],from[0],size[10],sort[\<custom:"dealerLocation":  
org.elasticsearch.index.mapper.geo.GeoPointFieldDataType$1@1bd5b5b\>]: Query  
Failed [Failed to execute main query]]; nested:  
ClassCastException[org.elasticsearch.index.mapper.geo.SingleValueGeoPointFieldData  
cannot be cast to  
org.elasticsearch.index.field.data.strings.SingleValueStringFieldData];  
}{[6qz3pf-nTp-raW4PxwknnQ][en\_gb][0]:  
QueryPhaseExecutionException[[en\_gb][0]:  
query[ConstantScore(NotDeleted(cache(\_type:usedcar)))],from[0],size[10],sort[\<custom:"dealerLocation":  
org.elasticsearch.index.mapper.geo.GeoPointFieldDataType$1@1e78c5e\>]: Query  
Failed [Failed to execute main query]]; nested:  
ClassCastException[org.elasticsearch.index.mapper.geo.SingleValueGeoPointFieldData  
cannot be cast to  
org.elasticsearch.index.field.data.strings.SingleValueStringFieldData];  
}{[6qz3pf-nTp-raW4PxwknnQ][en\_gb][4]:  
QueryPhaseExecutionException[[en\_gb][4]:  
query[ConstantScore(NotDeleted(cache(\_type:usedcar)))],from[0],size[10],sort[\<custom:"dealerLocation":  
org.elasticsearch.index.mapper.geo.GeoPointFieldDataType$1@b09747\>]: Query  
Failed [Failed to execute main query]]; nested:  
ClassCastException[org.elasticsearch.index.mapper.geo.SingleValueGeoPointFieldData  
cannot be cast to  
org.elasticsearch.index.field.data.strings.SingleValueStringFieldData]; }]",  
"status": 500  
}

Any idea ?

Regards,

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [June 13, 2013, 2:50pm UTC](https://discuss.elastic.co/t/sorting-on-a-geopoint-throws-a-classcastexception/12386/2 "2013-06-13T14:50:54Z")

</div>

If you want to use geo sorting you need to use the "\_geo\_distance" format  
as is described here:

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

I expect that the following works:  
{  
"query": {  
"match\_all": {}  
},  
"size": 10,  
"sort": {  
"\_geo\_distance" : {  
"dealerLocation" : [-70, 40], // \<-- the point to calculate  
the distance from  
"order" : "asc",  
"unit" : "km"  
}  
}  
}

What Elasticsearch version are you using? Seems like regular field sorting  
doesn't like to sort on geo\_point. A better error message would be nicer.

Martijn

On 12 June 2013 17:48, Mehrez Marouani [mehrez.marouani@gmail.com](mailto:mehrez.marouani@gmail.com) wrote:

> Hi everybody,
> 
> I'm trying to add a sort on a geo\_point field but get a ClassCastException.
> 
> * * *
> 
> ## The query:
> 
> {  
> "query": {  
> "match\_all": {}  
> },  
> "size": 10,  
> "sort": {  
> "dealerLocation": {  
> "order": "asc"  
> }  
> }  
> }
> 
> * * *
> 
> ## the mapping
> 
> {  
> "usedcar": {  
> "properties": {  
> "dealerId": {  
> "type": "string"  
> },  
> "dealerLocation": {  
> "type": "geo\_point"  
> }  
> }  
> }  
> }
> 
> * * *
> 
> ## the result
> 
> {  
> "error": "SearchPhaseExecutionException[Failed to execute phase  
> [query], total failure; shardFailures {[6qz3pf-nTp-raW4PxwknnQ][en\_gb][3]:  
> QueryPhaseExecutionException[[en\_gb][3]:  
> query[ConstantScore(NotDeleted(cache(\_type:usedcar)))],from[0],size[10],sort[\<custom:"dealerLocation":  
> org.elasticsearch.index.mapper.geo.GeoPointFieldDataType$1@1bd5b5b\>]:  
> Query Failed [Failed to execute main query]]; nested:  
> ClassCastException[org.elasticsearch.index.mapper.geo.SingleValueGeoPointFieldData  
> cannot be cast to  
> org.elasticsearch.index.field.data.strings.SingleValueStringFieldData];  
> }{[6qz3pf-nTp-raW4PxwknnQ][en\_gb][0]:  
> QueryPhaseExecutionException[[en\_gb][0]:  
> query[ConstantScore(NotDeleted(cache(\_type:usedcar)))],from[0],size[10],sort[\<custom:"dealerLocation":  
> org.elasticsearch.index.mapper.geo.GeoPointFieldDataType$1@1e78c5e\>]:  
> Query Failed [Failed to execute main query]]; nested:  
> ClassCastException[org.elasticsearch.index.mapper.geo.SingleValueGeoPointFieldData  
> cannot be cast to  
> org.elasticsearch.index.field.data.strings.SingleValueStringFieldData];  
> }{[6qz3pf-nTp-raW4PxwknnQ][en\_gb][4]:  
> QueryPhaseExecutionException[[en\_gb][4]:  
> query[ConstantScore(NotDeleted(cache(\_type:usedcar)))],from[0],size[10],sort[\<custom:"dealerLocation":  
> org.elasticsearch.index.mapper.geo.GeoPointFieldDataType$1@b09747\>]:  
> Query Failed [Failed to execute main query]]; nested:  
> ClassCastException[org.elasticsearch.index.mapper.geo.SingleValueGeoPointFieldData  
> cannot be cast to  
> org.elasticsearch.index.field.data.strings.SingleValueStringFieldData]; }]",  
> "status": 500  
> }
> 
> Any idea ?
> 
> Regards,
> 
> --  
> 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).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
Met vriendelijke groet,

Martijn van Groningen

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Mehrez\_Marouani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mehrez_marouani/32/1218_2.png) [@Mehrez\_Marouani](https://discuss.elastic.co/u/Mehrez_Marouani)\
**Post date:** [June 14, 2013, 12:32pm UTC](https://discuss.elastic.co/t/sorting-on-a-geopoint-throws-a-classcastexception/12386/3 "2013-06-14T12:32:24Z")

</div>

Thanks Martijn.

I've already solved the problem by using the latest version.  
I was using 0.20.1 version.

Regards,

On Thursday, June 13, 2013 4:50:54 PM UTC+2, Martijn v Groningen wrote:

> If you want to use geo sorting you need to use the "\_geo\_distance" format  
> as is described here:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/sort/)
> 
> I expect that the following works:  
> {  
> "query": {  
> "match\_all": {}  
> },  
> "size": 10,  
> "sort": {  
> "\_geo\_distance" : {  
> "dealerLocation" : [-70, 40], // \<-- the point to  
> calculate the distance from  
> "order" : "asc",  
> "unit" : "km"  
> }  
> }  
> }
> 
> What Elasticsearch version are you using? Seems like regular field sorting  
> doesn't like to sort on geo\_point. A better error message would be nicer.
> 
> Martijn
> 
> On 12 June 2013 17:48, Mehrez Marouani \<[mehrez....@gmail.com](mailto:mehrez....@gmail.com) \<javascript:\>
> 
> > wrote:
> 
> > Hi everybody,
> > 
> > I'm trying to add a sort on a geo\_point field but get a  
> > ClassCastException.
> > 
> > * * *
> > 
> > ## The query:
> > 
> > {  
> > "query": {  
> > "match\_all": {}  
> > },  
> > "size": 10,  
> > "sort": {  
> > "dealerLocation": {  
> > "order": "asc"  
> > }  
> > }  
> > }
> > 
> > * * *
> > 
> > ## the mapping
> > 
> > {  
> > "usedcar": {  
> > "properties": {  
> > "dealerId": {  
> > "type": "string"  
> > },  
> > "dealerLocation": {  
> > "type": "geo\_point"  
> > }  
> > }  
> > }  
> > }
> > 
> > * * *
> > 
> > ## the result
> > 
> > {  
> > "error": "SearchPhaseExecutionException[Failed to execute phase  
> > [query], total failure; shardFailures {[6qz3pf-nTp-raW4PxwknnQ][en\_gb][3]:  
> > QueryPhaseExecutionException[[en\_gb][3]:  
> > query[ConstantScore(NotDeleted(cache(\_type:usedcar)))],from[0],size[10],sort[\<custom:"dealerLocation":  
> > org.elasticsearch.index.mapper.geo.GeoPointFieldDataType$1@1bd5b5b\>]: Query  
> > Failed [Failed to execute main query]]; nested:  
> > ClassCastException[org.elasticsearch.index.mapper.geo.SingleValueGeoPointFieldData  
> > cannot be cast to  
> > org.elasticsearch.index.field.data.strings.SingleValueStringFieldData];  
> > }{[6qz3pf-nTp-raW4PxwknnQ][en\_gb][0]:  
> > QueryPhaseExecutionException[[en\_gb][0]:  
> > query[ConstantScore(NotDeleted(cache(\_type:usedcar)))],from[0],size[10],sort[\<custom:"dealerLocation":  
> > org.elasticsearch.index.mapper.geo.GeoPointFieldDataType$1@1e78c5e\>]: Query  
> > Failed [Failed to execute main query]]; nested:  
> > ClassCastException[org.elasticsearch.index.mapper.geo.SingleValueGeoPointFieldData  
> > cannot be cast to  
> > org.elasticsearch.index.field.data.strings.SingleValueStringFieldData];  
> > }{[6qz3pf-nTp-raW4PxwknnQ][en\_gb][4]:  
> > QueryPhaseExecutionException[[en\_gb][4]:  
> > query[ConstantScore(NotDeleted(cache(\_type:usedcar)))],from[0],size[10],sort[\<custom:"dealerLocation":  
> > org.elasticsearch.index.mapper.geo.GeoPointFieldDataType$1@b09747\>]: Query  
> > Failed [Failed to execute main query]]; nested:  
> > ClassCastException[org.elasticsearch.index.mapper.geo.SingleValueGeoPointFieldData  
> > cannot be cast to  
> > org.elasticsearch.index.field.data.strings.SingleValueStringFieldData]; }]",  
> > "status": 500  
> > }
> > 
> > Any idea ?
> > 
> > Regards,
> > 
> > --  
> > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> Met vriendelijke groet,
> 
> Martijn van Groningen

--  
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).  
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, 2:31am UTC](https://discuss.elastic.co/t/sorting-on-a-geopoint-throws-a-classcastexception/12386/4 "2017-07-06T02:31:15Z")

</div>


