Geo-distance query

Geo-distance query

Matches geo_point and geo_shape values within a given distance of a geopoint.

Example

Assume the following documents are indexed:

  1. resp = client.indices.create(
  2. index="my_locations",
  3. mappings={
  4. "properties": {
  5. "pin": {
  6. "properties": {
  7. "location": {
  8. "type": "geo_point"
  9. }
  10. }
  11. }
  12. }
  13. },
  14. )
  15. print(resp)
  16. resp1 = client.index(
  17. index="my_locations",
  18. id="1",
  19. document={
  20. "pin": {
  21. "location": {
  22. "lat": 40.12,
  23. "lon": -71.34
  24. }
  25. }
  26. },
  27. )
  28. print(resp1)
  29. resp2 = client.indices.create(
  30. index="my_geoshapes",
  31. mappings={
  32. "properties": {
  33. "pin": {
  34. "properties": {
  35. "location": {
  36. "type": "geo_shape"
  37. }
  38. }
  39. }
  40. }
  41. },
  42. )
  43. print(resp2)
  44. resp3 = client.index(
  45. index="my_geoshapes",
  46. id="1",
  47. document={
  48. "pin": {
  49. "location": {
  50. "type": "polygon",
  51. "coordinates": [
  52. [
  53. [
  54. 13,
  55. 51.5
  56. ],
  57. [
  58. 15,
  59. 51.5
  60. ],
  61. [
  62. 15,
  63. 54
  64. ],
  65. [
  66. 13,
  67. 54
  68. ],
  69. [
  70. 13,
  71. 51.5
  72. ]
  73. ]
  74. ]
  75. }
  76. }
  77. },
  78. )
  79. print(resp3)
  1. response = client.indices.create(
  2. index: 'my_locations',
  3. body: {
  4. mappings: {
  5. properties: {
  6. pin: {
  7. properties: {
  8. location: {
  9. type: 'geo_point'
  10. }
  11. }
  12. }
  13. }
  14. }
  15. }
  16. )
  17. puts response
  18. response = client.index(
  19. index: 'my_locations',
  20. id: 1,
  21. body: {
  22. pin: {
  23. location: {
  24. lat: 40.12,
  25. lon: -71.34
  26. }
  27. }
  28. }
  29. )
  30. puts response
  31. response = client.indices.create(
  32. index: 'my_geoshapes',
  33. body: {
  34. mappings: {
  35. properties: {
  36. pin: {
  37. properties: {
  38. location: {
  39. type: 'geo_shape'
  40. }
  41. }
  42. }
  43. }
  44. }
  45. }
  46. )
  47. puts response
  48. response = client.index(
  49. index: 'my_geoshapes',
  50. id: 1,
  51. body: {
  52. pin: {
  53. location: {
  54. type: 'polygon',
  55. coordinates: [
  56. [
  57. [
  58. 13,
  59. 51.5
  60. ],
  61. [
  62. 15,
  63. 51.5
  64. ],
  65. [
  66. 15,
  67. 54
  68. ],
  69. [
  70. 13,
  71. 54
  72. ],
  73. [
  74. 13,
  75. 51.5
  76. ]
  77. ]
  78. ]
  79. }
  80. }
  81. }
  82. )
  83. puts response
  1. const response = await client.indices.create({
  2. index: "my_locations",
  3. mappings: {
  4. properties: {
  5. pin: {
  6. properties: {
  7. location: {
  8. type: "geo_point",
  9. },
  10. },
  11. },
  12. },
  13. },
  14. });
  15. console.log(response);
  16. const response1 = await client.index({
  17. index: "my_locations",
  18. id: 1,
  19. document: {
  20. pin: {
  21. location: {
  22. lat: 40.12,
  23. lon: -71.34,
  24. },
  25. },
  26. },
  27. });
  28. console.log(response1);
  29. const response2 = await client.indices.create({
  30. index: "my_geoshapes",
  31. mappings: {
  32. properties: {
  33. pin: {
  34. properties: {
  35. location: {
  36. type: "geo_shape",
  37. },
  38. },
  39. },
  40. },
  41. },
  42. });
  43. console.log(response2);
  44. const response3 = await client.index({
  45. index: "my_geoshapes",
  46. id: 1,
  47. document: {
  48. pin: {
  49. location: {
  50. type: "polygon",
  51. coordinates: [
  52. [
  53. [13, 51.5],
  54. [15, 51.5],
  55. [15, 54],
  56. [13, 54],
  57. [13, 51.5],
  58. ],
  59. ],
  60. },
  61. },
  62. },
  63. });
  64. console.log(response3);
  1. PUT /my_locations
  2. {
  3. "mappings": {
  4. "properties": {
  5. "pin": {
  6. "properties": {
  7. "location": {
  8. "type": "geo_point"
  9. }
  10. }
  11. }
  12. }
  13. }
  14. }
  15. PUT /my_locations/_doc/1
  16. {
  17. "pin": {
  18. "location": {
  19. "lat": 40.12,
  20. "lon": -71.34
  21. }
  22. }
  23. }
  24. PUT /my_geoshapes
  25. {
  26. "mappings": {
  27. "properties": {
  28. "pin": {
  29. "properties": {
  30. "location": {
  31. "type": "geo_shape"
  32. }
  33. }
  34. }
  35. }
  36. }
  37. }
  38. PUT /my_geoshapes/_doc/1
  39. {
  40. "pin": {
  41. "location": {
  42. "type" : "polygon",
  43. "coordinates" : [[[13.0 ,51.5], [15.0, 51.5], [15.0, 54.0], [13.0, 54.0], [13.0 ,51.5]]]
  44. }
  45. }
  46. }

