I'm using Elasticsearch v5.0
and want to query all cities within the specified radius. But it looks like I didn't specific sorting feature? Where to set that?
Thank you
public static final int NB_MAX_RESULTS = 200;
public static final double ORIGIN_CITY_LON = 2.34; // Paris longitude(2.34)
public static final double ORIGIN_CITY_LAT = 48.86; // Paris lattitude(48.86)
public static final int DISTANCE_FROM_ORIGIN = 1000;
public static final DistanceUnit DISTANCE_UNIT = DistanceUnit.KILOMETERS;
QueryBuilder qb = geoDistanceQuery("coordinates")
.point(Constant.ORIGIN_CITY_LAT, Constant.ORIGIN_CITY_LON)
.distance(Constant.DISTANCE_FROM_ORIGIN, Constant.DISTANCE_UNIT)
.optimizeBbox("memory")
.geoDistance(GeoDistance.ARC);
SearchRequestBuilder searchRequestBuilder = client.prepareSearch("location").setTypes("city")
.setQuery(qb)
.setFrom(0)
.setSize(Constant.NB_MAX_RESULTS)
.addSort(SortBuilders.fieldSort("coordinates")
.order(SortOrder.DESC))
.addAggregation(AggregationBuilders.geoDistance("agg", new GeoPoint(Constant.ORIGIN_CITY_LAT, Constant.ORIGIN_CITY_LON))
.field("coordinates")
.unit(DistanceUnit.KILOMETERS)
.addRange(0, Constant.DISTANCE_FROM_ORIGIN));
SearchResponse sr = searchRequestBuilder.execute().actionGet();
Range agg = sr.getAggregations().get("agg");
System.out.println("------------------------------------------------------------------------------");
System.out.println("Search Results (reversed order):");
System.out.println("------------------------------------------------------------------------------");
for (SearchHit hit : sr.getHits()) {
Map<String,SearchHitField> fields = hit.getFields();
for(Map.Entry<String,SearchHitField> entry : fields.entrySet()){
System.out.println(entry.getValue().getValues().get(0) +" ("+hit.getId()+") is "+hit.getSortValues()[0].toString()+" km far from origin");
}
}
System.out.println("------------------------------------------------------------------------------");
System.out.println("GeoDistanceFacet results:");
System.out.println("------------------------------------------------------------------------------");
for (Range.Bucket entry : agg.getBuckets()) {
System.out.println("Key: "+entry.getKeyAsString());
System.out.println("Distance from origin: "+entry.getFrom()); // Distance from requested
System.out.println("Distance to requested: "+entry.getTo()); // Distance to requested
System.out.println("Number of results: "+entry.getDocCount()); // Doc count
}
This is the ERROR message:
Failed to execute phase [query], all shards failed; shardFailures {[T01_IzVqSaCvPJrGEfrnFw][location][0]: RemoteTransportException[[T01_IzV][127.0.0.1:9300][indices:data/read/search[phase/query]]]; nested: IllegalArgumentException[can't sort on geo_point field without using specific sorting feature, like geo_distance]; }{[T01_IzVqSaCvPJrGEfrnFw][location][1]: RemoteTransportException[[T01_IzV][127.0.0.1:9300][indices:data/read/search[phase/query]]]; nested: IllegalArgumentException[can't sort on geo_point field without using specific sorting feature, like geo_distance]; }{[T01_IzVqSaCvPJrGEfrnFw][location][2]: RemoteTransportException[[T01_IzV][127.0.0.1:9300][indices:data/read/search[phase/query]]]; nested: IllegalArgumentException[can't sort on geo_point field without using specific sorting feature, like geo_distance]; }{[T01_IzVqSaCvPJrGEfrnFw][location][3]: RemoteTransportException[[T01_IzV][127.0.0.1:9300][indices:data/read/search[phase/query]]]; nested: IllegalArgumentException[can't sort on geo_point field without using specific sorting feature, like geo_distance]; }{[T01_IzVqSaCvPJrGEfrnFw][location][4]: RemoteTransportException[[T01_IzV][127.0.0.1:9300][indices:data/read/search[phase/query]]]; nested: IllegalArgumentException[can't sort on geo_point field without using specific sorting feature, like geo_distance]; }
This screenshot is about my index;