xy queries

To search for documents that contain xy point and xy shape fields, use an xy query.

Spatial relations

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

RelationDescriptionSupporting xy Field Type
INTERSECTS(Default) Matches documents whose xy point or xy shape intersects the shape provided in the query.xy_point, xy_shape
DISJOINTMatches documents whose xy shape does not intersect with the shape provided in the query.xy_shape
WITHINMatches documents whose xy shape is completely within the shape provided in the query.xy_shape
CONTAINSMatches documents whose xy shape completely contains the shape provided in the query.xy_shape

The following examples illustrate searching for documents that contain xy shapes. To learn how to search for documents that contain xy points, see the Querying xy points section.

Defining the shape in an xy query

You can define the shape in an xy query either by providing a new shape definition at query time or by referencing the name of a shape pre-indexed in another index.

Using a new shape definition

To provide a new shape to an xy query, define it in the xy_shape field.

The following example illustrates searching for documents with xy shapes that match an xy shape defined at query time.

First, create an index and map the geometry field as an xy_shape:

  1. PUT testindex
  2. {
  3. "mappings": {
  4. "properties": {
  5. "geometry": {
  6. "type": "xy_shape"
  7. }
  8. }
  9. }
  10. }

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

  1. PUT testindex/_doc/1
  2. {
  3. "geometry": {
  4. "type": "point",
  5. "coordinates": [0.5, 3.0]
  6. }
  7. }
  8. PUT testindex/_doc/2
  9. {
  10. "geometry" : {
  11. "type" : "polygon",
  12. "coordinates" : [
  13. [[2.5, 6.0],
  14. [0.5, 4.5],
  15. [1.5, 2.0],
  16. [3.5, 3.5],
  17. [2.5, 6.0]]
  18. ]
  19. }
  20. }

Define an envelope—a bounding rectangle in the [[minX, maxY], [maxX, minY]] format. Search for documents with xy points or shapes that intersect that envelope:

  1. GET testindex/_search
  2. {
  3. "query": {
  4. "xy_shape": {
  5. "geometry": {
  6. "shape": {
  7. "type": "envelope",
  8. "coordinates": [ [ 0.0, 6.0], [ 4.0, 2.0] ]
  9. },
  10. "relation": "WITHIN"
  11. }
  12. }
  13. }
  14. }

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

xy shape query

The response contains both documents:

  1. {
  2. "took" : 363,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 2,
  13. "relation" : "eq"
  14. },
  15. "max_score" : 0.0,
  16. "hits" : [
  17. {
  18. "_index" : "testindex",
  19. "_id" : "1",
  20. "_score" : 0.0,
  21. "_source" : {
  22. "geometry" : {
  23. "type" : "point",
  24. "coordinates" : [
  25. 0.5,
  26. 3.0
  27. ]
  28. }
  29. }
  30. },
  31. {
  32. "_index" : "testindex",
  33. "_id" : "2",
  34. "_score" : 0.0,
  35. "_source" : {
  36. "geometry" : {
  37. "type" : "polygon",
  38. "coordinates" : [
  39. [
  40. [
  41. 2.5,
  42. 6.0
  43. ],
  44. [
  45. 0.5,
  46. 4.5
  47. ],
  48. [
  49. 1.5,
  50. 2.0
  51. ],
  52. [
  53. 3.5,
  54. 3.5
  55. ],
  56. [
  57. 2.5,
  58. 6.0
  59. ]
  60. ]
  61. ]
  62. }
  63. }
  64. }
  65. ]
  66. }
  67. }

Using a pre-indexed shape definition

When constructing an xy query, you can also reference the name of a shape pre-indexed in another index. Using this method, you can define an xy shape at index time and refer to it by name, providing the following parameters in the indexed_shape object.

ParameterDescription
indexThe name of the index that contains the pre-indexed shape.
idThe document ID of the document that contains the pre-indexed shape.
pathThe field name of the field that contains the pre-indexed shape as a path.

The following example illustrates referencing the name of a shape pre-indexed in another index. In this example, the index pre-indexed-shapes contains the shape that defines the boundaries, and the index testindex contains the shapes whose locations are checked against those boundaries.

