geo-shape-query.asciidoc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. [[java-query-dsl-geo-shape-query]]
  2. ==== GeoShape Query
  3. See {ref}/query-dsl-geo-shape-query.html[Geo Shape Query]
  4. Note: the `geo_shape` type uses `Spatial4J` and `JTS`, both of which are
  5. optional dependencies. Consequently you must add `Spatial4J` and `JTS`
  6. to your classpath in order to use this type:
  7. [source,xml]
  8. -----------------------------------------------
  9. <dependency>
  10. <groupId>org.locationtech.spatial4j</groupId>
  11. <artifactId>spatial4j</artifactId>
  12. <version>0.6</version> <1>
  13. </dependency>
  14. <dependency>
  15. <groupId>com.vividsolutions</groupId>
  16. <artifactId>jts</artifactId>
  17. <version>1.13</version> <2>
  18. <exclusions>
  19. <exclusion>
  20. <groupId>xerces</groupId>
  21. <artifactId>xercesImpl</artifactId>
  22. </exclusion>
  23. </exclusions>
  24. </dependency>
  25. -----------------------------------------------
  26. <1> check for updates in http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22org.locationtech.spatial4j%22%20AND%20a%3A%22spatial4j%22[Maven Central]
  27. <2> check for updates in http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22com.vividsolutions%22%20AND%20a%3A%22jts%22[Maven Central]
  28. [source,java]
  29. --------------------------------------------------
  30. // Import ShapeRelation and ShapeBuilder
  31. import org.elasticsearch.common.geo.ShapeRelation;
  32. import org.elasticsearch.common.geo.builders.ShapeBuilder;
  33. --------------------------------------------------
  34. [source,java]
  35. --------------------------------------------------
  36. List<Coordinate> points = new ArrayList<>();
  37. points.add(new Coordinate(0, 0));
  38. points.add(new Coordinate(0, 10));
  39. points.add(new Coordinate(10, 10));
  40. points.add(new Coordinate(10, 0));
  41. points.add(new Coordinate(0, 0));
  42. QueryBuilder qb = geoShapeQuery(
  43. "pin.location", <1>
  44. ShapeBuilders.newMultiPoint(points) <2>
  45. .relation(ShapeRelation.WITHIN); <3>
  46. --------------------------------------------------
  47. <1> field
  48. <2> shape
  49. <3> relation can be `ShapeRelation.CONTAINS`, `ShapeRelation.WITHIN`, `ShapeRelation.INTERSECTS` or `ShapeRelation.DISJOINT`
  50. [source,java]
  51. --------------------------------------------------
  52. // Using pre-indexed shapes
  53. QueryBuilder qb = geoShapeQuery(
  54. "pin.location", <1>
  55. "DEU", <2>
  56. "countries") <3>
  57. .relation(ShapeRelation.WITHIN)) <4>
  58. .indexedShapeIndex("shapes") <5>
  59. .indexedShapePath("location"); <6>
  60. --------------------------------------------------
  61. <1> field
  62. <2> The ID of the document that containing the pre-indexed shape.
  63. <3> Index type where the pre-indexed shape is.
  64. <4> relation
  65. <5> Name of the index where the pre-indexed shape is. Defaults to 'shapes'.
  66. <6> The field specified as path containing the pre-indexed shape. Defaults to 'shape'.