Skip to content
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

(dsl): Support 'geoDistance' query #211

Merged
merged 6 commits into from
May 11, 2023
Merged

Conversation

markaya
Copy link
Contributor

@markaya markaya commented May 9, 2023

Summary:

  • Add geo-distance query
  • Add unit tests for geo-distance query methods
  • Add integration tests for geo-distance query

Resolves geoDistanceQuery in #91

@@ -108,6 +108,74 @@ object ElasticQuery {
final def filter(queries: ElasticQuery[Any]*): BoolQuery[Any] =
Bool[Any](filter = queries.toList, must = Nil, mustNot = Nil, should = Nil, boost = None, minimumShouldMatch = None)

/**
* Constructs an instance of [[zio.elasticsearch.query.GeoDistanceQuery]] using the specified parameters.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use Constructs a type-safe instance of...?

*
* @param field
* the type-safe field for which query is specified for
* @param longitudeAndLatitude
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of this name.

GeoDistance(field = field, point = Left((longitude, latitude)))

/**
* Constructs an instance of [[zio.elasticsearch.query.GeoDistanceQuery]] using the specified parameters.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use type-safe here as well?

* @param field
* the type-safe field for which query is specified for
* @param longitudeAndLatitude
* longitude and latitude of desired point written as string(e.g. "40,31") or geo hash (e.g. "drm3btev3e86")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing " " between as string and (e.g. ...).

* @param field
* the field for which query is specified for
* @param longitudeAndLatitude
* longitude and latitude of desired point written as string(e.g. "40,31") or geo hash (e.g. "drm3btev3e86")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing " " between as string and (e.g. ...).

/**
* sets the `distanceType` parameter for the [[zio.elasticsearch.query.GeoDistanceQuery]]
*
* Defines how to compute the distance. Can either be [[zio.elasticsearch.query.DistanceType.Arc]] (default), or
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check this and you can use a similar format.


def distanceType(value: DistanceType): GeoDistanceQuery[S] = self.copy(distanceType = Some(value))

private def getPointJson: (String, Json) = point match {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move this to paramsToJson if it's used only once. Also, no need for a method.

@@ -833,6 +836,48 @@ object ElasticQuerySpec extends ZIOSpecDefault {
)
)
)
},
test("geoDistance") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep this sorted.

test("bool")
...
test("exists")
...
test("geoDistance")
...
test("match")
test("matchAll")
...

@@ -2324,6 +2369,112 @@ object ElasticQuerySpec extends ZIOSpecDefault {
assert(queryWithBoost.toJson)(equalTo(expectedWithBoost.toJson)) &&
assert(queryWithCaseInsensitive.toJson)(equalTo(expectedWithCaseInsensitive.toJson)) &&
assert(queryWithAllParams.toJson)(equalTo(expectedWithAllParams.toJson))
},
test("geoDistance") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

|}
|""".stripMargin

assert(query.toJson)(equalTo(expected.toJson))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are missing && here.

@markaya markaya force-pushed the task-support-geodistance-query branch from a2ad8ab to c919276 Compare May 10, 2023 11:10
@drmarjanovic drmarjanovic changed the title Add Geo-distance query (dsl): Support 'geoDistance' query May 10, 2023
* @param field
* the type-safe field for which query is specified for
* @param longitude
* longitude of desired point
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...of the desired point.

* @param longitude
* longitude of desired point
* @param latitude
* latitude of desired point
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

of the desired point.

* @param field
* the field for which query is specified for
* @param longitude
* longitude of desired point
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for every place in this file.


package zio.elasticsearch.query

final case class Distance(distanceValue: Double, distanceUnit: DistanceUnit) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be private[elasticsearch]?

* [[zio.elasticsearch.query.DistanceUnit]].
*
* @param value
* the [[Double]] value for a non-negative real number used for distance
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just: a non-negative number...


/**
* Sets the `distanceType` parameter for the [[zio.elasticsearch.query.GeoDistanceQuery]].
*
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove empty line.

* @param unit
* the [[zio.elasticsearch.query.DistanceUnit]] in which we want to represent the distance
* @return
* a new instance of the [[zio.elasticsearch.query.GeoDistanceQuery]] with the score value set.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...with the distance..??

* - [[zio.elasticsearch.query.DistanceType.Arc]]: Default algorithm
* - [[zio.elasticsearch.query.DistanceType.Plane]]: Faster, but inaccurate on long distances and close to the poles
* @return
* a new instance of the [[zio.elasticsearch.query.GeoDistanceQuery]] with the score value set.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

score value or distanceType value?

* @param value
* the [[String]] value to represent the name field
* @return
* a new instance of the [[zio.elasticsearch.query.GeoDistanceQuery]] with the score value set.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe apply everywhere "...enriched with the paramName param."

Comment on lines 177 to 180
distance: Option[Distance] = None,
distanceType: Option[DistanceType] = None,
queryName: Option[String] = None,
validationMethod: Option[ValidationMethod] = None
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we have predefined values here?

import zio.elasticsearch.FieldAccessorBuilder
import zio.schema.{DeriveSchema, Schema}

case class Location(lat: Double, lon: Double)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use final here.

val queryWithName =
geoDistance(TestDocument.stringField, 20.0, 21.1).name("name")
val queryWithValidationMethod =
geoDistance(TestDocument.stringField, 20.0, 21.1).validationMethod(IgnoreMalformed)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add queryWithAllParams.

@dbulaja98 dbulaja98 merged commit 1a2cc3b into main May 11, 2023
@dbulaja98 dbulaja98 deleted the task-support-geodistance-query branch May 11, 2023 11:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants