diff --git a/docs/overview/queries/elastic_query_geo_polygon.md b/docs/overview/queries/elastic_query_geo_polygon.md new file mode 100644 index 000000000..32ed259ff --- /dev/null +++ b/docs/overview/queries/elastic_query_geo_polygon.md @@ -0,0 +1,37 @@ +--- +id: elastic_query_geo_polygon +title: "Geo-polygon Query" +--- + +A query returning hits that only fall within a polygon of points. + +In order to use the `GeoPolygon` query import the following: +```scala +import zio.elasticsearch.query.GeoPolygonQuery +import zio.elasticsearch.ElasticQuery._ +``` + +You can create a `GeoPolygon` query using the `geoPolygon` method with list of coordinates in the following manner: +```scala +val query: GeoPolygonQuery = geoPolygon(field = "location", List("0, 0", "0, 90", "90, 90", "90, 0")) +``` + +You can create a [type-safe](https://lambdaworks.github.io/zio-elasticsearch/overview/overview_zio_prelude_schema) `GeoPolygon` query using the `geoPolygon` method with list of coordinates in the following manner: +```scala +val query: GeoPolygonQuery = geoPolygon(field = Document.location, List("0, 0", "0, 90", "90, 90", "90, 0")) +``` + + +If you want to change the `_name`, you can use `name` method: +```scala +val queryWithName: GeoPolygonQuery = geoPolygon(field = "location", coordinates = List("0, 0", "0, 90", "90, 90", "90, 0")).name("name") +``` + +If you want to change the `validation_method`, you can use `validationMethod` method: +```scala +import zio.elasticsearch.query.ValidationMethod + +val queryWithValidationMethod: GeoPolygonQuery = geoPolygon(field = "location", coordinates = List("0, 0", "0, 90", "90, 90", "90, 0")).validationMethod(value = ValidationMethod.IgnoreMalformed) +``` + +You can find more information about `GeoPolygon` query [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-polygon-query.html). diff --git a/modules/integration/src/test/scala/zio/elasticsearch/HttpExecutorSpec.scala b/modules/integration/src/test/scala/zio/elasticsearch/HttpExecutorSpec.scala index beaf8476d..b7f4a79c5 100644 --- a/modules/integration/src/test/scala/zio/elasticsearch/HttpExecutorSpec.scala +++ b/modules/integration/src/test/scala/zio/elasticsearch/HttpExecutorSpec.scala @@ -2688,7 +2688,7 @@ object HttpExecutorSpec extends IntegrationSpec { |{ | "mappings": { | "properties": { - | "locationField": { + | "geoPointField": { | "type": "geo_point" | } | } @@ -2702,16 +2702,17 @@ object HttpExecutorSpec extends IntegrationSpec { _ <- Executor.execute( ElasticRequest.create[TestDocument](geoPolygonIndex, document).refreshTrue ) + r1 <- Executor .execute( ElasticRequest.search( geoPolygonIndex, ElasticQuery - .geoPolygon("locationField", List("0, 0", "0, 90", "90, 90", "90, 0")) + .geoPolygon("geoPointField", List("0, 0", "0, 90", "90, 90", "90, 0")) ) ) .documentAs[TestDocument] - } yield assertTrue(r1 == Chunk(document)) + } yield assert(r1)(equalTo(Chunk(document))) } } @@ after(Executor.execute(ElasticRequest.deleteIndex(geoPolygonIndex)).orDie) ), diff --git a/modules/library/src/test/scala/zio/elasticsearch/ElasticQuerySpec.scala b/modules/library/src/test/scala/zio/elasticsearch/ElasticQuerySpec.scala index 08457f739..2fad16004 100644 --- a/modules/library/src/test/scala/zio/elasticsearch/ElasticQuerySpec.scala +++ b/modules/library/src/test/scala/zio/elasticsearch/ElasticQuerySpec.scala @@ -2983,9 +2983,10 @@ object ElasticQuerySpec extends ZIOSpecDefault { geoPolygon(TestDocument.stringField, List("40, -70", "30, -80", "20, -90")).validationMethod( IgnoreMalformed ) - val queryWithAllParams = geoPolygon(TestDocument.stringField, List("40, -70", "30, -80", "20, -90")) - .validationMethod(IgnoreMalformed) - .name("name") + val queryWithAllParams = + geoPolygon(TestDocument.stringField, List("40, -70", "30, -80", "20, -90")) + .validationMethod(IgnoreMalformed) + .name("name") val expected = """ diff --git a/website/sidebars.js b/website/sidebars.js index 25fa56761..8c11fe381 100644 --- a/website/sidebars.js +++ b/website/sidebars.js @@ -22,6 +22,7 @@ module.exports = { 'overview/queries/elastic_query_function_score', 'overview/queries/elastic_query_fuzzy', 'overview/queries/elastic_query_geo_distance', + 'overview/queries/elastic_query_geo_polygon', 'overview/queries/elastic_query_has_child', 'overview/queries/elastic_query_has_parent', 'overview/queries/elastic_query_match',