Preparing documents for location-based search
To start filtering documents based on their geographic location, you must make sure they contain a valid_geo or _geojson field. If you also want to sort documents geogeraphically, they must have a valid _geo field.
_geo and _geojson are reserved fields. If you include one of them in your documents, Meilisearch expects its value to conform to a specific format.
When using JSON and NDJSON, _geo must contain an object with two keys: lat and lng. Both fields must contain either a floating point number or a string indicating, respectively, latitude and longitude:
_geojson must be an object whose contents follow the GeoJSON specification:
MultiPolygon or MultiLine. Transmeridian shapes are polygons or lines that cross the 180th meridian.
Meilisearch does not support polygons with holes. If your polygon consists of an external ring and an inner empty space, Meilisearch ignores the hole and treats the polygon as a solid shape.
Using _geo and _geojson together
If your application requires both sorting by distance to a point and filtering by shapes other than a circle or a rectangle, you will need to add both _geo and _geojson to your documents.When handling documents with both fields, Meilisearch:- Ignores
_geojsonvalues when sorting - Ignores
_geovalues when filtering with_geoPolygon - Matches both
_geoand_geojsonvalues when filtering with_geoRadiusand_geoBoundingBox
Examples
Suppose we have a JSON array containing a few restaurants:_geo data:
Using _geo with CSV
If your dataset is formatted as CSV, the file header must have a _geo column. Each row in the dataset must then contain a column with a comma-separated string indicating latitude and longitude:
_geojson attribute.
Filtering results with _geoRadius, _geoBoundingBox, and _geoPolygon
You can use _geo and _geojson data to filter queries so you only receive results located within a given geographic area.
Configuration
To filter results based on their location, you must add_geo or _geojson to the filterableAttributes list:
filterableAttributes. Depending on the size of your dataset, this might take a considerable amount of time.
You can read more about configuring filterableAttributes in our dedicated filtering guide.
Usage
Use thefilter search parameter along with _geoRadius and _geoBoundingBox. These are special filter rules that ensure Meilisearch only returns results located within a specific geographic area. If you are using GeoJSON for your documents, you may also filter results with _geoPolygon.
_geoRadius
_geoBoundingBox
_geoPolygon
Examples
Using our example dataset, we can search for places to eat near the center of Milan with_geoRadius:
_geoBoundingBox:
_geoPolygon:
_geoRadius, _geoBoundingBox, and _geoPolygon with other filters. We can narrow down our previous search so it only includes pizzerias:
Sorting results with _geoPoint
Configuration
Before using geosearch for sorting, you must add the_geo attribute to the sortableAttributes list:
It is not possible to sort documents based on the
_geojson attribute.Usage
Examples
The_geoPoint sorting function can be used like any other sorting rule. We can order documents based on how close they are to the Eiffel Tower:
_geoPoint also works when used together with other sorting rules. We can sort restaurants based on their proximity to the Eiffel Tower and their rating: