geoDistanceSort(String, GeoPoint[]) is ambiguous for the type SortBuilders

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;
    }

AFAIK SortBuilders.geoDistanceSort("foo") does not compile at all in 6.1.
Not sure how you are getting that but may be you have 2 JARs in your classpath, one with 5.x the other with 6.1?

Yes i have 2 jars in classpath , one with 5.5 and other with 6.10.

Please let me know is there any alternative for

SortBuilders.geoDistanceSort("abc")

and
GeoHashUtils.stringEncode("abc")

Yes i have 2 jars in classpath , one with 5.5 and other with 6.10.

This is not going to work.

I removed 5.5 then using 6.10, next how to proceed?

Your favorite Java IDE will tell you what are the methods available... At least, mine does that.

Anyway, here we go:

https://artifacts.elastic.co/javadoc/org/elasticsearch/elasticsearch/6.1.2/org/elasticsearch/search/sort/SortBuilders.html#method.summary

I tried to modify my code but i am not successfully, removed 5.5 jar and using 6.1.0 jar. please help me.

    if (StringUtils.isNotBlank(schoolId)) {
            	afb = QueryBuilders.boolQuery().mustNot(QueryBuilders.termQuery("_id", schoolId));
            	//afb.add(QueryBuilders.boolQuery().mustNot(QueryBuilders.termQuery("_id", schoolId)));
            }
           /// actual code 
 afb.add(QueryBuilders.geoHashCellQuery("location",
                    GeoHashUtils.stringEncode(longitude, latitude), true).precision(
                    Constants.NEARBY_SCHOOL_PRECISION));*/
              //modified code, stuck up here
            afb = QueryBuilders.geoBoundingBoxQuery("location").setCorners(40.73, -74.1, 40.73, -74.1);
          
          //actual code  afb.add(QueryBuilders.geoDistanceQuery("location").lat(latitude).lon(longitude)
                    .distance(mile, DistanceUnit.MILES).optimizeBbox("memory"));*/
            //modified code, stuck up here
            afb = QueryBuilders.geoBoundingBoxQuery("location").setCorners(40.73, -74.1, 40.73, -74.1);
 afb.add(QueryBuilders.geoHashCellQuery("location",
                GeoHashUtils.stringEncode(longitude, latitude), true).precision(
                Constants.NEARBY_SCHOOL_PRECISION));
        
        afb.add(QueryBuilders.geoDistanceQuery("location")
                .lat(latitude).lon(longitude)
                .distance(mile, DistanceUnit.MILES).optimizeBbox("memory"));

Thanks for your quick responses.

The above 2 lines are the issue makers, so i followed this link,
https://www.elastic.co/guide/en/elasticsearch/reference/6.x/query-dsl-geohash-cell-query.html
and
https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-geo-queries.html

Please let me know what method to replace geoHashCellQuery() and geoDistanceQuery() is not accepting "The method lat(double) is undefined for the type GeoDistanceQueryBuilder"

Able to resolve this

afb = QueryBuilders.geoDistanceQuery("location").point(latitude, longitude).distance(mile, DistanceUnit.MILES);

But don't know how to do this part on the above code

.optimizeBbox("memory")

This option has been removed. See https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-geo-distance-query.html (it's marked as Deprecated in 2.2).

It looks like elasticsearch 5.6 documentation?

Yes. And?

If that method is depreciated then why they are documenting it in 5.6 documentation?

Please help me in resolving this issue

afb.add(QueryBuilders.geoHashCellQuery("location",
                GeoHashUtils.stringEncode(longitude, latitude), true).precision(
                Constants.NEARBY_SCHOOL_PRECISION));
        
        afb.add(QueryBuilders.geoDistanceQuery("location")
                .lat(latitude).lon(longitude)
                .distance(mile, DistanceUnit.MILES).optimizeBbox("memory"));

Because it has not been removed in 5.6 but in 6.x.

Depreciation does not mean removal.

Ok Thanks for your reply.

afb.add(QueryBuilders.geoHashCellQuery("location",
GeoHashUtils.stringEncode(longitude, latitude), true).precision(
Constants.NEARBY_SCHOOL_PRECISION));

above code modified like this

afb = QueryBuilders.geoBoundingBoxQuery("location").setCorners(40.73, -74.1, 40.73, -74.1);

but unable to do this part, please suggest me.

true).precision(
Constants.NEARBY_SCHOOL_PRECISION));

what is the replacement method for geoDistanceQuery()

afb.add(QueryBuilders.geoDistanceQuery("location")
                .lat(latitude).lon(longitude)
                .distance(mile, DistanceUnit.MILES).optimizeBbox("memory"));

I guess something like:

afb.add(QueryBuilders.geoDistanceQuery("location")
                .point(latitude, longitude)
                .distance(mile, DistanceUnit.MILES));

this is working

afb = (QueryBuilders.geoDistanceQuery("location")
                        .point(latitude, longitude)
                        .distance(mile, DistanceUnit.MILES));

but have to apply
.optimizeBbox("memory")

Why? It does not exist.

QueryBuilder qb = geoDistanceQuery("pin.location")  
    .point(40, -70)                                 
    .distance(200, DistanceUnit.KILOMETERS)         
    .optimizeBbox("memory")                         
    .geoDistance(GeoDistance.ARC); 

https://www.elastic.co/guide/en/elasticsearch/client/java-api/2.4/java-geo-queries.html

Do we need to use this url?

https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-geo-queries.html#java-query-dsl-geo-bounding-box-query