You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: learn/filtering_and_sorting/geosearch.mdx
+57-12Lines changed: 57 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,9 +21,9 @@ Due to Meilisearch allowing malformed `_geo` fields in the following versions (v
21
21
22
22
## Preparing documents for location-based search
23
23
24
-
In order to start filtering and sorting documents based on their geographic location, you must make sure they contain a valid `_geo` field.
24
+
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.
25
25
26
-
`_geo`is a reserved field. If you include it in your documents, Meilisearch expects its value to conform to a specific format.
26
+
`_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.
27
27
28
28
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:
29
29
@@ -37,6 +37,37 @@ When using JSON and NDJSON, `_geo` must contain an object with two keys: `lat` a
37
37
}
38
38
```
39
39
40
+
`_geojson` must be an object whose contents follow the [GeoJSON specification](https://geojson.org/):
41
+
42
+
```json
43
+
{
44
+
…
45
+
"_geojson": {
46
+
"type": "Feature",
47
+
"geometry": {
48
+
"type": "Point",
49
+
"coordinates": [0.0, 0.0]
50
+
}
51
+
}
52
+
}
53
+
```
54
+
55
+
Meilisearch does not support transmeridian shapes. If your document includes a transmeridian shape, split it into two separate shapes grouped as a `MultiPolygon` or `MultiLine`. Transmeridian shapes are polygons or lines that cross the 180th meridian.
56
+
57
+
**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.
58
+
59
+
<Note>
60
+
### Using `_geo` and `_geojson` together
61
+
62
+
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.
63
+
64
+
When handling documents with both fields, Meilisearch:
65
+
66
+
- Ignores `_geojson` values when sorting
67
+
- Ignores `_geo` values when filtering with `_geoPolygon`
68
+
- Matches both `_geo` and `_geojson` values when filtering with `_geoRadius` and `_geoBoundingBox`
69
+
</Note>
70
+
40
71
### Examples
41
72
42
73
Suppose we have a JSON array containing a few restaurants:
@@ -67,7 +98,7 @@ Suppose we have a JSON array containing a few restaurants:
67
98
]
68
99
```
69
100
70
-
Our restaurant dataset looks like this once we add geopositioning data:
101
+
Our restaurant dataset looks like this once we add `_geo` data:
71
102
72
103
```json
73
104
[
@@ -122,13 +153,15 @@ If your dataset is formatted as CSV, the file header must have a `_geo` column.
## Filtering results with `_geoRadius` and `_geoBoundingBox`
156
+
CSV files do not support the `_geojson` attribute.
157
+
158
+
## Filtering results with `_geoRadius`, `_geoBoundingBox`, and `_geoPolygon`
126
159
127
-
You can use `_geo` data to filter queries so you only receive results located within a given geographic area.
160
+
You can use `_geo`and `_geojson`data to filter queries so you only receive results located within a given geographic area.
128
161
129
162
### Configuration
130
163
131
-
In order to filter results based on their location, you must add the `_geo`attribute to the `filterableAttributes` list:
164
+
To filter results based on their location, you must add `_geo`or `_geojson` to the `filterableAttributes` list:
132
165
133
166
<CodeSamplesGeosearchGuideFilterSettings1 />
134
167
@@ -138,18 +171,24 @@ Meilisearch will rebuild your index whenever you update `filterableAttributes`.
138
171
139
172
### Usage
140
173
141
-
Use the [`filter` search parameter](/reference/api/search#filter) along with `_geoRadius`or`_geoBoundingBox`. These are special filter rules that ensure Meilisearch only returns results located within a specific geographic area.
174
+
Use the [`filter` search parameter](/reference/api/search#filter) 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`.
@@ -162,6 +201,10 @@ We also make a similar query using `_geoBoundingBox`:
162
201
163
202
<CodeSamplesGeosearchGuideFilterUsage3 />
164
203
204
+
And with `_geoPolygon`:
205
+
206
+
<CodeSamplesGeosearchGuideFilterUsage4 />
207
+
165
208
```json
166
209
[
167
210
{
@@ -189,7 +232,7 @@ We also make a similar query using `_geoBoundingBox`:
189
232
]
190
233
```
191
234
192
-
It is also possible to combine `_geoRadius`and `_geoBoundingBox` with other filters. We can narrow down our previous search so it only includes pizzerias:
235
+
It is also possible to combine `_geoRadius`, `_geoBoundingBox`, and `_geoPolygon` with other filters. We can narrow down our previous search so it only includes pizzerias:
193
236
194
237
<CodeSamplesGeosearchGuideFilterUsage2 />
195
238
@@ -217,11 +260,13 @@ It is also possible to combine `_geoRadius` and `_geoBoundingBox` with other fil
217
260
218
261
### Configuration
219
262
220
-
Before using geosearch for sorting, you must add the `_geo` attribute to the `sortableAttributes` list:
263
+
Before using geosearch for sorting, you must add the `_geo` attribute to the [`sortableAttributes` list](/learn/filtering_and_sorting/sort_search_results):
221
264
222
265
<CodeSamplesGeosearchGuideSortSettings1 />
223
266
224
-
[Read more about `sortableAttributes` here.](/learn/filtering_and_sorting/sort_search_results)
267
+
<Danger>
268
+
It is not possible to sort documents based on the `_geojson` attribute.
Copy file name to clipboardExpand all lines: reference/api/search.mdx
+31-9Lines changed: 31 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -481,20 +481,23 @@ You can then use the filter in a search query:
481
481
482
482
<CodeSamplesFacetedSearchWalkthroughFilter1 />
483
483
484
-
#### Filtering results with `_geoRadius`and `_geoBoundingBox`
484
+
#### Filtering results with `_geoRadius`, `_geoBoundingBox`, and `_geoPolygon`
485
485
486
-
If your documents contain `_geo` data, you can use the `_geoRadius` and `_geoBoundingBox`built-in filter rules to filter results according to their geographic position.
486
+
If your documents contain `_geo`or `_geojson`data, you can use the following built-in filter rules to filter results according to their geographic position:
487
487
488
488
<Tabs>
489
489
490
490
<Tabtitle="_geoRadius">
491
-
`_geoRadius` establishes a circular area based on a central point and a radius. This filter rule requires three parameters: `lat`, `lng` and `distance_in_meters`.
491
+
`_geoRadius` establishes a circular area based on a central point and a radius. This filter rule accepts the following parameters: `lat`, `lng`, `distance_in_meters`, `resolution`.
`lat` and `lng` should be geographic coordinates expressed as floating point numbers. `distance_in_meters` indicates the radius of the area within which you want your results and should be an integer.
497
+
-`lat` and `lng` should be geographic coordinates expressed as floating point numbers.
498
+
-`distance_in_meters` indicates the radius of the area within which you want your results and should be an integer.
499
+
-`resolution` must be an integer between `3` and `1000` inclusive, and is an optional parameter. When using `_geojson` coordinates, `resolution` sets how many points Meilisearch will use to create a polygon that approximates the shape of a circle. Documents using `_geo` data ignore this parameter. Defaults to `125`. Increasing `resolution` may result in performance issues and is only necessary when dealing with large country-sized circles.
`_geoBoundingBox` establishes a rectangular area based on the coordinates for its top right and bottom left corners. This filter rule requires two arrays of geographic coordinates:
505
508
506
509
```
507
-
_geoBoundingBox([{lat}, {lng}], [{lat}, {lng}])
510
+
_geoBoundingBox([LAT, LNG], [LAT, LNG])
508
511
```
509
512
510
-
`lat` and `lng` should be geographic coordinates expressed as floating point numbers. The first array indicates the top right corner and the second array indicates the bottom left corner of the bounding box.
513
+
`LAT` and `LNG` should be geographic coordinates expressed as floating point numbers. The first array indicates the top right corner and the second array indicates the bottom left corner of the bounding box.
511
514
512
515
<CodeSamplesGeosearchGuideFilterUsage3 />
513
516
514
517
Meilisearch will throw an error if the top right corner is under the bottom left corner.
515
518
516
519
</Tab>
517
520
521
+
<Tabtitle="_geoPolygon">
522
+
`_geoPolygon` establishes an area based on the coordinates of the specified points. This filter rule requires three arrays or more arrays of geographic coordinates and can only be used with GeoJSON documents:
`LAT` and `LNG` should be geographic coordinates expressed as floating point numbers. If your polygon is not closed, Meilisearch will close it automatically. Closed polygons are polygons where the first and last points share the same coordinates.
529
+
530
+
<CodeSamplesGeosearchGuideFilterUsage4 />
531
+
532
+
Polygons cannot cross the 180th meridian. If a shape crosses the antimeridian, you must make two polygons and join them using the `AND` filter operator.
533
+
534
+
`_geoPolygon` is not compatible with documents using only `_geo` data. You must specify a `_geojson` attribute to use `_geoPolygon`.
535
+
536
+
</Tab>
537
+
518
538
</Tabs>
519
539
520
540
If any parameters are invalid or missing, Meilisearch returns an [`invalid_search_filter`](/reference/errors/error_codes#invalid_search_filter) error.
@@ -924,7 +944,7 @@ You can search for science fiction books ordered from cheapest to most expensive
924
944
925
945
#### Sorting results with `_geoPoint`
926
946
927
-
When dealing with documents containing geolocation data, you can use `_geoPoint` to sort results based on their distance from a specific geographic location.
947
+
When dealing with documents containing `_geo` data, you can use `_geoPoint` to sort results based on their distance from a specific geographic location.
928
948
929
949
`_geoPoint` is a sorting function that requires two floating point numbers indicating a location's latitude and longitude. You must also specify whether the sort should be ascending (`asc`) or descending (`desc`):
930
950
@@ -946,7 +966,9 @@ Queries using `_geoPoint` will always include a `geoDistance` field containing t
946
966
]
947
967
```
948
968
949
-
[You can read more about location-based sorting in our dedicated guide.](/learn/filtering_and_sorting/geosearch#sorting-results-with-_geopoint)
969
+
Geographic sorting is only compatible with documents containing `_geo` data. `_geoPoint` ignores all data in the `_geojson` object.
970
+
971
+
[You can read more about location-based sorting in the dedicated guide.](/learn/filtering_and_sorting/geosearch#sorting-results-with-_geopoint)
Copy file name to clipboardExpand all lines: reference/errors/error_codes.mdx
+4Lines changed: 4 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -212,6 +212,10 @@ This error occurs if:
212
212
213
213
The provided `_geo` field of one or more documents is invalid. Meilisearch expects `_geo` to be an object with two fields, `lat` and `lng`, each containing geographic coordinates expressed as a string or floating point number. Read more about `_geo` and how to troubleshoot it in [our dedicated guide](/learn/filtering_and_sorting/geosearch).
214
214
215
+
## `invalid_document_geojson_field`
216
+
217
+
The `geojson` field in one or more documents is invalid or doesn't match the [GeoJSON specification](https://datatracker.ietf.org/doc/html/rfc7946).
218
+
215
219
## `invalid_export_url`
216
220
217
221
The export target instance URL is invalid or could not be reached.
0 commit comments