First, create an index pre-indexed-shapes and map the geometry field for this index as an xy_shape:

  1. PUT pre-indexed-shapes
  2. {
  3. "mappings": {
  4. "properties": {
  5. "geometry": {
  6. "type": "xy_shape"
  7. }
  8. }
  9. }
  10. }

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

  1. PUT pre-indexed-shapes/_doc/rectangle
  2. {
  3. "geometry": {
  4. "type": "envelope",
  5. "coordinates" : [ [ 0.0, 6.0], [ 4.0, 2.0] ]
  6. }
  7. }

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

  1. PUT testindex/_doc/1
  2. {
  3. "geometry": {
  4. "type": "point",
  5. "coordinates": [0.5, 3.0]
  6. }
  7. }
  8. PUT testindex/_doc/2
  9. {
  10. "geometry" : {
  11. "type" : "polygon",
  12. "coordinates" : [
  13. [[2.5, 6.0],
  14. [0.5, 4.5],
  15. [1.5, 2.0],
  16. [3.5, 3.5],
  17. [2.5, 6.0]]
  18. ]
  19. }
  20. }

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

  1. GET testindex/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "filter": {
  6. "xy_shape": {
  7. "geometry": {
  8. "indexed_shape": {
  9. "index": "pre-indexed-shapes",
  10. "id": "rectangle",
  11. "path": "geometry"
  12. }
  13. }
  14. }
  15. }
  16. }
  17. }
  18. }

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

  1. {
  2. "took" : 26,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 2,
  13. "relation" : "eq"
  14. },
  15. "max_score" : 0.0,
  16. "hits" : [
  17. {
  18. "_index" : "testindex",
  19. "_id" : "1",
  20. "_score" : 0.0,
  21. "_source" : {
  22. "geometry" : {
  23. "type" : "point",
  24. "coordinates" : [
  25. 0.5,
  26. 3.0
  27. ]
  28. }
  29. }
  30. },
  31. {
  32. "_index" : "testindex",
  33. "_id" : "2",
  34. "_score" : 0.0,
  35. "_source" : {
  36. "geometry" : {
  37. "type" : "polygon",
  38. "coordinates" : [
  39. [
  40. [
  41. 2.5,
  42. 6.0
  43. ],
  44. [
  45. 0.5,
  46. 4.5
  47. ],
  48. [
  49. 1.5,
  50. 2.0
  51. ],
  52. [
  53. 3.5,
  54. 3.5
  55. ],
  56. [
  57. 2.5,
  58. 6.0
  59. ]
  60. ]
  61. ]
  62. }
  63. }
  64. }
  65. ]
  66. }
  67. }

Querying xy points

You can also use an xy query to search for documents that contain xy points.

Create a mapping with point as xy_point:

  1. PUT testindex1
  2. {
  3. "mappings": {
  4. "properties": {
  5. "point": {
  6. "type": "xy_point"
  7. }
  8. }
  9. }
  10. }

Index three points:

  1. PUT testindex1/_doc/1
  2. {
  3. "point": "1.0, 1.0"
  4. }
  5. PUT testindex1/_doc/2
  6. {
  7. "point": "2.0, 0.0"
  8. }
  9. PUT testindex1/_doc/3
  10. {
  11. "point": "-2.0, 2.0"
  12. }

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

  1. GET testindex1/_search
  2. {
  3. "query": {
  4. "xy_shape": {
  5. "point": {
  6. "shape": {
  7. "type": "circle",
  8. "coordinates": [0.0, 0.0],
  9. "radius": 2
  10. }
  11. }
  12. }
  13. }
  14. }

xy point only supports the default INTERSECTS spatial relation, so you don’t need to provide the relation parameter.

The following image depicts the example. Points 1 and 2 are within the circle, and point 3 is outside the circle.

xy point query

The response returns documents 1 and 2:

  1. {
  2. "took" : 575,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 2,
  13. "relation" : "eq"
  14. },
  15. "max_score" : 0.0,
  16. "hits" : [
  17. {
  18. "_index" : "testindex1",
  19. "_id" : "1",
  20. "_score" : 0.0,
  21. "_source" : {
  22. "point" : "1.0, 1.0"
  23. }
  24. },
  25. {
  26. "_index" : "testindex1",
  27. "_id" : "2",
  28. "_score" : 0.0,
  29. "_source" : {
  30. "point" : "2.0, 0.0"
  31. }
  32. }
  33. ]
  34. }
  35. }

xy queries - 图3