Skip to content

Commit 61c3080

Browse files
Add _geoPolygon and _geojson documentation (#3360)
1 parent 8c7ebb6 commit 61c3080

File tree

7 files changed

+109
-25
lines changed

7 files changed

+109
-25
lines changed

.code-samples.meilisearch.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,11 @@ geosearch_guide_filter_usage_3: |-
685685
-X POST 'MEILISEARCH_URL/indexes/restaurants/search' \
686686
-H 'Content-type:application/json' \
687687
--data-binary '{ "filter": "_geoBoundingBox([45.494181, 9.214024], [45.449484, 9.179175])" }'
688+
geosearch_guide_filter_usage_4: |-
689+
curl \
690+
-X POST 'MEILISEARCH_URL/indexes/restaurants/search' \
691+
-H 'Content-type:application/json' \
692+
--data-binary '{ "filter": "_geoPolygon([45.494181, 9.214024], [45.449484, 9.179175], [45.449486, 9.179177])" }'
688693
geosearch_guide_sort_settings_1: |-
689694
curl \
690695
-X PUT 'MEILISEARCH_URL/indexes/restaurants/settings/sortable-attributes' \

learn/filtering_and_sorting/geosearch.mdx

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ Due to Meilisearch allowing malformed `_geo` fields in the following versions (v
2121

2222
## Preparing documents for location-based search
2323

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.
2525

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.
2727

2828
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:
2929

@@ -37,6 +37,37 @@ When using JSON and NDJSON, `_geo` must contain an object with two keys: `lat` a
3737
}
3838
```
3939

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+
4071
### Examples
4172

4273
Suppose we have a JSON array containing a few restaurants:
@@ -67,7 +98,7 @@ Suppose we have a JSON array containing a few restaurants:
6798
]
6899
```
69100

70-
Our restaurant dataset looks like this once we add geopositioning data:
101+
Our restaurant dataset looks like this once we add `_geo` data:
71102

72103
```json
73104
[
@@ -122,13 +153,15 @@ If your dataset is formatted as CSV, the file header must have a `_geo` column.
122153
"3","Artico Gelateria Tradizionale","Via Dogana, 1, 20123 Milan, Italy","ice cream",10,"48.8826517,2.3352748"
123154
```
124155

125-
## Filtering results with `_geoRadius` and `_geoBoundingBox`
156+
CSV files do not support the `_geojson` attribute.
157+
158+
## Filtering results with `_geoRadius`, `_geoBoundingBox`, and `_geoPolygon`
126159

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.
128161

129162
### Configuration
130163

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:
132165

133166
<CodeSamplesGeosearchGuideFilterSettings1 />
134167

@@ -138,18 +171,24 @@ Meilisearch will rebuild your index whenever you update `filterableAttributes`.
138171

139172
### Usage
140173

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`.
142175

143176
### `_geoRadius`
144177

145178
```
146-
_geoRadius(lat, lng, distance_in_meters)
179+
_geoRadius(lat, lng, distance_in_meters, resolution)
147180
```
148181

149182
### `_geoBoundingBox`
150183

151184
```
152-
_geoBoundingBox([{lat}, {lng}], [{lat}, {lng}])
185+
_geoBoundingBox([LAT, LNG], [LAT, LNG])
186+
```
187+
188+
### `_geoPolygon`
189+
190+
```
191+
_geoPolygon([LAT, LNG], [LAT, LNG], [LAT, LNG], …)
153192
```
154193

155194
### Examples
@@ -162,6 +201,10 @@ We also make a similar query using `_geoBoundingBox`:
162201

163202
<CodeSamplesGeosearchGuideFilterUsage3 />
164203

204+
And with `_geoPolygon`:
205+
206+
<CodeSamplesGeosearchGuideFilterUsage4 />
207+
165208
```json
166209
[
167210
{
@@ -189,7 +232,7 @@ We also make a similar query using `_geoBoundingBox`:
189232
]
190233
```
191234

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:
193236

194237
<CodeSamplesGeosearchGuideFilterUsage2 />
195238

@@ -217,11 +260,13 @@ It is also possible to combine `_geoRadius` and `_geoBoundingBox` with other fil
217260

218261
### Configuration
219262

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):
221264

222265
<CodeSamplesGeosearchGuideSortSettings1 />
223266

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.
269+
</Danger>
225270

226271
### Usage
227272

reference/api/search.mdx

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -481,20 +481,23 @@ You can then use the filter in a search query:
481481

482482
<CodeSamplesFacetedSearchWalkthroughFilter1 />
483483

484-
#### Filtering results with `_geoRadius` and `_geoBoundingBox`
484+
#### Filtering results with `_geoRadius`, `_geoBoundingBox`, and `_geoPolygon`
485485

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:
487487

488488
<Tabs>
489489

490490
<Tab title="_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`.
492492

493493
```json
494-
_geoRadius(lat, lng, distance_in_meters)
494+
_geoRadius(lat, lng, distance_in_meters, resolution)
495495
```
496496

497-
`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.
500+
498501

499502
<CodeSamplesGeosearchGuideFilterUsage1 />
500503

@@ -504,17 +507,34 @@ _geoRadius(lat, lng, distance_in_meters)
504507
`_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:
505508

506509
```
507-
_geoBoundingBox([{lat}, {lng}], [{lat}, {lng}])
510+
_geoBoundingBox([LAT, LNG], [LAT, LNG])
508511
```
509512

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.
511514

512515
<CodeSamplesGeosearchGuideFilterUsage3 />
513516

514517
Meilisearch will throw an error if the top right corner is under the bottom left corner.
515518

516519
</Tab>
517520

521+
<Tab title="_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:
523+
524+
```
525+
_geoPolygon([LAT, LNG], [LAT, LNG], [LAT, LNG], …)
526+
```
527+
528+
`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+
518538
</Tabs>
519539

520540
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
924944

925945
#### Sorting results with `_geoPoint`
926946

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.
928948

929949
`_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`):
930950

@@ -946,7 +966,9 @@ Queries using `_geoPoint` will always include a `geoDistance` field containing t
946966
]
947967
```
948968

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)
950972

951973
### Matching strategy
952974

reference/errors/error_codes.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,10 @@ This error occurs if:
212212

213213
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).
214214

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+
215219
## `invalid_export_url`
216220

