Skip to content

Commit 09fb9d2

Browse files
Add geodistance query documentation (opensearch-project#7607)
* Add geodistance query documentation Signed-off-by: Fanit Kolchina <kolchfa@amazon.com> * Add optional sentence Signed-off-by: Fanit Kolchina <kolchfa@amazon.com> * Fix links Signed-off-by: Fanit Kolchina <kolchfa@amazon.com> * More links Signed-off-by: Fanit Kolchina <kolchfa@amazon.com> * Fix a link Signed-off-by: Fanit Kolchina <kolchfa@amazon.com> * Apply suggestions from code review Co-authored-by: Nathan Bower <nbower@amazon.com> Signed-off-by: kolchfa-aws <105444904+kolchfa-aws@users.noreply.github.com> * Editorial comment Signed-off-by: Fanit Kolchina <kolchfa@amazon.com> --------- Signed-off-by: Fanit Kolchina <kolchfa@amazon.com> Signed-off-by: kolchfa-aws <105444904+kolchfa-aws@users.noreply.github.com> Co-authored-by: Nathan Bower <nbower@amazon.com>
1 parent 9442ac0 commit 09fb9d2

File tree

5 files changed

+199
-10
lines changed

5 files changed

+199
-10
lines changed

_api-reference/common-parameters.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,37 @@ The following request specifies filters to limit the fields returned in the resp
9090

9191
GET _search?filter_path=<field_name>.*,-<field_name>
9292
```
93+
94+
## Units
95+
96+
OpenSearch APIs support the following units.
97+
98+
### Time units
99+
100+
The following table lists all supported time units.
101+
102+
Units | Specify as
103+
:--- | :---
104+
Days | `d`
105+
Hours | `h`
106+
Minutes | `m`
107+
Seconds | `s`
108+
Milliseconds | `ms`
109+
Microseconds | `micros`
110+
Nanoseconds | `nanos`
111+
112+
### Distance units
113+
114+
The following table lists all supported distance units.
115+
116+
Units | Specify as
117+
:--- | :---
118+
Miles | `mi` or `miles`
119+
Yards | `yd` or `yards`
120+
Feet | `ft` or `feet`
121+
Inches | `in` or `inch`
122+
Kilometers | `km` or `kilometers`
123+
Meters | `m` or `meters`
124+
Centimeters | `cm` or `centimeters`
125+
Millimeters | `mm` or `millimeters`
126+
Nautical miles | `NM`, `nmi`, or `nauticalmiles`

_query-dsl/geo-and-xy/geo-bounding-box.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ PUT testindex1
3131
}
3232
}
3333
```
34+
{% include copy-curl.html %}
3435

3536
Index three geopoints as objects with latitudes and longitudes:
3637

@@ -42,15 +43,21 @@ PUT testindex1/_doc/1
4243
"lon": 40.71
4344
}
4445
}
46+
```
47+
{% include copy-curl.html %}
4548

49+
```json
4650
PUT testindex1/_doc/2
4751
{
4852
"point": {
4953
"lat": 72.64,
5054
"lon": 22.62
5155
}
5256
}
57+
```
58+
{% include copy-curl.html %}
5359

60+
```json
5461
PUT testindex1/_doc/3
5562
{
5663
"point": {
@@ -59,6 +66,7 @@ PUT testindex1/_doc/3
5966
}
6067
}
6168
```
69+
{% include copy-curl.html %}
6270

6371
Search for all documents and filter the documents whose points lie within the rectangle defined in the query:
6472

@@ -88,6 +96,7 @@ GET testindex1/_search
8896
}
8997
}
9098
```
99+
{% include copy-curl.html %}
91100

92101
The response contains the matching document:
93102

@@ -163,6 +172,7 @@ GET testindex1/_search
163172
}
164173
}
165174
```
175+
{% include copy-curl.html %}
166176

167177
## Request fields
168178

@@ -205,6 +215,7 @@ GET testindex1/_search
205215
}
206216
}
207217
```
218+
{% include copy-curl.html %}
208219

209220
To specify a bounding box that covers the whole area of a geohash, provide that geohash as both `top_left` and `bottom_right` parameters of the bounding box:
210221

