Filtering search results by location with the Geo Point data type
The Geo Point data type lets you index geographic coordinates (latitude and longitude) for each page, so you can filter search results by distance from a location. A common use case is a store, office, or location finder where visitors only want results within a certain radius of where they are.
Geo filtering is available for custom (API) and CludoJS implementations. It is not configurable through the standard template UI, so you will be working with the search script or directly with the Search API.
1. Configure a Geo Point field in the crawler
First, the crawler needs to extract the coordinates from each page and store them as a Geo Point. In Configuration > Crawlers, edit your crawler and add a new field:
- Give the field a unique name (for example
GeoLocation). - Set a pattern that extracts the coordinates from the page.
- Select GeoPoint as the data type.
The coordinates must be in decimal degrees (the format without the degree symbol). Latitude ranges from -90 to 90 and longitude from -180 to 180. The crawler accepts the values separated by a comma or a semicolon, for example 55.638916,12.5514433 or 55.638916;12.5514433.
Once indexed, the field name gets a _geo_point suffix. A field you named GeoLocation is stored and queried as GeoLocation_geo_point. We recommend using the Test Crawler feature to confirm the coordinates are extracted correctly before running a full crawl.
Finally, add the Geo Point field to your engine facets so it can be used as a filter: edit the engine > List engine facets > Add engine facet, and select your Geo Point field.
2. Filter results within a radius
Geo filtering uses a geo_distance filter that takes five values, in this order: the field name (with the _geo_point suffix), the latitude, the longitude, the distance, and the distance unit.
In a CludoJS implementation, add it to the filters object in your cludoSettings:
filters: {"geo_distance": ["GeoLocation_geo_point", "55.638916", "12.5514433", "100", "kilometers"]}
If you are calling the Search API directly, use the same shape inside the request body’s filters object. Standard query terms and other filters can be applied alongside the geo filter.
The supported distance units are:
- Kilometers:
kilometers - Meters:
meters - Centimeters:
centimeters - Millimeters:
millimeters - Miles:
miles - Yards:
yards - Feet:
feet - Inches:
inch - Nautical miles:
nauticalmiles
In an API implementation you can also sort results so the nearest match appears first, either on its own or combined with the radius filter, while still applying your normal query and filters. If you would like help setting up proximity sorting for your engine, reach out to us and we will get you the exact request setup for your implementation.
3. Use the visitor’s location
The coordinates in the filter are the center point of the search. To search around the visitor, get their location on your side first and pass it into the filter. The browser’s built-in geolocation API is the most common way to do this:
navigator.geolocation.getCurrentPosition(function (position) {
var lat = position.coords.latitude;
var lon = position.coords.longitude;
CludoSearch.filter("geo_distance", ["GeoLocation_geo_point", lat, lon, "100", "kilometers"]);
});
Calling CludoSearch.filter() updates the geo filter and re-runs the search, so you can also wire it to an input where the visitor enters a location or changes the radius after the first search. Note that the browser asks the visitor for permission before sharing their location, so handle the case where permission is declined.