Skip to content

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,48 @@ object ElasticQuery {
final def prefix(field: String, value: String): Prefix[Any] =
Prefix(field = field, value = value, caseInsensitive = None)

/**
* Constructs an instance of [[zio.elasticsearch.query.QueryStringQuery]] using the specified parameters.
* [[zio.elasticsearch.query.QueryStringQuery]] supports query strings with simple syntax for searching multiple
* fields.
*
* @param query
* the query string to search for
* @return
* an instance of [[zio.elasticsearch.query.QueryStringQuery]] that represents the query to be performed.
*/
final def queryStringQuery(query: String): QueryString[Any] =
QueryString(
query = query,
fields = Chunk.empty,
defaultField = None,
boost = None,
minimumShouldMatch = None
)

/**
* Constructs a type-safe instance of [[zio.elasticsearch.query.QueryStringQuery]] using the specified parameters.
* [[zio.elasticsearch.query.QueryStringQuery]] supports query strings with simple syntax for searching multiple
* fields.
*
* @param fields
* the type-safe fields to be searched
* @param query
* the query string to search for
* @tparam S
* the document type on which the query is executed
* @return
* an instance of [[zio.elasticsearch.query.QueryStringQuery]] that represents the query to be performed.
*/
final def queryStringQuery[S: Schema](query: String, fields: Field[S, _]*): QueryStringQuery[S] =
Copy link
Member

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?

QueryString[S](
query = query,
fields = Chunk.fromIterable(fields.map(_.toString)),
defaultField = None,
boost = None,
minimumShouldMatch = None
)

/**
* Constructs a type-safe unbounded instance of [[zio.elasticsearch.query.RangeQuery]] using the specified parameters.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down Expand Up @@ -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](
defaultField: Option[String],
fields: Chunk[String],
query: String,
boost: Option[Double],
minimumShouldMatch: Option[Int]
) extends QueryStringQuery[S] { self =>

def boost(value: Double): QueryStringQuery[S] =
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] =
Copy link
Member

Choose a reason for hiding this comment

The 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(_))))
Copy link
Member

Choose a reason for hiding this comment

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

Can we use .map here?

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]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
)
)
},
Copy link
Member

Choose a reason for hiding this comment

The 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)
Expand Down
Loading