-
Notifications
You must be signed in to change notification settings - Fork 20
(dsl): Support Geo bounding box query #651
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
* text=auto eol=lf | ||
sbt linguist-vendored |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,65 @@ | ||||||
--- | ||||||
id: elastic_query_geo_bounding_box | ||||||
title: "Geo-bounding-box Query" | ||||||
--- | ||||||
|
||||||
The `GeoBoundingBox` query matches documents containing geo points that fall within a defined rectangular bounding box, specified by the `top-left` and `bottom-right` corners. | ||||||
|
||||||
To use the `GeoBoundingBox` query, import the following: | ||||||
```scala | ||||||
import zio.elasticsearch.query.GeoBoundingBoxQuery | ||||||
import zio.elasticsearch.ElasticQuery._ | ||||||
``` | ||||||
|
||||||
You can create a [type-safe](https://lambdaworks.github.io/zio-elasticsearch/overview/overview_zio_prelude_schema) `GeoBoundingBoxQuery` query using typed document fields like this: | ||||||
```scala | ||||||
val query: GeoBoundingBoxQuery[Document] = | ||||||
geoBoundingBoxQuery( | ||||||
field = Document.location, | ||||||
topLeft = GeoPoint(40.73, -74.1), | ||||||
bottomRight = GeoPoint(40.01, -71.12) | ||||||
) | ||||||
``` | ||||||
|
||||||
You can create a `GeoBoundingBox` query using the `geoBoundingBoxQuery` method with GeoPoints for the `top-left` and `bottom-right` corners: | ||||||
```scala | ||||||
val query: GeoBoundingBoxQuery[Any] = | ||||||
geoBoundingBoxQuery( | ||||||
field = "location", | ||||||
topLeft = GeoPoint(40.73, -74.1), | ||||||
bottomRight = GeoPoint(40.01, -71.12) | ||||||
) | ||||||
``` | ||||||
|
||||||
If you want to `boost` the relevance score of the query, you can use the `boost` method: | ||||||
```scala | ||||||
val queryWithBoost = | ||||||
geoBoundingBoxQuery("location", GeoPoint(40.73, -74.1), GeoPoint(40.01, -71.12)) | ||||||
.boost(1.5) | ||||||
``` | ||||||
|
||||||
To ignore unmapped fields (fields that do not exist in the mapping), use the ignoreUnmapped method: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
```scala | ||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Omit empty line. |
||||||
val queryWithIgnoreUnmapped = | ||||||
geoBoundingBoxQuery("location", GeoPoint(40.73, -74.1), GeoPoint(40.01, -71.12)) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use named parameters. |
||||||
.ignoreUnmapped(true) | ||||||
|
||||||
``` | ||||||
To give the query a name for identification in the response, use the name method: | ||||||
```scala | ||||||
val queryWithName = | ||||||
geoBoundingBoxQuery("location", GeoPoint(40.73, -74.1), GeoPoint(40.01, -71.12)) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use named parameters. |
||||||
.name("myGeoBoxQuery") | ||||||
``` | ||||||
|
||||||
To specify how invalid geo coordinates are handled, use the validationMethod method: | ||||||
```scala | ||||||
import zio.elasticsearch.query.ValidationMethod | ||||||
|
||||||
val queryWithValidationMethod = | ||||||
geoBoundingBoxQuery("location", GeoPoint(40.73, -74.1), GeoPoint(40.01, -71.12)) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use named parameters. |
||||||
.validationMethod(ValidationMethod.IgnoreMalformed) | ||||||
``` | ||||||
|
||||||
You can find more information about `Geo-bounding-box` query [here](https://www.elastic.co/docs/reference/query-languages/query-dsl/query-dsl-geo-bounding-box-query).** |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2696,6 +2696,46 @@ object HttpExecutorSpec extends IntegrationSpec { | |
} | ||
} | ||
), | ||
suite("geo-bounding-box query")( | ||
test("using geo-bounding-box query") { | ||
checkOnce(genTestDocument) { document => | ||
val indexDefinition = | ||
""" | ||
|{ | ||
| "mappings": { | ||
| "properties": { | ||
| "geoPointField": { | ||
| "type": "geo_point" | ||
| } | ||
| } | ||
| } | ||
|} | ||
|""".stripMargin | ||
|
||
for { | ||
_ <- Executor.execute(ElasticRequest.createIndex(firstSearchIndex, indexDefinition)) | ||
_ <- Executor.execute(ElasticRequest.deleteByQuery(firstSearchIndex, matchAll)) | ||
_ <- Executor.execute( | ||
ElasticRequest.create[TestDocument](firstSearchIndex, document).refreshTrue | ||
) | ||
result <- Executor | ||
.execute( | ||
ElasticRequest.search( | ||
firstSearchIndex, | ||
ElasticQuery.geoBoundingBoxQuery( | ||
"geoPointField", | ||
topLeft = | ||
GeoPoint(document.geoPointField.lat + 0.1, document.geoPointField.lon - 0.1), | ||
bottomRight = | ||
GeoPoint(document.geoPointField.lat - 0.1, document.geoPointField.lon + 0.1) | ||
) | ||
Comment on lines
+2725
to
+2731
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please name first parameter too ("geoPointField"). |
||
) | ||
) | ||
.documentAs[TestDocument] | ||
} yield assert(result)(equalTo(Chunk(document))) | ||
} | ||
} @@ after(Executor.execute(ElasticRequest.deleteIndex(firstSearchIndex)).orDie) | ||
), | ||
suite("geo-distance query")( | ||
test("using geo-distance query") { | ||
checkOnce(genTestDocument) { document => | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ package zio.elasticsearch.query | |
import zio.Chunk | ||
import zio.elasticsearch.ElasticPrimitive._ | ||
import zio.elasticsearch.Field | ||
import zio.elasticsearch.data.GeoPoint | ||
import zio.elasticsearch.query.options._ | ||
import zio.elasticsearch.query.sort.options.HasFormat | ||
import zio.json.ast.Json | ||
|
@@ -497,6 +498,116 @@ private[elasticsearch] final case class Fuzzy[S]( | |
} | ||
} | ||
|
||
sealed trait GeoBoundingBoxQuery[S] extends ElasticQuery[S] { | ||
|
||
/** | ||
* Sets the `boost` parameter for the [[zio.elasticsearch.query.GeoBoundingBoxQuery]]. Boosts the relevance score of | ||
* the query. | ||
* | ||
* @param value | ||
* the boost factor as a [[Double]]. Values greater than 1.0 increase relevance, values between 0 and 1.0 decrease | ||
* it. | ||
* @return | ||
* an instance of [[zio.elasticsearch.query.GeoBoundingBoxQuery]] enriched with the `boost` parameter. | ||
*/ | ||
def boost(value: Double): GeoBoundingBoxQuery[S] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why don't we extend |
||
|
||
/** | ||
* Sets the `ignoreUnmapped` parameter for the [[zio.elasticsearch.query.GeoBoundingBoxQuery]]. Determines how to | ||
* handle unmapped fields. | ||
* | ||
* @param value | ||
* - true: unmapped fields are ignored and the query returns no matches for them | ||
* - false: query will throw an exception if the field is unmapped | ||
* @return | ||
* an instance of [[zio.elasticsearch.query.GeoBoundingBoxQuery]] enriched with the `ignoreUnmapped` parameter. | ||
*/ | ||
def ignoreUnmapped(value: Boolean = false): GeoBoundingBoxQuery[S] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, why shouldn't we use already existing |
||
|
||
/** | ||
* Sets the `queryName` parameter for the [[zio.elasticsearch.query.GeoBoundingBoxQuery]]. Represents the optional | ||
* name used to identify the query. | ||
* | ||
* @param value | ||
* the [[String]] name used to tag and identify this query in responses | ||
* @return | ||
* an instance of [[zio.elasticsearch.query.GeoBoundingBoxQuery]] enriched with the `queryName` parameter. | ||
*/ | ||
def name(value: String): GeoBoundingBoxQuery[S] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we have this in |
||
|
||
/** | ||
* Sets the `validationMethod` parameter for the [[zio.elasticsearch.query.GeoBoundingBoxQuery]]. Defines handling of | ||
* incorrect coordinates. | ||
* | ||
* @param value | ||
* defines how to handle invalid latitude and longitude: | ||
* - [[zio.elasticsearch.query.ValidationMethod.Strict]]: Default method | ||
* - [[zio.elasticsearch.query.ValidationMethod.IgnoreMalformed]]: Accepts geo points with invalid latitude or | ||
* longitude | ||
* - [[zio.elasticsearch.query.ValidationMethod.Coerce]]: Additionally try and infer correct coordinates | ||
* @return | ||
* an instance of [[zio.elasticsearch.query.GeoDistanceQuery]] enriched with the `validationMethod` parameter. | ||
*/ | ||
def validationMethod(value: ValidationMethod): GeoBoundingBoxQuery[S] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also this one. |
||
} | ||
|
||
private[elasticsearch] final case class GeoBoundingBox[S]( | ||
field: String, | ||
topLeft: GeoPoint, | ||
bottomRight: GeoPoint, | ||
Comment on lines
+556
to
+557
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reorder these two. |
||
boost: Option[Double] = None, | ||
ignoreUnmapped: Option[Boolean] = None, | ||
queryName: Option[String] = None, | ||
validationMethod: Option[ValidationMethod] = None | ||
) extends GeoBoundingBoxQuery[S] { self => | ||
|
||
def boost(value: Double): GeoBoundingBoxQuery[S] = | ||
self.copy(boost = Some(value)) | ||
|
||
def ignoreUnmapped(value: Boolean = false): GeoBoundingBoxQuery[S] = | ||
self.copy(ignoreUnmapped = Some(value)) | ||
|
||
def name(value: String): GeoBoundingBoxQuery[S] = | ||
self.copy(queryName = Some(value)) | ||
|
||
def validationMethod(value: ValidationMethod): GeoBoundingBoxQuery[S] = | ||
self.copy(validationMethod = Some(value)) | ||
|
||
private[elasticsearch] override def toJson(fieldPath: Option[String]): Json = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Omit |
||
Obj( | ||
"geo_bounding_box" -> Obj( | ||
Chunk( | ||
Some( | ||
field -> Obj( | ||
Chunk( | ||
Some( | ||
"top_left" -> Obj( | ||
Chunk( | ||
Some("lat" -> topLeft.lat.toJson), | ||
Some("lon" -> topLeft.lon.toJson) | ||
).flatten: _* | ||
) | ||
), | ||
Some( | ||
"bottom_right" -> Obj( | ||
Chunk( | ||
Some("lat" -> bottomRight.lat.toJson), | ||
Some("lon" -> bottomRight.lon.toJson) | ||
).flatten: _* | ||
) | ||
) | ||
).flatten: _* | ||
) | ||
), | ||
boost.map("boost" -> _.toJson), | ||
ignoreUnmapped.map("ignore_unmapped" -> _.toJson), | ||
queryName.map("_name" -> _.toJson), | ||
validationMethod.map("validation_method" -> _.toString.toJson) | ||
).flatten: _* | ||
) | ||
) | ||
} | ||
|
||
sealed trait GeoDistanceQuery[S] extends ElasticQuery[S] { | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use named parameters.