Shape query

Shape query

Queries documents that contain fields indexed using the shape type.

Requires the shape Mapping.

The query supports two ways of defining the target shape, either by providing a whole shape definition, or by referencing the name, or id, of a shape pre-indexed in another index. Both formats are defined below with examples.

Inline Shape Definition

Similar to the geo_shape query, the shape query uses GeoJSON or Well Known Text (WKT) to represent shapes.

Given the following index:

  1. resp = client.indices.create(
  2. index="example",
  3. mappings={
  4. "properties": {
  5. "geometry": {
  6. "type": "shape"
  7. }
  8. }
  9. },
  10. )
  11. print(resp)
  12. resp1 = client.index(
  13. index="example",
  14. id="1",
  15. refresh="wait_for",
  16. document={
  17. "name": "Lucky Landing",
  18. "geometry": {
  19. "type": "point",
  20. "coordinates": [
  21. 1355.400544,
  22. 5255.530286
  23. ]
  24. }
  25. },
  26. )
  27. print(resp1)
  1. response = client.indices.create(
  2. index: 'example',
  3. body: {
  4. mappings: {
  5. properties: {
  6. geometry: {
  7. type: 'shape'
  8. }
  9. }
  10. }
  11. }
  12. )
  13. puts response
  14. response = client.index(
  15. index: 'example',
  16. id: 1,
  17. refresh: 'wait_for',
  18. body: {
  19. name: 'Lucky Landing',
  20. geometry: {
  21. type: 'point',
  22. coordinates: [
  23. 1355.400544,
  24. 5255.530286
  25. ]
  26. }
  27. }
  28. )
  29. puts response
  1. const response = await client.indices.create({
  2. index: "example",
  3. mappings: {
  4. properties: {
  5. geometry: {
  6. type: "shape",
  7. },
  8. },
  9. },
  10. });
  11. console.log(response);
  12. const response1 = await client.index({
  13. index: "example",
  14. id: 1,
  15. refresh: "wait_for",
  16. document: {
  17. name: "Lucky Landing",
  18. geometry: {
  19. type: "point",
  20. coordinates: [1355.400544, 5255.530286],
  21. },
  22. },
  23. });
  24. console.log(response1);
  1. PUT /example
  2. {
  3. "mappings": {
  4. "properties": {
  5. "geometry": {
  6. "type": "shape"
  7. }
  8. }
  9. }
  10. }
  11. PUT /example/_doc/1?refresh=wait_for
  12. {
  13. "name": "Lucky Landing",
  14. "geometry": {
  15. "type": "point",
  16. "coordinates": [ 1355.400544, 5255.530286 ]
  17. }
  18. }

The following query will find the point using the Elasticsearch’s envelope GeoJSON extension:

  1. resp = client.search(
  2. index="example",
  3. query={
  4. "shape": {
  5. "geometry": {
  6. "shape": {
  7. "type": "envelope",
  8. "coordinates": [
  9. [
  10. 1355,
  11. 5355
  12. ],
  13. [
  14. 1400,
  15. 5200
  16. ]
  17. ]
  18. },
  19. "relation": "within"
  20. }
  21. }
  22. },
  23. )
  24. print(resp)
  1. const response = await client.search({
  2. index: "example",
  3. query: {
  4. shape: {
  5. geometry: {
  6. shape: {
  7. type: "envelope",
  8. coordinates: [
  9. [1355, 5355],
  10. [1400, 5200],
  11. ],
  12. },
  13. relation: "within",
  14. },
  15. },
  16. },
  17. });
  18. console.log(response);
  1. GET /example/_search
  2. {
  3. "query": {
  4. "shape": {
  5. "geometry": {
  6. "shape": {
  7. "type": "envelope",
  8. "coordinates": [ [ 1355.0, 5355.0 ], [ 1400.0, 5200.0 ] ]
  9. },
  10. "relation": "within"
  11. }
  12. }
  13. }
  14. }

Pre-Indexed Shape

The Query also supports using a shape which has already been indexed in another index. This is particularly useful for when you have a pre-defined list of shapes which are useful to your application and you want to reference this using a logical name (for example New Zealand) rather than having to provide their coordinates each time. In this situation it is only necessary to provide:

  • id - The ID of the document that containing the pre-indexed shape.
  • index - Name of the index where the pre-indexed shape is. Defaults to shapes.
  • path - The field specified as path containing the pre-indexed shape. Defaults to shape.
  • routing - The routing of the shape document if required.