@@ -227,4 +238,5 @@ GET testindex1/_search
227238
}
228239
}
229240
}
230-
```
241+
```
242+
{% include copy-curl.html %}

_query-dsl/geo-and-xy/geodistance.md

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
---
2+
layout: default
3+
title: Geodistance
4+
parent: Geographic and xy queries
5+
grand_parent: Query DSL
6+
nav_order: 20
7+
---
8+
9+
# Geodistance query
10+
11+
A geodistance query returns documents with geopoints that are within a specified distance from the provided geopoint. A document with multiple geopoints matches the query if at least one geopoint matches the query.
12+
13+
The searched document field must be mapped as `geo_point`.
14+
{: .note}
15+
16+
## Example
17+
18+
Create a mapping with the `point` field mapped as `geo_point`:
19+
20+
```json
21+
PUT testindex1
22+
{
23+
"mappings": {
24+
"properties": {
25+
"point": {
26+
"type": "geo_point"
27+
}
28+
}
29+
}
30+
}
31+
```
32+
{% include copy-curl.html %}
33+
34+
Index a geopoint, specifying its latitude and longitude:
35+
36+
```json
37+
PUT testindex1/_doc/1
38+
{
39+
"point": {
40+
"lat": 74.00,
41+
"lon": 40.71
42+
}
43+
}
44+
```
45+
{% include copy-curl.html %}
46+
47+
Search for documents whose `point` objects are within the specified `distance` from the specified `point`:
48+
49+
```json
50+
GET /testindex1/_search
51+
{
52+
"query": {
53+
"bool": {
54+
"must": {
55+
"match_all": {}
56+
},
57+
"filter": {
58+
"geo_distance": {
59+
"distance": "50mi",
60+
"point": {
61+
"lat": 73.5,
62+
"lon": 40.5
63+
}
64+
}
65+
}
66+
}
67+
}
68+
}
69+
```
70+
{% include copy-curl.html %}
71+
72+
The response contains the matching document:
73+
74+
```json
75+
{
76+
"took": 5,
77+
"timed_out": false,
78+
"_shards": {
79+
"total": 1,
80+
"successful": 1,
81+
"skipped": 0,
82+
"failed": 0
83+
},
84+
"hits": {
85+
"total": {
86+
"value": 1,
87+
"relation": "eq"
88+
},
89+
"max_score": 1,
90+
"hits": [
91+
{
92+
"_index": "testindex1",
93+
"_id": "1",
94+
"_score": 1,
95+
"_source": {
96+
"point": {
97+
"lat": 74,
98+
"lon": 40.71
99+
}
100+
}
101+
}
102+
]
103+
}
104+
}
105+
```
106+
107+
## Request fields
108+
109+
Geodistance queries accept the following fields.
110+
111+
Field | Data type | Description
112+
:--- | :--- | :---
113+
`_name` | String | The name of the filter. Optional.
114+
`distance` | String | The distance within which to match the points. This distance is the radius of a circle centered at the specified point. For supported distance units, see [Distance units]({{site.url}}{{site.baseurl}}/api-reference/common-parameters/#distance-units). Required.
115+
`distance_type` | String | Specifies how to calculate the distance. Valid values are `arc` or `plane` (faster but inaccurate for long distances or points close to the poles). Optional. Default is `arc`.
116+
`validation_method` | String | The validation method. Valid values are `IGNORE_MALFORMED` (accept geopoints with invalid coordinates), `COERCE` (try to coerce coordinates to valid values), and `STRICT` (return an error when coordinates are invalid). Optional. Default is `STRICT`.
117+
`ignore_unmapped` | Boolean | Specifies whether to ignore an unmapped field. If set to `true`, then the query does not return any documents that contain an unmapped field. If set to `false`, then an exception is thrown when the field is unmapped. Optional. Default is `false`.
118+
119+
## Accepted formats
120+
121+
You can specify the geopoint coordinates when indexing a document and searching for documents in any [format]({{site.url}}{{site.baseurl}}/opensearch/supported-field-types/geo-point#formats) accepted by the geopoint field type.

_query-dsl/geo-and-xy/index.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ redirect_from:
1212

1313
# Geographic and xy queries
1414

15-
Geographic and xy queries let you search fields that contain points and shapes on a map or coordinate plane. Geographic queries work on geospatial data, while xy queries work on two-dimensional coordinate data. Out of all geographic queries, the geoshape query is very similar to the xy query, but the former searches [geographic fields]({{site.url}}{{site.baseurl}}/opensearch/supported-field-types/geographic), while the latter searches [Cartesian fields]({{site.url}}{{site.baseurl}}/opensearch/supported-field-types/xy).
15+
Geographic and xy queries let you search fields that contain points and shapes on a map or coordinate plane. Geographic queries work on geospatial data, while xy queries work on two-dimensional coordinate data. Out of all geographic queries, the geoshape query is very similar to the xy query, but the former searches [geographic fields]({{site.url}}{{site.baseurl}}/opensearch/supported-field-types/geographic/), while the latter searches [Cartesian fields]({{site.url}}{{site.baseurl}}/opensearch/supported-field-types/xy).
1616

1717
## xy queries
1818

@@ -24,13 +24,13 @@ xy queries return documents that contain:
2424

2525
## Geographic queries
2626

27-
Geographic queries search for documents that contain geospatial geometries. These geometries can be specified in [`geo_point`]({{site.url}}{{site.baseurl}}/opensearch/supported-field-types/geo-point) fields, which support points on a map, and [`geo_shape`]({{site.url}}{{site.baseurl}}/opensearch/supported-field-types/geo-shape) fields, which support points, lines, circles, and polygons.
27+
Geographic queries search for documents that contain geospatial geometries. These geometries can be specified in [`geo_point`]({{site.url}}{{site.baseurl}}/opensearch/supported-field-types/geo-point/) fields, which support points on a map, and [`geo_shape`]({{site.url}}{{site.baseurl}}/opensearch/supported-field-types/geo-shape/) fields, which support points, lines, circles, and polygons.
2828

2929
OpenSearch provides the following geographic query types:
3030

3131
- [**Geo-bounding box queries**]({{site.url}}{{site.baseurl}}/opensearch/query-dsl/geo-and-xy/geo-bounding-box/): Return documents with geopoint field values that are within a bounding box.
32-
- **Geodistance queries** return documents with geopoints that are within a specified distance from the provided geopoint.
33-
- **Geopolygon queries** return documents with geopoints that are within a polygon.
34-
- **Geoshape queries** return documents that contain:
35-
- geoshapes and geopoints that have one of four spatial relations to the provided shape: `INTERSECTS`, `DISJOINT`, `WITHIN`, or `CONTAINS`.
36-
- geopoints that intersect the provided shape.
32+
- [**Geodistance queries**]({{site.url}}{{site.baseurl}}/query-dsl/geo-and-xy/geodistance/): Return documents with geopoints that are within a specified distance from the provided geopoint.
33+
- **Geopolygon queries**: Return documents with geopoints that are within a polygon.
34+
- **Geoshape queries**: Return documents that contain:
35+
- Geoshapes and geopoints that have one of four spatial relations to the provided shape: `INTERSECTS`, `DISJOINT`, `WITHIN`, or `CONTAINS`.
36+
- Geopoints that intersect the provided shape.

_query-dsl/geo-and-xy/xy.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ redirect_from:
1212

1313
# xy query
1414

15-
To search for documents that contain [xy point]({{site.url}}{{site.baseurl}}/opensearch/supported-field-types/xy-point) and [xy shape]({{site.url}}{{site.baseurl}}/opensearch/supported-field-types/xy-shape) fields, use an xy query.
15+
To search for documents that contain [xy point]({{site.url}}{{site.baseurl}}/opensearch/supported-field-types/xy-point/) or [xy shape]({{site.url}}{{site.baseurl}}/opensearch/supported-field-types/xy-shape/) fields, use an xy query.
1616

1717
## Spatial relations
1818

1919
When you provide an xy shape to the xy query, the xy fields are matched using the following spatial relations to the provided shape.
2020

21-
Relation | Description | Supporting xy Field Type
21+
Relation | Description | Supporting xy field type
2222
:--- | :--- | :---
2323
`INTERSECTS` | (Default) Matches documents whose xy point or xy shape intersects the shape provided in the query. | `xy_point`, `xy_shape`
2424
`DISJOINT` | Matches documents whose xy shape does not intersect with the shape provided in the query. | `xy_shape`
@@ -51,6 +51,7 @@ PUT testindex
5151
}
5252
}
5353
```
54+
{% include copy-curl.html %}
5455

5556
Index a document with a point and a document with a polygon:
5657

@@ -62,7 +63,10 @@ PUT testindex/_doc/1
6263
"coordinates": [0.5, 3.0]
6364
}
6465
}
66+
```
67+
{% include copy-curl.html %}
6568

