Geosearch
Filter and sort search results based on their geographic location.
Meilisearch allows you to filter and sort results based on their geographic location. This can be useful when you only want results within a specific area or when sorting results based on their distance from a specific location.
Due to Meilisearch allowing malformed _geo
fields in the following versions (v0.27, v0.28 and v0.29), please ensure the _geo
field follows the correct format.
Preparing documents for location-based search
In order to start filtering and sorting documents based on their geographic location, you must make sure they contain a valid _geo
field.
_geo
is a reserved field. If you include it 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:
Examples
Suppose we have a JSON array containing a few restaurants:
Our restaurant dataset looks like this once we add geopositioning data:
Trying to index a dataset with one or more documents containing badly formatted _geo
values will cause Meilisearch to throw an invalid_document_geo_field
error. In this case, the update will fail and no documents will be added or modified.
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:
Filtering results with _geoRadius
and _geoBoundingBox
You can use _geo
data to filter queries so you only receive results located within a given geographic area.
Configuration
In order to filter results based on their location, you must add the _geo
attribute to the filterableAttributes
list:
Meilisearch will rebuild your index whenever you update 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 the filter
search parameter along with _geoRadius
or _geoBoundingBox
. These are special filter rules that ensure Meilisearch only returns results located within a specific geographic area.
_geoRadius
_geoBoundingBox
Examples
Using our example dataset, we can search for places to eat near the center of Milan with _geoRadius
:
We also make a similar query using _geoBoundingBox
:
It is also possible to combine _geoRadius
and _geoBoundingBox
with other filters. We can narrow down our previous search so it only includes pizzerias:
_geo
, _geoDistance
, and _geoPoint
are not valid filter rules. Trying to use any of them with the filter
search parameter will result in an invalid_search_filter
error.
Sorting results with _geoPoint
Configuration
Before using geosearch for sorting, you must add the _geo
attribute to the sortableAttributes
list:
Read more about sortableAttributes
here.
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:
With our restaurants dataset, the results look like this:
_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:
Was this page helpful?