The following is an example of using the Filter with a pre-indexed shape:

  1. resp = client.indices.create(
  2. index="shapes",
  3. mappings={
  4. "properties": {
  5. "geometry": {
  6. "type": "shape"
  7. }
  8. }
  9. },
  10. )
  11. print(resp)
  12. resp1 = client.index(
  13. index="shapes",
  14. id="footprint",
  15. document={
  16. "geometry": {
  17. "type": "envelope",
  18. "coordinates": [
  19. [
  20. 1355,
  21. 5355
  22. ],
  23. [
  24. 1400,
  25. 5200
  26. ]
  27. ]
  28. }
  29. },
  30. )
  31. print(resp1)
  32. resp2 = client.search(
  33. index="example",
  34. query={
  35. "shape": {
  36. "geometry": {
  37. "indexed_shape": {
  38. "index": "shapes",
  39. "id": "footprint",
  40. "path": "geometry"
  41. }
  42. }
  43. }
  44. },
  45. )
  46. print(resp2)
  1. response = client.indices.create(
  2. index: 'shapes',
  3. body: {
  4. mappings: {
  5. properties: {
  6. geometry: {
  7. type: 'shape'
  8. }
  9. }
  10. }
  11. }
  12. )
  13. puts response
  14. response = client.index(
  15. index: 'shapes',
  16. id: 'footprint',
  17. body: {
  18. geometry: {
  19. type: 'envelope',
  20. coordinates: [
  21. [
  22. 1355,
  23. 5355
  24. ],
  25. [
  26. 1400,
  27. 5200
  28. ]
  29. ]
  30. }
  31. }
  32. )
  33. puts response
  34. response = client.search(
  35. index: 'example',
  36. body: {
  37. query: {
  38. shape: {
  39. geometry: {
  40. indexed_shape: {
  41. index: 'shapes',
  42. id: 'footprint',
  43. path: 'geometry'
  44. }
  45. }
  46. }
  47. }
  48. }
  49. )
  50. puts response
  1. const response = await client.indices.create({
  2. index: "shapes",
  3. mappings: {
  4. properties: {
  5. geometry: {
  6. type: "shape",
  7. },
  8. },
  9. },
  10. });
  11. console.log(response);
  12. const response1 = await client.index({
  13. index: "shapes",
  14. id: "footprint",
  15. document: {
  16. geometry: {
  17. type: "envelope",
  18. coordinates: [
  19. [1355, 5355],
  20. [1400, 5200],
  21. ],
  22. },
  23. },
  24. });
  25. console.log(response1);
  26. const response2 = await client.search({
  27. index: "example",
  28. query: {
  29. shape: {
  30. geometry: {
  31. indexed_shape: {
  32. index: "shapes",
  33. id: "footprint",
  34. path: "geometry",
  35. },
  36. },
  37. },
  38. },
  39. });
  40. console.log(response2);
  1. PUT /shapes
  2. {
  3. "mappings": {
  4. "properties": {
  5. "geometry": {
  6. "type": "shape"
  7. }
  8. }
  9. }
  10. }
  11. PUT /shapes/_doc/footprint
  12. {
  13. "geometry": {
  14. "type": "envelope",
  15. "coordinates": [ [ 1355.0, 5355.0 ], [ 1400.0, 5200.0 ] ]
  16. }
  17. }
  18. GET /example/_search
  19. {
  20. "query": {
  21. "shape": {
  22. "geometry": {
  23. "indexed_shape": {
  24. "index": "shapes",
  25. "id": "footprint",
  26. "path": "geometry"
  27. }
  28. }
  29. }
  30. }
  31. }

Spatial Relations

The following is a complete list of spatial relation operators available:

  • INTERSECTS - (default) Return all documents whose shape field intersects the query geometry.
  • DISJOINT - Return all documents whose shape field has nothing in common with the query geometry.
  • WITHIN - Return all documents whose shape field is within the query geometry.
  • CONTAINS - Return all documents whose shape field contains the query geometry.

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.