69+
```json
6670
PUT testindex/_doc/2
6771
{
6872
"geometry" : {
@@ -77,6 +81,7 @@ PUT testindex/_doc/2
7781
}
7882
}
7983
```
84+
{% include copy-curl.html %}
8085

8186
Define an [`envelope`]({{site.url}}{{site.baseurl}}/opensearch/supported-field-types/xy-shape#envelope)&mdash;a bounding rectangle in the `[[minX, maxY], [maxX, minY]]` format. Search for documents with xy points or shapes that intersect that envelope:
8287

@@ -96,6 +101,7 @@ GET testindex/_search
96101
}
97102
}
98103
```
104+
{% include copy-curl.html %}
99105

100106
The following image depicts the example. Both the point and the polygon are within the bounding envelope.
101107

@@ -200,6 +206,7 @@ PUT pre-indexed-shapes
200206
}
201207
}
202208
```
209+
{% include copy-curl.html %}
203210

204211
Index an envelope that specifies the boundaries and name it `rectangle`:
205212

@@ -212,6 +219,7 @@ PUT pre-indexed-shapes/_doc/rectangle
212219
}
213220
}
214221
```
222+
{% include copy-curl.html %}
215223

216224
Index a document with a point and a document with a polygon into the index `testindex`:
217225

@@ -223,7 +231,10 @@ PUT testindex/_doc/1
223231
"coordinates": [0.5, 3.0]
224232
}
225233
}
234+
```
235+
{% include copy-curl.html %}
226236

