Geo-search
Geo-searching lets you:- Filter and sort around a set of latitude and longitude coordinates
- Filter and sort around user location based on their IP address (IPv4 only)
- Filter by one or more box-shaped geographical areas (bounded boxes)
- Filter by one or more freely drawn geographical areas (polygons)
Enabling geo-search by adding geolocation data to records
To enable geo-searching, you must have geolocation data in your records as a_geoloc
attribute that contains latitude and longitude (lat
and lng
properties). The coordinates must be numbers, not strings.
Here’s an example of a record with one geolocation:
JSON
JSON
Geographical filtering and sorting
Geo-searching filters results based on a set of geographic coordinates. You can define one or more geographic areas: the engine will only return records within that area. You can refine this behavior by changing boundaries, distance, or precision.Filter around a central point
You can define a central geographical point and show only records within a certain radius of that point. This central point is defined in one of two ways:- Sent with the query
- Derived from user IP address (supports only IPv4)
- Filter results around a location
- Understand the engine’s geographical ranking
aroundLatLng
aroundLatLngViaIP
Increase the search radius
The default behavior is to stop finding records after approximately 1,000 matching records. Therefore, your search radius could be quite small in dense areas. Imagine the number of cafés in Paris: 1,000 matching records wouldn’t be enough to list them all. You can override this behavior by forcing a larger radius, either by usingaroundRadius
or minimumAroundRadius
.
This override only applies to radius-based searches, not area-based ones like rectangles and polygons.
Even with a larger radius, the results still match any applied query or filter.
The number of results can be low if the query or filters are restrictive, even if you set a huge radius.
Sorting by distance
If you want to sort but not filter results based on radial distance, set thearoundRadius
parameter to all
.
This means your records are ordered but not restricted based on distance from a provided point.
Filtering inside rectangular or polygonal areas
You can restrict results to a given area by setting geometric boundaries in either the shape of a rectangle or other polygon. To restrict results within a given area, you should include thelat
and lng
of a bounding shape in the search parameters. The engine excludes all results outside of the shape.
Filtering within or around an area doesn’t affect the ranking of results.
Additionally, area-based geo-searching doesn’t work in combination with radius-based geo-searching.
When filtering on rectangular or polygonal areas, the engine considers all results within the area as being geographically equal.
To learn more, see:
- Filter results inside a rectangle
- Filter results inside a polygon
- How to solve the 180th meridian issue
Don’t mix bounding boxes and polygons
You can’t use
insideBoundingBox
and insidePolygon
together. If you provide both, only insideBoundingBox
actions will run.- You use a rectangular bounding box to display the South West region of the US. Blue pins could indicate cities where you have restaurants.
- As users zoom in, you want polygons to mark the boundaries of these cities. Red pins could mark the locations of individual restaurants within the polygons.
insideBoundingBox
takes precedence,
you’ll only see blue pins on the map.
For this reason, using a mix of bounding boxes and polygons isn’t recommended.
Geographical ranking
Geographical ranking is based on the distance between the geolocation in the search query and the best-matching geolocation in the record. For example, when searching around a location, the results can be sorted by distance - the closer the record is to thelat
/lng
you provided, the higher it ranks in the results.
Geo criterion in the ranking formula
Algolia’s ranking criteria contain a Geo criterion, which by default is the second-ranking criterion. This criterion is only meaningful when records contain the_geoloc
attribute with the corresponding lat
and lng
properties.