Use a geo_distance filter to match geo_point values within a specified distance of another geopoint:

  1. resp = client.search(
  2. index="my_locations",
  3. query={
  4. "bool": {
  5. "must": {
  6. "match_all": {}
  7. },
  8. "filter": {
  9. "geo_distance": {
  10. "distance": "200km",
  11. "pin.location": {
  12. "lat": 40,
  13. "lon": -70
  14. }
  15. }
  16. }
  17. }
  18. },
  19. )
  20. print(resp)
  1. response = client.search(
  2. index: 'my_locations',
  3. body: {
  4. query: {
  5. bool: {
  6. must: {
  7. match_all: {}
  8. },
  9. filter: {
  10. geo_distance: {
  11. distance: '200km',
  12. 'pin.location' => {
  13. lat: 40,
  14. lon: -70
  15. }
  16. }
  17. }
  18. }
  19. }
  20. }
  21. )
  22. puts response
  1. const response = await client.search({
  2. index: "my_locations",
  3. query: {
  4. bool: {
  5. must: {
  6. match_all: {},
  7. },
  8. filter: {
  9. geo_distance: {
  10. distance: "200km",
  11. "pin.location": {
  12. lat: 40,
  13. lon: -70,
  14. },
  15. },
  16. },
  17. },
  18. },
  19. });
  20. console.log(response);
  1. GET /my_locations/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": {
  6. "match_all": {}
  7. },
  8. "filter": {
  9. "geo_distance": {
  10. "distance": "200km",
  11. "pin.location": {
  12. "lat": 40,
  13. "lon": -70
  14. }
  15. }
  16. }
  17. }
  18. }
  19. }

Use the same filter to match geo_shape values within the given distance:

  1. resp = client.search(
  2. index="my_geoshapes",
  3. query={
  4. "bool": {
  5. "must": {
  6. "match_all": {}
  7. },
  8. "filter": {
  9. "geo_distance": {
  10. "distance": "200km",
  11. "pin.location": {
  12. "lat": 40,
  13. "lon": -70
  14. }
  15. }
  16. }
  17. }
  18. },
  19. )
  20. print(resp)
  1. response = client.search(
  2. index: 'my_geoshapes',
  3. body: {
  4. query: {
  5. bool: {
  6. must: {
  7. match_all: {}
  8. },
  9. filter: {
  10. geo_distance: {
  11. distance: '200km',
  12. 'pin.location' => {
  13. lat: 40,
  14. lon: -70
  15. }
  16. }
  17. }
  18. }
  19. }
  20. }
  21. )
  22. puts response
  1. const response = await client.search({
  2. index: "my_geoshapes",
  3. query: {
  4. bool: {
  5. must: {
  6. match_all: {},
  7. },
  8. filter: {
  9. geo_distance: {
  10. distance: "200km",
  11. "pin.location": {
  12. lat: 40,
  13. lon: -70,
  14. },
  15. },
  16. },
  17. },
  18. },
  19. });
  20. console.log(response);
  1. GET my_geoshapes/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": {
  6. "match_all": {}
  7. },
  8. "filter": {
  9. "geo_distance": {
  10. "distance": "200km",
  11. "pin.location": {
  12. "lat": 40,
  13. "lon": -70
  14. }
  15. }
  16. }
  17. }
  18. }
  19. }

