-
Notifications
You must be signed in to change notification settings - Fork 20
Added QueryStringQuery #648
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 |
---|---|---|
|
@@ -22,7 +22,7 @@ import zio.elasticsearch.Field | |
import zio.elasticsearch.query.options._ | ||
import zio.elasticsearch.query.sort.options.HasFormat | ||
import zio.json.ast.Json | ||
import zio.json.ast.Json.{Arr, Obj} | ||
import zio.json.ast.Json.{Arr, Obj, Str} | ||
import zio.schema.Schema | ||
|
||
sealed trait ElasticQuery[-S] { self => | ||
|
@@ -1016,6 +1016,49 @@ private[elasticsearch] final case class Prefix[S]( | |
} | ||
} | ||
|
||
sealed trait QueryStringQuery[S] | ||
extends ElasticQuery[S] | ||
with HasBoost[QueryStringQuery[S]] | ||
with HasMinimumShouldMatch[QueryStringQuery[S]] | ||
|
||
private[elasticsearch] final case class QueryString[S]( | ||
JuliaIlic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
defaultField: Option[String], | ||
fields: Chunk[String], | ||
query: String, | ||
boost: Option[Double], | ||
minimumShouldMatch: Option[Int] | ||
) extends QueryStringQuery[S] { self => | ||
|
||
def boost(value: Double): QueryStringQuery[S] = | ||
JuliaIlic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.copy(boost = Some(value)) | ||
|
||
def fields(field: String, fields: String*): QueryStringQuery[S] = | ||
self.copy(fields = Chunk.fromIterable(field +: fields)) | ||
|
||
def fields(fields: Chunk[String]): QueryStringQuery[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. Do we need this? |
||
self.copy(fields = fields) | ||
|
||
def minimumShouldMatch(value: Int): QueryStringQuery[S] = | ||
self.copy(minimumShouldMatch = Some(value)) | ||
|
||
private[elasticsearch] def toJson(fieldPath: Option[String]): Json = { | ||
val fieldsJson = | ||
if (fields.nonEmpty) Some("fields" -> Arr(fields.map(Str(_)))) | ||
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 we use |
||
else None | ||
|
||
val params = Chunk( | ||
Some("query" -> Str(query)), | ||
defaultField.map("default_field" -> Str(_)), | ||
fieldsJson, | ||
boost.map("boost" -> Json.Num(_)), | ||
minimumShouldMatch.map("minimum_should_match" -> Json.Num(_)) | ||
).flatten | ||
|
||
Obj("query" -> Obj("query_string" -> Obj(params))) | ||
} | ||
|
||
} | ||
|
||
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 |
---|---|---|
|
@@ -1514,6 +1514,70 @@ object ElasticQuerySpec extends ZIOSpecDefault { | |
) | ||
) | ||
}, | ||
test("queryStringQuery") { | ||
val queryNoFields = queryStringQuery("(new york city) OR (big apple)") | ||
val queryWithFields = queryStringQuery("(new york city) OR (big apple)") | ||
.fields(Chunk("title", "description")) | ||
val queryWithTypedFields = queryStringQuery("(new york city) OR (big apple)") | ||
.fields(Chunk(TestDocument.stringField.toString)) | ||
val queryWithMinShouldMatch = queryNoFields.minimumShouldMatch(1) | ||
val queryAllParams = queryWithFields.minimumShouldMatch(1).boost(2.0) | ||
assert(queryNoFields)( | ||
equalTo( | ||
QueryString[Any]( | ||
query = "(new york city) OR (big apple)", | ||
fields = Chunk.empty, | ||
defaultField = None, | ||
boost = None, | ||
minimumShouldMatch = None | ||
) | ||
) | ||
) && | ||
assert(queryWithFields)( | ||
equalTo( | ||
QueryString[Any]( | ||
query = "(new york city) OR (big apple)", | ||
fields = Chunk("title", "description"), | ||
defaultField = None, | ||
boost = None, | ||
minimumShouldMatch = None | ||
) | ||
) | ||
) && | ||
assert(queryWithTypedFields)( | ||
equalTo( | ||
QueryString[Any]( | ||
query = "(new york city) OR (big apple)", | ||
fields = Chunk("string"), | ||
defaultField = None, | ||
boost = None, | ||
minimumShouldMatch = None | ||
) | ||
) | ||
) && | ||
assert(queryWithMinShouldMatch)( | ||
equalTo( | ||
QueryString[Any]( | ||
query = "(new york city) OR (big apple)", | ||
fields = Chunk.empty, | ||
defaultField = None, | ||
boost = None, | ||
minimumShouldMatch = Some(1) | ||
) | ||
) | ||
) && | ||
assert(queryAllParams)( | ||
equalTo( | ||
QueryString[Any]( | ||
query = "(new york city) OR (big apple)", | ||
fields = Chunk("title", "description"), | ||
defaultField = None, | ||
boost = Some(2.0), | ||
minimumShouldMatch = Some(1) | ||
) | ||
) | ||
) | ||
}, | ||
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 are missing an assertion here. |
||
test("range") { | ||
val query = range("testField") | ||
val queryString = range(TestDocument.stringField) | ||
|
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.
Does it make sense for fields to be empty?