diff --git a/modules/library/src/main/scala/zio/elasticsearch/ElasticQuery.scala b/modules/library/src/main/scala/zio/elasticsearch/ElasticQuery.scala index 5c0ebb8f9..0609124d7 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/ElasticQuery.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/ElasticQuery.scala @@ -1,5 +1,6 @@ package zio.elasticsearch +import zio.elasticsearch.ElasticQuery._ import zio.json.ast.Json import zio.json.ast.Json.{Arr, Bool, Num, Obj, Str} @@ -9,6 +10,17 @@ sealed trait ElasticQuery { self => final def asJsonBody: Json = Obj("query" -> self.asJson) + def boost(value: Float): ElasticQuery = + self match { + case q: Term => q.copy(boost = Option(value)) + case _ => self + } + + def caseInsensitive(value: Boolean): ElasticQuery = + self match { + case q: Term => q.copy(caseInsensitive = Option(value)) + case _ => self + } } object ElasticQuery { @@ -22,8 +34,13 @@ object ElasticQuery { def matches(field: String, query: Long): ElasticQuery = Match(field, query) - def term(field: String, value: String): ElasticQuery = - Term(field, value) + def term( + field: String, + value: String, + boost: Option[Float] = None, + caseInsensitive: Option[Boolean] = None + ): ElasticQuery = + Term(field, value, boost, caseInsensitive) def boolQuery(): BoolQuery = BoolQuery.empty @@ -56,8 +73,18 @@ object ElasticQuery { } } - private[elasticsearch] final case class Term(field: String, value: String) extends ElasticQuery { - override def asJson: Json = - Obj("term" -> Obj(field -> Str(value))) + private[elasticsearch] final case class Term( + field: String, + value: String, + boost: Option[Float], + caseInsensitive: Option[Boolean] + ) extends ElasticQuery { self => + override def asJson: Json = { + val termFields = + Some("value" -> Str(value)) ++ boost.map(float => "boost" -> Num(float)) ++ caseInsensitive.map(boolean => + "case_insensitive" -> Bool(boolean) + ) + Obj("term" -> Obj(field -> Obj(termFields.toSeq: _*))) + } } }