To match both geo_point and geo_shape values, search both indices:

  1. resp = client.search(
  2. index="my_locations,my_geoshapes",
  3. query={
  4. "bool": {
  5. "must": {
  6. "match_all": {}
  7. },
  8. "filter": {
  9. "geo_distance": {
  10. "distance": "200km",
  11. "pin.location": {
  12. "lat": 40,
  13. "lon": -70
  14. }
  15. }
  16. }
  17. }
  18. },
  19. )
  20. print(resp)
  1. response = client.search(
  2. index: 'my_locations,my_geoshapes',
  3. body: {
  4. query: {
  5. bool: {
  6. must: {
  7. match_all: {}
  8. },
  9. filter: {
  10. geo_distance: {
  11. distance: '200km',
  12. 'pin.location' => {
  13. lat: 40,
  14. lon: -70
  15. }
  16. }
  17. }
  18. }
  19. }
  20. }
  21. )
  22. puts response
  1. const response = await client.search({
  2. index: "my_locations,my_geoshapes",
  3. query: {
  4. bool: {
  5. must: {
  6. match_all: {},
  7. },
  8. filter: {
  9. geo_distance: {
  10. distance: "200km",
  11. "pin.location": {
  12. lat: 40,
  13. lon: -70,
  14. },
  15. },
  16. },
  17. },
  18. },
  19. });
  20. console.log(response);
  1. GET my_locations,my_geoshapes/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": {
  6. "match_all": {}
  7. },
  8. "filter": {
  9. "geo_distance": {
  10. "distance": "200km",
  11. "pin.location": {
  12. "lat": 40,
  13. "lon": -70
  14. }
  15. }
  16. }
  17. }
  18. }
  19. }

Accepted formats

In much the same way the geo_point type can accept different representations of the geo point, the filter can accept it as well:

Lat lon as properties
  1. resp = client.search(
  2. index="my_locations",
  3. query={
  4. "bool": {
  5. "must": {
  6. "match_all": {}
  7. },
  8. "filter": {
  9. "geo_distance": {
  10. "distance": "12km",
  11. "pin.location": {
  12. "lat": 40,
  13. "lon": -70
  14. }
  15. }
  16. }
  17. }
  18. },
  19. )
  20. print(resp)
  1. response = client.search(
  2. index: 'my_locations',
  3. body: {
  4. query: {
  5. bool: {
  6. must: {
  7. match_all: {}
  8. },
  9. filter: {
  10. geo_distance: {
  11. distance: '12km',
  12. 'pin.location' => {
  13. lat: 40,
  14. lon: -70
  15. }
  16. }
  17. }
  18. }
  19. }
  20. }
  21. )
  22. puts response
  1. const response = await client.search({
  2. index: "my_locations",
  3. query: {
  4. bool: {
  5. must: {
  6. match_all: {},
  7. },
  8. filter: {
  9. geo_distance: {
  10. distance: "12km",
  11. "pin.location": {
  12. lat: 40,
  13. lon: -70,
  14. },
  15. },
  16. },
  17. },
  18. },
  19. });
  20. console.log(response);
  1. GET /my_locations/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": {
  6. "match_all": {}
  7. },
  8. "filter": {
  9. "geo_distance": {
  10. "distance": "12km",
  11. "pin.location": {
  12. "lat": 40,
  13. "lon": -70
  14. }
  15. }
  16. }
  17. }
  18. }
  19. }
Lat lon as array

Format in [lon, lat], note, the order of lon/lat here in order to conform with GeoJSON.

  1. resp = client.search(
  2. index="my_locations",
  3. query={
  4. "bool": {
  5. "must": {
  6. "match_all": {}
  7. },
  8. "filter": {
  9. "geo_distance": {
  10. "distance": "12km",
  11. "pin.location": [
  12. -70,
  13. 40
  14. ]
  15. }
  16. }
  17. }
  18. },
  19. )
  20. print(resp)
  1. response = client.search(
  2. index: 'my_locations',
  3. body: {
  4. query: {
  5. bool: {
  6. must: {
  7. match_all: {}
  8. },
  9. filter: {
  10. geo_distance: {
  11. distance: '12km',
  12. 'pin.location' => [
  13. -70,
  14. 40
  15. ]
  16. }
  17. }
  18. }
  19. }
  20. }
  21. )
  22. puts response
  1. const response = await client.search({
  2. index: "my_locations",
  3. query: {
  4. bool: {
  5. must: {
  6. match_all: {},
  7. },
  8. filter: {
  9. geo_distance: {
  10. distance: "12km",
  11. "pin.location": [-70, 40],
  12. },
  13. },
  14. },
  15. },
  16. });
  17. console.log(response);
  1. GET /my_locations/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": {
  6. "match_all": {}
  7. },
  8. "filter": {
  9. "geo_distance": {
  10. "distance": "12km",
  11. "pin.location": [ -70, 40 ]
  12. }
  13. }
  14. }
  15. }
  16. }
