Skip to content

Commit 653706e

Browse files
committed
Added QueryStringQuery
1 parent 264dd05 commit 653706e

File tree

3 files changed

+100
-2
lines changed

3 files changed

+100
-2
lines changed

modules/library/src/main/scala/zio/elasticsearch/ElasticQuery.scala

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,49 @@ object ElasticQuery {
899899
final def prefix(field: String, value: String): Prefix[Any] =
900900
Prefix(field = field, value = value, caseInsensitive = None)
901901

902+
/**
903+
* Constructs a type-safe instance of [[zio.elasticsearch.query.QueryStringQuery]] using the specified
904+
* parameters. [[zio.elasticsearch.query.QueryStringQuery]] supports query strings with simple syntax for
905+
* searching multiple fields.
906+
*
907+
* @param fields
908+
* the type-safe fields to be searched
909+
* @param query
910+
* the query string to search for
911+
* @tparam S
912+
* the document type on which the query is executed
913+
* @return
914+
* an instance of [[zio.elasticsearch.query.QueryStringQuery]] that represents the query to be performed.
915+
*/
916+
final def queryStringQuery[S: Schema](query: String, fields: Field[S, _]*): QueryStringQuery[S] =
917+
QueryString[S](
918+
query = query,
919+
fields = Chunk.fromIterable(fields.map(_.toString)),
920+
defaultField = None,
921+
boost = None,
922+
minimumShouldMatch = None
923+
)
924+
925+
/**
926+
* Constructs an instance of [[zio.elasticsearch.query.QueryStringQuery]] using the specified parameters.
927+
* [[zio.elasticsearch.query.QueryStringQuery]] supports query strings with simple syntax for searching multiple
928+
* fields.
929+
*
930+
* @param query
931+
* the query string to search for
932+
* @return
933+
* an instance of [[zio.elasticsearch.query.QueryStringQuery]] that represents the query to be performed.
934+
*/
935+
final def queryStringQuery(query: String): QueryStringQuery[Any] =
936+
QueryString(
937+
query = query,
938+
fields = Chunk.empty,
939+
defaultField = None,
940+
boost = None,
941+
minimumShouldMatch = None
942+
)
943+
944+
902945
/**
903946
* Constructs a type-safe unbounded instance of [[zio.elasticsearch.query.RangeQuery]] using the specified parameters.
904947
*

modules/library/src/main/scala/zio/elasticsearch/query/Queries.scala

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import zio.elasticsearch.Field
2222
import zio.elasticsearch.query.options._
2323
import zio.elasticsearch.query.sort.options.HasFormat
2424
import zio.json.ast.Json
25-
import zio.json.ast.Json.{Arr, Obj}
25+
import zio.json.ast.Json.{Arr, Null, Obj, Str}
2626
import zio.schema.Schema
2727

2828
sealed trait ElasticQuery[-S] { self =>
@@ -1016,6 +1016,53 @@ private[elasticsearch] final case class Prefix[S](
10161016
}
10171017
}
10181018

1019+
sealed trait QueryStringQuery[S] extends ElasticQuery[S]
1020+
with HasFields[QueryStringQuery, S]
1021+
with HasBoost[QueryStringQuery[S]]
1022+
with HasMinimumShouldMatch[QueryStringQuery[S]]
1023+
1024+
private[elasticsearch] final case class QueryString[S](
1025+
query: String,
1026+
fields: Chunk[String],
1027+
defaultField: Option[String],
1028+
boost: Option[Double],
1029+
minimumShouldMatch: Option[Int]
1030+
) extends QueryStringQuery[S] { self =>
1031+
1032+
def fields(field: String, fields: String*): QueryStringQuery[S] =
1033+
copy(fields = Chunk.fromIterable(field +: fields))
1034+
1035+
def fields[S1 <: S: Schema](fields: Chunk[Field[S1, _]]): QueryStringQuery[S1] =
1036+
copy(fields = fields.map(_.toString))
1037+
1038+
def fields[S1 <: S: Schema](field: Field[S1, _], fields: Field[S1, _]*): QueryStringQuery[S1] =
1039+
self.copy(fields = Chunk.fromIterable((field +: fields).map(_.toString)))
1040+
1041+
def boost(value: Double): QueryStringQuery[S] =
1042+
self.copy(boost = Some(value))
1043+
1044+
def minimumShouldMatch(value: Int): QueryStringQuery[S] =
1045+
self.copy(minimumShouldMatch = Some(value))
1046+
1047+
override def toJson(fieldPath: Option[String]): Json = {
1048+
1049+
val fieldsJson = if (fields.nonEmpty) Some("fields" -> Arr(fields.map(_.toJson))) else None
1050+
Obj(
1051+
"query" -> Obj(
1052+
"query_string" -> Obj(
1053+
List(
1054+
Some("query" -> Str(query)),
1055+
defaultField.map(df => "default_field" -> Str(df)),
1056+
fieldsJson,
1057+
boost.map(b => "boost" -> Json.Num(b)),
1058+
minimumShouldMatch.map(msm => "minimum_should_match" -> Json.Num(msm))
1059+
).flatten: _*
1060+
)
1061+
)
1062+
)
1063+
}
1064+
}
1065+
10191066
sealed trait RangeQuery[S, A, LB <: LowerBound, UB <: UpperBound]
10201067
extends ElasticQuery[S]
10211068
with HasBoost[RangeQuery[S, A, LB, UB]]
@@ -1280,7 +1327,8 @@ private[elasticsearch] final case class Wildcard[S](
12801327
value: String,
12811328
boost: Option[Double],
12821329
caseInsensitive: Option[Boolean]
1283-
) extends WildcardQuery[S] { self =>
1330+
) extends WildcardQuery[S] {
1331+
self =>
12841332

12851333
def boost(value: Double): WildcardQuery[S] =
12861334
self.copy(boost = Some(value))

modules/library/src/test/scala/zio/elasticsearch/ElasticQuerySpec.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,6 +1514,13 @@ object ElasticQuerySpec extends ZIOSpecDefault {
15141514
)
15151515
)
15161516
},
1517+
test("queryStringQuery"){
1518+
val query = range("testField")
1519+
val queryNoFields = queryStringQuery("test")
1520+
val queryWithFields = queryStringQuery("test").fields("stringField1", "stringField2")
1521+
val queryWithMinShouldMatch = queryNoFields.minimumShouldMatch(2)
1522+
1523+
},
15171524
test("range") {
15181525
val query = range("testField")
15191526
val queryString = range(TestDocument.stringField)

0 commit comments

Comments
 (0)