217221
The export target instance URL is invalid or could not be reached.

snippets/samples/code_samples_facet_search_3.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ client.Index("books").FacetSearch(&meilisearch.FacetSearchRequest{
4949
```csharp C#
5050
var query = new SearchFacetsQuery()
5151
{
52-
FacetQuery = "c"
52+
FacetQuery = "c",
53+
ExhaustiveFacetCount: true
5354
};
5455
await client.Index("books").FacetSearchAsync("genres", query);
5556
```

snippets/samples/code_samples_getting_started_add_documents.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,14 @@ $client->index('movies')->addDocuments($movies);
7878
// <dependency>
7979
// <groupId>com.meilisearch.sdk</groupId>
8080
// <artifactId>meilisearch-java</artifactId>
81-
// <version>0.15.0</version>
81+
// <version>0.16.1</version>
8282
// <type>pom</type>
8383
// </dependency>
8484

8585
// For Gradle
8686
// Add the following line to the `dependencies` section of your `build.gradle`:
8787
//
88-
// implementation 'com.meilisearch.sdk:meilisearch-java:0.15.0'
88+
// implementation 'com.meilisearch.sdk:meilisearch-java:0.16.1'
8989

9090
// In your .java file:
9191
import com.meilisearch.sdk;
@@ -192,7 +192,7 @@ namespace Meilisearch_demo
192192
```text Rust
193193
// In your .toml file:
194194
[dependencies]
195-
meilisearch-sdk = "0.29.1"
195+
meilisearch-sdk = "0.30.0"
196196
# futures: because we want to block on futures
197197
futures = "0.3"
198198
# serde: required if you are going to use documents

snippets/samples/code_samples_rename_an_index_1.mdx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,11 @@ curl \
66
-H 'Content-Type: application/json' \
77
--data-binary '{ "uid": "INDEX_B" }'
88
```
9+
10+
```rust Rust
11+
curl \
12+
-X PATCH 'MEILISEARCH_URL/indexes/INDEX_A' \
13+
-H 'Content-Type: application/json' \
14+
--data-binary '{ "uid": "INDEX_B" }'
15+
```
916
</CodeGroup>

0 commit comments

Comments
 (0)