Search entities within a geographic distance with Spring Data Elasticsearch 4
A couple of months ago I published the post Using geo-distance sort in Spring Data Elasticsearch 4. In the comments there came up the question “What about searching within a distance?”
Well, this is not supported by query derivation from the method name, but it can easily be done with a custom repository implementation (see the documentation for more information about that).
I updated the example – which is available on GitHub – and will explain what is needed for this implementation. I won’t describe the entity and setup, please check the original post for that.
The custom repository interface
First we need to define a new repository interface that defines the method we want to provide:
public interface FoodPOIRepositoryCustom {
/**
* search all {@link FoodPOI} that are within a given distance of a point
*
* @param geoPoint
* the center point
* @param distance
* the distance
* @param unit
* the distance unit
* @return the found entities
*/
List<SearchHit<FoodPOI>> searchWithin(GeoPoint geoPoint, Double distance, String unit);
}
The custom repository implementation
Next we need to provide an implementation, important here is that this is named like the interface with the suffix “Impl”:
public class FoodPOIRepositoryCustomImpl implements FoodPOIRepositoryCustom {
private final ElasticsearchOperations operations;
public FoodPOIRepositoryCustomImpl(ElasticsearchOperations operations) {
this.operations = operations;
}
@Override
public List<SearchHit<FoodPOI>> searchWithin(GeoPoint geoPoint, Double distance, String unit) {
Query query = new CriteriaQuery(
new Criteria("location").within(geoPoint, distance.toString() + unit)
);
// add a sort to get the actual distance back in the sort value
Sort sort = Sort.by(new GeoDistanceOrder("location", geoPoint).withUnit(unit));
query.addSort(sort);
return operations.search(query, FoodPOI.class).getSearchHits();
}
}
In this implementation we have an ElasticsearchOperations
instance injected by Spring. In the method implementation we build a NativeSearchQuery
that specifies the distance query we want. In addition to that we add a GeoDistanceSort
to have the actual distance of the found entities in the output. We pass this query to the ElasticsearchOperations
instance and return the search result.
Adapt the repository
We need to add the new interface to our FoodPOIRepository
definition, which otherwise is unchanged:
public interface FoodPOIRepository extends ElasticsearchRepository<FoodPOI, String>, FoodPOIRepositoryCustom {
List<SearchHit<FoodPOI>> searchTop3By(Sort sort);
List<SearchHit<FoodPOI>> searchTop3ByName(String name, Sort sort);
}
Use it in the controller
In the rest controller, there is a new method that uses the distance search:
@PostMapping("/within")
List<ResultData> withinDistance(@RequestBody RequestData requestData) {
GeoPoint location = new GeoPoint(requestData.getLat(), requestData.getLon());
List<SearchHit<FoodPOI>> searchHits
= repository.searchWithin(location, requestData.distance, requestData.unit);
return toResultData(searchHits);
}
private List<ResultData> toResultData(List<SearchHit<FoodPOI>> searchHits) {
return searchHits.stream()
.map(searchHit -> {
Double distance = (Double) searchHit.getSortValues().get(0);
FoodPOI foodPOI = searchHit.getContent();
return new ResultData(foodPOI.getName(), foodPOI.getLocation(), distance);
}).collect(Collectors.toList());
}
We extract the needed parameters from the requestData
that came in, call our repository method and convert the results to our output format.
And that’s it
So with a small custom repository implementation we were able to add the desired functionality to our repository