Lat lon as WKT string

Format in Well-Known Text.

  1. resp = client.search(
  2. index="my_locations",
  3. query={
  4. "bool": {
  5. "must": {
  6. "match_all": {}
  7. },
  8. "filter": {
  9. "geo_distance": {
  10. "distance": "12km",
  11. "pin.location": "POINT (-70 40)"
  12. }
  13. }
  14. }
  15. },
  16. )
  17. print(resp)
  1. response = client.search(
  2. index: 'my_locations',
  3. body: {
  4. query: {
  5. bool: {
  6. must: {
  7. match_all: {}
  8. },
  9. filter: {
  10. geo_distance: {
  11. distance: '12km',
  12. 'pin.location' => 'POINT (-70 40)'
  13. }
  14. }
  15. }
  16. }
  17. }
  18. )
  19. puts response
  1. const response = await client.search({
  2. index: "my_locations",
  3. query: {
  4. bool: {
  5. must: {
  6. match_all: {},
  7. },
  8. filter: {
  9. geo_distance: {
  10. distance: "12km",
  11. "pin.location": "POINT (-70 40)",
  12. },
  13. },
  14. },
  15. },
  16. });
  17. console.log(response);
  1. GET /my_locations/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": {
  6. "match_all": {}
  7. },
  8. "filter": {
  9. "geo_distance": {
  10. "distance": "12km",
  11. "pin.location": "POINT (-70 40)"
  12. }
  13. }
  14. }
  15. }
  16. }
Geohash
  1. resp = client.search(
  2. index="my_locations",
  3. query={
  4. "bool": {
  5. "must": {
  6. "match_all": {}
  7. },
  8. "filter": {
  9. "geo_distance": {
  10. "distance": "12km",
  11. "pin.location": "drm3btev3e86"
  12. }
  13. }
  14. }
  15. },
  16. )
  17. print(resp)
  1. response = client.search(
  2. index: 'my_locations',
  3. body: {
  4. query: {
  5. bool: {
  6. must: {
  7. match_all: {}
  8. },
  9. filter: {
  10. geo_distance: {
  11. distance: '12km',
  12. 'pin.location' => 'drm3btev3e86'
  13. }
  14. }
  15. }
  16. }
  17. }
  18. )
  19. puts response
  1. const response = await client.search({
  2. index: "my_locations",
  3. query: {
  4. bool: {
  5. must: {
  6. match_all: {},
  7. },
  8. filter: {
  9. geo_distance: {
  10. distance: "12km",
  11. "pin.location": "drm3btev3e86",
  12. },
  13. },
  14. },
  15. },
  16. });
  17. console.log(response);
  1. GET /my_locations/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": {
  6. "match_all": {}
  7. },
  8. "filter": {
  9. "geo_distance": {
  10. "distance": "12km",
  11. "pin.location": "drm3btev3e86"
  12. }
  13. }
  14. }
  15. }
  16. }

Options

The following are options allowed on the filter:

distance

The radius of the circle centred on the specified location. Points which fall into this circle are considered to be matches. The distance can be specified in various units. See Distance Units.

distance_type

How to compute the distance. Can either be arc (default), or plane (faster, but inaccurate on long distances and close to the poles).

_name

Optional name field to identify the query

validation_method

Set to IGNORE_MALFORMED to accept geo points with invalid latitude or longitude, set to COERCE to additionally try and infer correct coordinates (default is STRICT).

Multi location per document

The geo_distance filter can work with multiple locations / points per document. Once a single location / point matches the filter, the document will be included in the filter.

Ignore unmapped

When set to true the ignore_unmapped option will ignore an unmapped field and will not match any documents for this query. This can be useful when querying multiple indexes which might have different mappings. When set to false (the default value) the query will throw an exception if the field is not mapped.