I am passing 200 in distance to see nearby, but geonear is checking with values less than 1.0000, so all my documents are returning from MongoDB

Viewed 51
    Double latitude = requestDTO.getLatitude();
    Double longitude = requestDTO.getLongitude();
    String countryId = requestDTO.getCountryId();
    String status = requestDTO.getStatusOfILM();

    Point point = new Point(longitude, latitude);
    String geoSpatialQueryForLine = "{countryId:'" + countryId + "'},{ilmState:'" + status + "'}";
    Query query = new BasicQuery(geoSpatialQueryForLine);
    List<AggregationOperation> list = new ArrayList<>();
    NearQuery nearQuery = NearQuery.near(point).query(query).maxDistance(distance).minDistance(0).spherical(true);
    list.add(Aggregation.geoNear(nearQuery, RestServiceConfig.SITE_TO_LINE_DISTANCE));
    list.add(Aggregation.project(RestServiceConfig.ID_KEY, RestServiceConfig.SITE_TO_LINE_DISTANCE));
    TypedAggregation<TariffLineDocument> agg = new TypedAggregation<>(TariffLineDocument.class, list);
    List<TariffLineDocument> result = mongoOperations.aggregate(agg, TariffLineDocument.class).getMappedResults();
    for (TariffLineDocument document : result) {
        TariffDocument tariffDocument = TariffDocument.builder()
                .tariffLayerId(document.getId())
                .layerType(RestServiceConfig.LINE)
                .siteToLineDistance(document.getSiteToLineDistance())
                .build();
        documentList.add(tariffDocument);
    }
    logger.info(RestServiceConfig.NUMBER_OF_LINE_SHAPES, documentList.size());
} catch (Exception exception) {
    logger.info(RestServiceConfig.ERROR_IN_METHOD, exception.getMessage());
}
return documentList;

This is my Java code. Here, sitetoline is returning as "siteToLineDistance": 0.004334109519106326, so all the documents have sitetoline within this range. As maxdistance is 200, every document is fetched. I want to only fetch documents which is in maxdistance range, i.e. 200 and I don't know what sitetolinedistance is in km/meters.

In what measures it is returning the sitetolinedistance like meters, km, miles, etc?
Output:

enter image description here

1 Answers

Seems need to provide metrics

for example

    NearQuery nearQuery = NearQuery.near(point).query(query).maxDistance(distance).minDistance(0).spherical(true);

There are several metric types in spring geo

public enum Metrics implements Metric {

    KILOMETERS(6378.137, "km"), MILES(3963.191, "mi"), NEUTRAL(1, "");
   // ...
}

so by default NEUTRAL is used, with multiplier equals to 1. In that case distance in between (0.0;0.0) and (1.0; 0.0) will be 1, not a hundreds of kms

Related