Geo Distance Search is used to search the data based on location or nearby location point. For example, If we want all the user details of our website in the area of 100 km from some location point then Geo Distance Search API is very useful.
Liferay 7/DXP provides Geo Distance Search API. In this blog, We will use this API to configure Geo Distance Search in Liferay Elastic Search.
Prerequisite:
Basic knowledge about Boolean query, Indexer
Steps to follow:
Step 1: Create Indexer of custom entity.
-
We have assumed that you have created your custom entity on which you want to apply Geo Distance Search on specific field.
-
We have also assumed that you have created the indexer of your custom entity
-
Create Indexer of your custom entity.
For more information, Please go through Liferay documentation for how to create custom indexer.
https://dev.liferay.com/develop/tutorials/-/knowledge_base/7-0/creating-an-entry-indexer
Step 2: Add geoLocation field in Indexer:
document.addGeoLocation(latitude, longitude);
addSearchTerm(searchQuery, searchContext, "geoLocation", false);
Step 3: Add the Geo Location Filter in your boolean query
GeoLocationPoint geoLocationPinPoint = new GeoLocationPoint(latitude, longitude);
GeoDistance geoDistance = new GeoDistance(geoDistanceValue, DistanceUnit.KILOMETERS);
GeoDistanceFilter geoDistanceFilter = new GeoDistanceFilter("geoLocation", geoLocationPinPoint, geoDistance);
SearchContext searchContext = SearchContextFactory.getInstance(request);
BooleanQuery booleanQuery = new BooleanQueryImpl();
...
BooleanFilter booleanFilter = new BooleanFilter();
booleanFilter.add(geoDistanceFilter);
booleanQuery.setPreBooleanFilter(booleanFilter);
...
try {
Hits hits = indexSearcher.search(searchContext, booleanQuery);
} catch (SearchException e) {
// Handle Exception
}
That’s it. You have configured Geo Distance functionality with Liferay7/DXP.
For professional paid support, you may contact us at [email protected] .