-
Notifications
You must be signed in to change notification settings - Fork 17
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 prefix query #258
Changes from 2 commits
1afdcca
6e1103b
381abe8
ae04900
aedad4d
91b9ad1
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 |
---|---|---|
|
@@ -544,6 +544,38 @@ object ElasticQuery { | |
final def nested(path: String, query: ElasticQuery[_]): NestedQuery[Any] = | ||
Nested(path = path, query = query, scoreMode = None, ignoreUnmapped = None, innerHitsField = None) | ||
|
||
/** | ||
* Constructs a type-safe instance of [[zio.elasticsearch.query.PrefixQuery]] using the specified parameters. | ||
* [[zio.elasticsearch.query.PrefixQuery]] is used for matching documents that contain a specific prefix in a provided | ||
* field. | ||
* | ||
* @param field | ||
* the type-safe field for which query is specified for | ||
* @param value | ||
* the value that will be used for the query, represented by an instance of type `A` | ||
* @tparam S | ||
* document for which field query is executed | ||
* @return | ||
* an instance of [[zio.elasticsearch.query.PrefixQuery]] that represents the prefix query to be performed. | ||
*/ | ||
final def prefix[S](field: Field[S, String], value: String): PrefixQuery[S] = | ||
Prefix(field = field.toString, value = value, caseInsensitive = None) | ||
|
||
/** | ||
* Constructs an instance of [[zio.elasticsearch.query.PrefixQuery]] using the specified parameters. | ||
* [[zio.elasticsearch.query.PrefixQuery]] is used for matching documents that contain a specific prefix in a provided | ||
* field. | ||
* | ||
* @param field | ||
* the field for which query is specified for | ||
* @param value | ||
* the value that will be used for the query, represented by an instance of type `A` | ||
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. Same here. |
||
* @return | ||
* an instance of [[zio.elasticsearch.query.PrefixQuery]] that represents the prefix query to be performed. | ||
*/ | ||
final def prefix(field: String, value: String): Prefix[Any] = | ||
Prefix(field = field, value = value, caseInsensitive = None) | ||
|
||
/** | ||
* Constructs a type-safe unbounded instance of [[zio.elasticsearch.query.RangeQuery]] using the specified parameters. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -665,6 +665,25 @@ private[elasticsearch] case object Unbounded extends LowerBound with UpperBound | |
private[elasticsearch] def toJson: Option[(String, Json)] = None | ||
} | ||
|
||
sealed trait PrefixQuery[S] extends ElasticQuery[S] with HasCaseInsensitive[PrefixQuery[S]] | ||
|
||
private[elasticsearch] final case class Prefix[S]( | ||
field: String, | ||
value: String, | ||
caseInsensitive: Option[Boolean] | ||
) extends PrefixQuery[S] { self => | ||
|
||
override def caseInsensitive(value: Boolean): PrefixQuery[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. Can you omit |
||
self.copy(caseInsensitive = Some(value)) | ||
|
||
override private[elasticsearch] 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. Here also. |
||
val prefixFields = Some("value" -> value.toJson) ++ caseInsensitive.map( | ||
"case_insensitive" -> _.toJson | ||
) | ||
Obj("prefix" -> Obj(fieldPath.foldRight(field)(_ + "." + _) -> Obj(Chunk.fromIterable(prefixFields)))) | ||
} | ||
} | ||
|
||
sealed trait RangeQuery[S, A, LB <: LowerBound, UB <: UpperBound] | ||
extends ElasticQuery[S] | ||
with HasBoost[RangeQuery[S, A, LB, UB]] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2531,6 +2531,50 @@ object ElasticQuerySpec extends ZIOSpecDefault { | |
assert(queryWithScoreMode.toJson(fieldPath = None))(equalTo(expectedWithScoreMode.toJson)) && | ||
assert(queryWithAllParams.toJson(fieldPath = None))(equalTo(expectedWithAllParams.toJson)) | ||
}, | ||
test("prefix") { | ||
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 provide |
||
val query = prefix(TestDocument.stringField, "test") | ||
val queryWithCaseInsensitive = prefix(TestDocument.stringField, "test").caseInsensitiveTrue | ||
val queryWithAllParams = prefix(TestDocument.stringField, "test").caseInsensitiveFalse | ||
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. Remove this check. 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. We have only one parameter, so |
||
|
||
val expected = | ||
""" | ||
|{ | ||
| "prefix": { | ||
| "stringField": { | ||
| "value": "test" | ||
| } | ||
| } | ||
|} | ||
|""".stripMargin | ||
|
||
val expectedWithCaseInsensitive = | ||
""" | ||
|{ | ||
| "prefix": { | ||
| "stringField": { | ||
| "value": "test", | ||
| "case_insensitive": true | ||
| } | ||
| } | ||
|} | ||
|""".stripMargin | ||
|
||
val expectedWithAllParams = | ||
""" | ||
|{ | ||
| "prefix": { | ||
| "stringField": { | ||
| "value": "test", | ||
| "case_insensitive": false | ||
| } | ||
| } | ||
|} | ||
|""".stripMargin | ||
|
||
assert(query.toJson(fieldPath = None))(equalTo(expected.toJson)) && | ||
assert(queryWithCaseInsensitive.toJson(fieldPath = None))(equalTo(expectedWithCaseInsensitive.toJson)) && | ||
assert(queryWithAllParams.toJson(fieldPath = None))(equalTo(expectedWithAllParams.toJson)) | ||
}, | ||
test("range") { | ||
val queryEmpty = range(TestDocument.intField) | ||
val queryEmptyWithBoost = range(TestDocument.intField).boost(3.14) | ||
|
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.
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.
Can you replace this for term method also?