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


(javag) #1

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

(David Pilato) #2

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?


(javag) #3

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")


(David Pilato) #4

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

This is not going to work.


(javag) #5

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


(David Pilato) #6

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


(javag) #7

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

(javag) #9
 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"


(javag) #10

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")


(David Pilato) #11

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).


(javag) #12

It looks like elasticsearch 5.6 documentation?


(David Pilato) #13

Yes. And?


(javag) #14

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"));

(David Pilato) #15

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

Depreciation does not mean removal.


(javag) #16

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"));

(David Pilato) #17

I guess something like:

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

(javag) #18

this is working

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

but have to apply
.optimizeBbox("memory")


(David Pilato) #19

Why? It does not exist.


(javag) #20
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


(javag) #21

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