Skip to content

Commit

Permalink
Add type parameter to ElasticQuery and propagate it through existing …
Browse files Browse the repository at this point in the history
…code
  • Loading branch information
mvelimir committed Mar 9, 2023
1 parent 5c2e234 commit e5bccca
Show file tree
Hide file tree
Showing 10 changed files with 334 additions and 241 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ final case class RepositoriesElasticsearch(elasticsearch: Elasticsearch) {
res <- elasticsearch.execute(ElasticRequest.deleteById(Index, DocumentId(id)).routing(routing).refreshFalse)
} yield res

def search(query: ElasticQuery[_]): Task[List[GitHubRepo]] =
def search(query: ElasticQuery[_, _]): Task[List[GitHubRepo]] =
elasticsearch.execute(ElasticRequest.search[GitHubRepo](Index, query))

private def routingOf(value: String): IO[IllegalArgumentException, Routing.Type] =
Expand Down Expand Up @@ -102,7 +102,7 @@ object RepositoriesElasticsearch {
def remove(organization: String, id: String): RIO[RepositoriesElasticsearch, DeletionOutcome] =
ZIO.serviceWithZIO[RepositoriesElasticsearch](_.remove(organization, id))

def search(query: ElasticQuery[_]): RIO[RepositoriesElasticsearch, List[GitHubRepo]] =
def search(query: ElasticQuery[_, _]): RIO[RepositoriesElasticsearch, List[GitHubRepo]] =
ZIO.serviceWithZIO[RepositoriesElasticsearch](_.search(query))

lazy val live: URLayer[Elasticsearch, RepositoriesElasticsearch] =
Expand Down
7 changes: 3 additions & 4 deletions modules/example/src/main/scala/example/api/Repositories.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package example.api

import example.{GitHubRepo, RepositoriesElasticsearch}
import zio.ZIO
import zio.elasticsearch.ElasticQuery.boolQuery
import zio.elasticsearch._
import zio.http._
import zio.http.model.Method
Expand Down Expand Up @@ -120,12 +119,12 @@ object Repositories {
.orDie
}

private def createElasticQuery(query: Criteria): ElasticQuery[_] =
private def createElasticQuery(query: Criteria): ElasticQuery[_, _] =
query match {
case CompoundCriteria(operator, filters) =>
operator match {
case And => boolQuery.must(filters.map(createElasticQuery): _*)
case Or => boolQuery.should(filters.map(createElasticQuery): _*)
case And => ElasticQuery.must(filters.map(createElasticQuery): _*)
case Or => ElasticQuery.should(filters.map(createElasticQuery): _*)
}
case DateCriteria(field, operator, value) =>
operator match {
Expand Down
51 changes: 32 additions & 19 deletions modules/library/src/main/scala/zio/elasticsearch/Boost.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,48 @@ import zio.elasticsearch.ElasticQueryType.{Bool, MatchAll, Range, Term, Wildcard
object Boost {

trait WithBoost[EQT <: ElasticQueryType] {
def withBoost(query: ElasticQuery[EQT], value: Double): ElasticQuery[EQT]
def withBoost[S](query: ElasticQuery[S, EQT], value: Double): ElasticQuery[S, EQT]
}

object WithBoost {
implicit val boolWithBoost: WithBoost[Bool] = (query: ElasticQuery[Bool], value: Double) =>
query match {
case q: BoolQuery => q.copy(boost = Some(value))
implicit val boolWithBoost: WithBoost[Bool] =
new WithBoost[Bool] {
def withBoost[S](query: ElasticQuery[S, Bool], value: Double): ElasticQuery[S, Bool] =
query match {
case q: BoolQuery[S] => q.copy(boost = Some(value))
}
}

implicit val matchAllWithBoost: WithBoost[MatchAll] = (query: ElasticQuery[MatchAll], value: Double) =>
query match {
case q: MatchAllQuery => q.copy(boost = Some(value))
implicit val matchAllWithBoost: WithBoost[MatchAll] =
new WithBoost[MatchAll] {
def withBoost[S](query: ElasticQuery[S, MatchAll], value: Double): ElasticQuery[S, MatchAll] =
query match {
case q: MatchAllQuery => q.copy(boost = Some(value))
}
}

implicit def rangeWithBoost[A, LB <: LowerBound, UB <: UpperBound]: WithBoost[Range[A, LB, UB]] =
(query: ElasticQuery[Range[A, LB, UB]], value: Double) =>
query match {
case q: RangeQuery[A, LB, UB] => q.copy(boost = Some(value))
}
new WithBoost[Range[A, LB, UB]] {
def withBoost[S](query: ElasticQuery[S, Range[A, LB, UB]], value: Double): ElasticQuery[S, Range[A, LB, UB]] =
query match {
case q: RangeQuery[_, A, LB, UB] => q.copy(boost = Some(value))
}
}

implicit def termWithBoost[A: ElasticPrimitive]: WithBoost[Term[A]] =
(query: ElasticQuery[Term[A]], value: Double) =>
query match {
case q: TermQuery[A] => q.copy(boost = Some(value))
}

implicit val wildcardWithBoost: WithBoost[Wildcard] = (query: ElasticQuery[Wildcard], value: Double) =>
query match {
case q: WildcardQuery => q.copy(boost = Some(value))
new WithBoost[Term[A]] {
def withBoost[S](query: ElasticQuery[S, Term[A]], value: Double): ElasticQuery[S, Term[A]] =
query match {
case q: TermQuery[_, A] => q.copy(boost = Some(value))
}
}

implicit val wildcardWithBoost: WithBoost[Wildcard] =
new WithBoost[Wildcard] {
def withBoost[S](query: ElasticQuery[S, Wildcard], value: Double): ElasticQuery[S, Wildcard] =
query match {
case q: WildcardQuery[_] => q.copy(boost = Some(value))
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,26 @@ import zio.elasticsearch.ElasticQueryType.{Term, Wildcard}
object CaseInsensitive {

trait WithCaseInsensitive[EQT <: ElasticQueryType] {
def withCaseInsensitive(query: ElasticQuery[EQT], value: Boolean): ElasticQuery[EQT]
def withCaseInsensitive[S](query: ElasticQuery[S, EQT], value: Boolean): ElasticQuery[S, EQT]
}

object WithCaseInsensitive {
implicit val termWithCaseInsensitiveString: WithCaseInsensitive[Term[String]] =
(query: ElasticQuery[Term[String]], value: Boolean) =>
query match {
case q: TermQuery[String] => q.copy(caseInsensitive = Some(value))
}

new WithCaseInsensitive[Term[String]] {
def withCaseInsensitive[S](
query: ElasticQuery[S, Term[String]],
value: Boolean
): ElasticQuery[S, Term[String]] =
query match {
case q: TermQuery[S, String] => q.copy(caseInsensitive = Some(value))
}
}
implicit val wildcardWithCaseInsensitiveString: WithCaseInsensitive[Wildcard] =
(query: ElasticQuery[Wildcard], value: Boolean) =>
query match {
case q: WildcardQuery => q.copy(caseInsensitive = Some(value))
}
new WithCaseInsensitive[Wildcard] {
def withCaseInsensitive[S](query: ElasticQuery[S, Wildcard], value: Boolean): ElasticQuery[S, Wildcard] =
query match {
case q: WildcardQuery[S] => q.copy(caseInsensitive = Some(value))
}
}
}
}
Loading

0 comments on commit e5bccca

Please sign in to comment.