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
+56-11Lines changed: 56 additions & 11 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
+
In order to start filtering and sorting documents based on their geographic location, you must make sure they contain a valid `_geo`or `_geojson`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 that cross the 180th or antimeridian.
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,7 +171,7 @@ 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
+25-6Lines changed: 25 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -481,9 +481,9 @@ 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:
`_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
505
506
506
```
507
-
_geoBoundingBox([{lat}, {lng}], [{lat}, {lng}])
507
+
_geoBoundingBox([LAT, LNG], [LAT, LNG])
508
508
```
509
509
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.
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.
511
511
512
512
<CodeSamplesGeosearchGuideFilterUsage3 />
513
513
514
514
Meilisearch will throw an error if the top right corner is under the bottom left corner.
515
515
516
516
</Tab>
517
517
518
+
<Tabtitle="_geoPolygon">
519
+
`_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.
526
+
527
+
<CodeSamplesGeosearchGuideFilterUsage4 />
528
+
529
+
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.
530
+
531
+
`_geoPolygon` is not compatible with documents using only `_geo` data. You must specify a `_geojson` attribute to use `_geoPolygon`.
532
+
533
+
</Tab>
534
+
518
535
</Tabs>
519
536
520
537
If any parameters are invalid or missing, Meilisearch returns an [`invalid_search_filter`](/reference/errors/error_codes#invalid_search_filter) error.
@@ -924,7 +941,7 @@ You can search for science fiction books ordered from cheapest to most expensive
924
941
925
942
#### Sorting results with `_geoPoint`
926
943
927
-
When dealing with documents containing geolocation data, you can use `_geoPoint` to sort results based on their distance from a specific geographic location.
944
+
When dealing with documents containing `_geo` data, you can use `_geoPoint` to sort results based on their distance from a specific geographic location.
928
945
929
946
`_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
947
@@ -946,7 +963,9 @@ Queries using `_geoPoint` will always include a `geoDistance` field containing t
946
963
]
947
964
```
948
965
949
-
[You can read more about location-based sorting in our dedicated guide.](/learn/filtering_and_sorting/geosearch#sorting-results-with-_geopoint)
966
+
Geographic sorting is only compatible with documents containing `_geo` data. `_geoPoint` ignores all data in the `_geojson` object.
967
+
968
+
[You can read more about location-based sorting in the dedicated guide.](/learn/filtering_and_sorting/geosearch#sorting-results-with-_geopoint)
0 commit comments