I have implemented search and sorting of users in Elasticsearch based on their location. It used to work fine in Elasticsearch 1.6, but after upgrading to Elasticsearch 2.3.3 it stopped working.
Relevant part of my mapping for user.location in ES is:
{
"user": {
"properties": {
"location": {
"type": "geo_point"
},
}
When I try to execute the following query directly it works fine:
[
{
"from": 0, "size": 3,
"query": {
"bool": { "must_not": { "term": { "id": 2 }
},
"should": { "match_all": {} }
}
},
"sort": [
{
"_geo_distance": {
"user.location": [
{ "lat": 49.2827291, "lon": -123.12073750000002 }
],
"unit": "km"
} } ] } ]
However, when the same query is executed using Java API, there is an exception:
Failed to execute phase [query], all shards failed; shardFailures {[G88txaRXQESUgytUEnTwug][users_zj][0]: RemoteTransportException[[Living Colossus][127.0.0.1:9300][indices:data/read/search[phase/query]]]; nested: SearchParseException[failed to parse search source [{"from":0,"size":3,"query":{"bool":{"must_not":{"term":{"id":2}},"should":{"match_all":{}}}},"sort":[{"_geo_distance":{"user.location":[{"lat":49.2827291,"lon":-123.12073750000002}],"unit":"km"}}]}]]; nested: IllegalArgumentException[failed to find mapper for [user.location] for geo distance based sort]; }{[G88txaRXQESUgytUEnTwug][users_zj][1]: RemoteTransportException[[Living Colossus][127.0.0.1:9300][indices:data/read/search[phase/query]]]; nested: SearchParseException[failed to parse search source [{"from":0,"size":3,"query":{"bool":{"must_not":{"term":{"id":2}},"should":{"match_all":{}}}},"sort":[{"_geo_distance":{"user.location":[{"lat":49.2827291,"lon":-123.12073750000002}],"unit":"km"}}]}]]; nested: IllegalArgumentException[failed to find mapper for [user.location] for geo distance based sort]; }{[G88txaRXQESUgytUEnTwug][users_zj][2]: RemoteTransportException[[Living Colossus][127.0.0.1:9300][indices:data/read/search[phase/query]]]; nested: SearchParseException[failed to parse search source [{"from":0,"size":3,"query":{"bool":{"must_not":{"term":{"id":2}},"should":{"match_all":{}}}},"sort":[{"_geo_distance":{"user.location":[{"lat":49.2827291,"lon":-123.12073750000002}],"unit":"km"}}]}]]; nested: IllegalArgumentException[failed to find mapper for [user.location] for geo distance based sort]; }{[G88txaRXQESUgytUEnTwug][users_zj][3]: RemoteTransportException[[Living Colossus][127.0.0.1:9300][indices:data/read/search[phase/query]]]; nested: SearchParseException[failed to parse search source [{"from":0,"size":3,"query":{"bool":{"must_not":{"term":{"id":2}},"should":{"match_all":{}}}},"sort":[{"_geo_distance":{"user.location":[{"lat":49.2827291,"lon":-123.12073750000002}],"unit":"km"}}]}]]; nested: IllegalArgumentException[failed to find mapper for [user.location] for geo distance based sort]; }{[G88txaRXQESUgytUEnTwug][users_zj][4]: RemoteTransportException[[Living Colossus][127.0.0.1:9300][indices:data/read/search[phase/query]]]; nested: SearchParseException[failed to parse search source [{"from":0,"size":3,"query":{"bool":{"must_not":{"term":{"id":2}},"should":{"match_all":{}}}},"sort":[{"_geo_distance":{"user.location":[{"lat":49.2827291,"lon":-123.12073750000002}],"unit":"km"}}]}]]; nested: IllegalArgumentException[failed to find mapper for [user.location] for geo distance based sort]; }
Caused by: java.lang.IllegalArgumentException: failed to find mapper for [user.location] for geo distance based sort
This is java code that fires exception:
Client client = ElasticSearchFactory.getClient(); QueryBuilder qb = new MatchAllQueryBuilder(); BoolQueryBuilder bQueryBuilder = QueryBuilders.boolQuery(); bQueryBuilder.should(qb);
for (User ignUser : ignoredUsers) { if (ignUser != null) { bQueryBuilder.mustNot(termQuery("id", ignUser.getId())); } } GeoDistanceSortBuilder sortBuilder = SortBuilders.geoDistanceSort("user.location") .point(lat, lon) .unit(DistanceUnit.KILOMETERS) .order(SortOrder.ASC);
SearchResponse sResponse = client .prepareSearch(ESIndexNames.INDEX_USERS) .setTypes(ESIndexTypes.USER) .setQuery(bQueryBuilder).addSort(sortBuilder) .setFrom(0) .setSize(limit).execute() .actionGet(); } catch (NoNodeAvailableException e1) {
Anybody has idea how to investigate and find the problem?
Thanks,
Zoran