geo-shape-query.asciidoc 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. GeoShapeQueryBuilder qb = geoShapeQuery(
  37. "pin.location", <1>
  38. ShapeBuilder.newMultiPoint() <2>
  39. .point(0, 0)
  40. .point(0, 10)
  41. .point(10, 10)
  42. .point(10, 0)
  43. .point(0, 0));
  44. qb.relation(ShapeRelation.WITHIN); <3>
  45. --------------------------------------------------
  46. <1> field
  47. <2> shape
  48. <3> relation can be `ShapeRelation.WITHIN`, `ShapeRelation.INTERSECTS` or `ShapeRelation.DISJOINT`
  49. [source,java]
  50. --------------------------------------------------
  51. // Using pre-indexed shapes
  52. GeoShapeQueryBuilder qb = geoShapeQuery(
  53. "pin.location", <1>
  54. "DEU", <2>
  55. "countries"); <3>
  56. qb.relation(ShapeRelation.WITHIN)) <4>
  57. .indexedShapeIndex("shapes") <5>
  58. .indexedShapePath("location"); <6>
  59. --------------------------------------------------
  60. <1> field
  61. <2> The ID of the document that containing the pre-indexed shape.
  62. <3> Index type where the pre-indexed shape is.
  63. <4> relation
  64. <5> Name of the index where the pre-indexed shape is. Defaults to 'shapes'.
  65. <6> The field specified as path containing the pre-indexed shape. Defaults to 'shape'.