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 1 commit
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,49 @@ object ElasticQuery {
final def prefix(field: String, value: String): Prefix[Any] =
Prefix(field = field, value = value, caseInsensitive = 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
Collaborator

Choose a reason for hiding this comment

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

Since this fields parameter can be empty list, maybe we shouldn't ask for it here. In other method you don't have it as parameter, so I would do it like this here too.

QueryString[S](
query = query,
fields = Chunk.fromIterable(fields.map(_.toString)),
defaultField = None,
boost = None,
minimumShouldMatch = 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): QueryStringQuery[Any] =
QueryString(
query = query,
fields = Chunk.empty,
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, Null, Obj, Str}
import zio.schema.Schema

sealed trait ElasticQuery[-S] { self =>
Expand Down Expand Up @@ -1016,6 +1016,53 @@ private[elasticsearch] final case class Prefix[S](
}
}

sealed trait QueryStringQuery[S] extends ElasticQuery[S]
with HasFields[QueryStringQuery, S]
with HasBoost[QueryStringQuery[S]]
with HasMinimumShouldMatch[QueryStringQuery[S]]

private[elasticsearch] final case class QueryString[S](
query: String,
fields: Chunk[String],
defaultField: Option[String],
boost: Option[Double],
minimumShouldMatch: Option[Int]
) extends QueryStringQuery[S] { self =>

def fields(field: String, fields: String*): QueryStringQuery[S] =
copy(fields = Chunk.fromIterable(field +: fields))

def fields[S1 <: S: Schema](fields: Chunk[Field[S1, _]]): QueryStringQuery[S1] =
copy(fields = fields.map(_.toString))

def fields[S1 <: S: Schema](field: Field[S1, _], fields: Field[S1, _]*): QueryStringQuery[S1] =
self.copy(fields = Chunk.fromIterable((field +: fields).map(_.toString)))

def boost(value: Double): QueryStringQuery[S] =
self.copy(boost = Some(value))

def minimumShouldMatch(value: Int): QueryStringQuery[S] =
self.copy(minimumShouldMatch = Some(value))

override def toJson(fieldPath: Option[String]): Json = {

val fieldsJson = if (fields.nonEmpty) Some("fields" -> Arr(fields.map(_.toJson))) else None
Obj(
"query" -> Obj(
"query_string" -> Obj(
List(
Some("query" -> Str(query)),
defaultField.map(df => "default_field" -> Str(df)),
fieldsJson,
boost.map(b => "boost" -> Json.Num(b)),
minimumShouldMatch.map(msm => "minimum_should_match" -> Json.Num(msm))
).flatten: _*
)
)
)
}
}

sealed trait RangeQuery[S, A, LB <: LowerBound, UB <: UpperBound]
extends ElasticQuery[S]
with HasBoost[RangeQuery[S, A, LB, UB]]
Expand Down Expand Up @@ -1280,7 +1327,8 @@ private[elasticsearch] final case class Wildcard[S](
value: String,
boost: Option[Double],
caseInsensitive: Option[Boolean]
) extends WildcardQuery[S] { self =>
) extends WildcardQuery[S] {
self =>

def boost(value: Double): WildcardQuery[S] =
self.copy(boost = Some(value))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1514,6 +1514,13 @@ object ElasticQuerySpec extends ZIOSpecDefault {
)
)
},
test("queryStringQuery"){
val query = range("testField")
val queryNoFields = queryStringQuery("test")
val queryWithFields = queryStringQuery("test").fields("stringField1", "stringField2")
Copy link
Member

Choose a reason for hiding this comment

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

Provide queryWithFields where fields aren't plain strings.

val queryWithMinShouldMatch = queryNoFields.minimumShouldMatch(2)

},
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