Java Spring boot Geocode nearby operation with pagination and additional filter

Is there not a easy method to get paginated end point for geocode nearby operation with spring jpa repository method

If i have a distributorType as not null, my code below takes too much time to run even with proper indexes

private GeoPage<Property> searchNearBy(String lat, String lng, double radius, Integer pageNumber, Integer pageSize, String distributorType) {
	Point centerPoint = new Point(Double.parseDouble(lng), Double.parseDouble(lat));
	Distance maxDistance = new Distance(radius, Metrics.KILOMETERS);
	PageRequest page = PageRequest.of(pageNumber, pageSize);
	if(distributorType == null || distributorType.isEmpty()) {
		return repository.findByGeoCodeNear(centerPoint, maxDistance, page);
	} else {
		GeoResults<Property> result = mongoOperations.query(Property.class)
				.as(Property.class)
				.near(NearQuery.near(centerPoint)
						.maxDistance(maxDistance)
						.query(new Query(Criteria.where("distributorType").is(distributorType)))
						.with(page))
				.all();
		long count = mongoOperations.count(new Query(Criteria.where("distributorType").is(distributorType).and("location").near(centerPoint).maxDistance(maxDistance.getValue())), Property.class);
		return new GeoPage<>(result, page, count);
	}
}

Hi – I see you mentioned Spring JPA. Have you tried Spring Data MongoDB? There is support for $geoNear, documentation here.

Hi Ashni,

Thanks for your reply, can you please simple code snippet to get GeoPage with nearBy operation and for a particular distributorType, i dont want to get GeoResult and then being forced to write separate count query