237+
```json
227238
PUT testindex/_doc/2
228239
{
229240
"geometry" : {
@@ -238,6 +249,7 @@ PUT testindex/_doc/2
238249
}
239250
}
240251
```
252+
{% include copy-curl.html %}
241253

242254
Search for documents with shapes that intersect `rectangle` in the index `testindex` using a filter:
243255

@@ -261,6 +273,7 @@ GET testindex/_search
261273
}
262274
}
263275
```
276+
{% include copy-curl.html %}
264277

265278
The preceding query uses the default spatial relation `INTERSECTS` and returns both the point and the polygon:
266279

@@ -352,6 +365,7 @@ PUT testindex1
352365
}
353366
}
354367
```
368+
{% include copy-curl.html %}
355369

356370
Index three points:
357371

@@ -360,17 +374,24 @@ PUT testindex1/_doc/1
360374
{
361375
"point": "1.0, 1.0"
362376
}
377+
```
378+
{% include copy-curl.html %}
363379

380+
```json
364381
PUT testindex1/_doc/2
365382
{
366383
"point": "2.0, 0.0"
367384
}
385+
```
386+
{% include copy-curl.html %}
368387

388+
```json
369389
PUT testindex1/_doc/3
370390
{
371391
"point": "-2.0, 2.0"
372392
}
373393
```
394+
{% include copy-curl.html %}
374395

375396
Search for points that lie within the circle with the center at (0, 0) and a radius of 2:
376397

@@ -390,6 +411,7 @@ GET testindex1/_search
390411
}
391412
}
392413
```
414+
{% include copy-curl.html %}
393415

394416
xy point only supports the default `INTERSECTS` spatial relation, so you don't need to provide the `relation` parameter.
395417
{: .note}

0 commit comments

Comments
 (0)