Skip to content

Commit

Permalink
Add optional fields to term query
Browse files Browse the repository at this point in the history
  • Loading branch information
mvelimir committed Dec 18, 2022
1 parent 9566f46 commit a1aa542
Showing 1 changed file with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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}

Expand All @@ -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 {
Expand All @@ -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

Expand Down Expand Up @@ -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: _*)))
}
}
}

0 comments on commit a1aa542

Please sign in to comment.