In Elastic Search 6.1.0, facing this error,"The method geoDistanceSort(String, GeoPoint[]) is ambiguous for the type SortBuilders"
SortBuilders.geoDistanceSort("location")
.point(latitude, longitude)
.order(SortOrder.ASC)
.unit(DistanceUnit.MILES)).setFrom(0)
.execute().actionGet();
In below code getting error to create GeoHashUtils class
afb.add(QueryBuilders.geoHashCellQuery("location",
GeoHashUtils.stringEncode(longitude, latitude), true).precision(
Constants.NEARBY_SCHOOL_PRECISION));
This is the full code
@Override
public List<GreatSchoolBaseInfo> getNearbySchoolInfoListByLatLng(
double latitude, double longitude, int mile, int size, String schoolId) {
List<GreatSchoolBaseInfo> nearbySchoolInfoList = new ArrayList<GreatSchoolBaseInfo>();
//AndQueryBuilder afb = QueryBuilders.andQuery();
QueryBuilder afb = null;
if (StringUtils.isNotBlank(schoolId)) {
afb = QueryBuilders.boolQuery().mustNot(QueryBuilders.termQuery("_id", schoolId));
//afb.add(QueryBuilders.boolQuery().mustNot(QueryBuilders.termQuery("_id", schoolId)));
}
afb.add(QueryBuilders.geoHashCellQuery("location",
GeoHashUtils.stringEncode(longitude, latitude), true).precision(
Constants.NEARBY_SCHOOL_PRECISION));
/* QueryBuilders.geoBoundingBoxQuery("pin.location")
.setCorners(longitude,latitude); */
// QueryBuilders.geoBoundingBoxQuery("location");
afb.add(QueryBuilders.geoDistanceQuery("location").lat(latitude).lon(longitude)
.distance(mile, DistanceUnit.MILES).optimizeBbox("memory"));
//FilteredQueryBuilder query = QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), afb);
QueryBuilder query = QueryBuilders.boolQuery().filter(afb);
SearchResponse sr = null;
try {
sr = esClient
.prepareSearch()
.setIndices(this.esSchoolIndex)
.setQuery(query)
.setSize(size)
.addSort(
SortBuilders.geoDistanceSort("location")
.point(latitude, longitude)
.order(SortOrder.ASC)
.unit(DistanceUnit.MILES)).setFrom(0)
.execute().actionGet();
} catch (Exception e) {
logger.error(e.toString(), e.fillInStackTrace());
return nearbySchoolInfoList;
}
for (SearchHit e : sr.getHits()) {
BigDecimal distance = new BigDecimal(0);
if (e.getSortValues() != null && e.getSortValues().length > 0) {
if (NumberUtils.isBigDecimal(e.getSortValues()[0].toString())) {
distance = new BigDecimal(e.getSortValues()[0].toString());
}
}
Map<Class<?>, ObjectFactory> params = new HashMap<Class<?>, ObjectFactory>();
params.put(Boolean.class, new ObjectFactory() {
@Override
public Object instantiate(ObjectBinder context, Object value,
Type targetType, Class targetClass) {
return value != null ? new Boolean(value.toString())
: new Boolean(false);
}
});
params.put(GeoPoint.class, new ObjectFactory() {
@Override
public Object instantiate(ObjectBinder context, Object value,
Type targetType, Class targetClass) {
return new GeoPoint(((JsonNumber) ((Map) value).get("lat"))
.doubleValue(), ((JsonNumber) ((Map) value)
.get("lon")).doubleValue());
}
});
GreatSchoolBaseInfo nearbySchool = JsonUtils.deserialize(
GreatSchoolBaseInfo.class, e.getSourceAsString(), params);
nearbySchool.setDistance(distance);
nearbySchoolInfoList.add(nearbySchool);
}
return nearbySchoolInfoList;
}