Skip to content

Commit

Permalink
Scala tweak
Browse files Browse the repository at this point in the history
  • Loading branch information
lenguyenthanh committed Jun 10, 2024
1 parent 59d402f commit 7b4ee01
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 26 deletions.
2 changes: 1 addition & 1 deletion modules/core/src/main/scala/ESClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package lila.search

import cats.MonadThrow
import cats.syntax.all.*
import com.sksamuel.elastic4s.ElasticDsl.{ RichFuture as _, * }
import com.sksamuel.elastic4s.ElasticDsl.*
import com.sksamuel.elastic4s.fields.ElasticField
import com.sksamuel.elastic4s.{
ElasticClient,
Expand Down
2 changes: 1 addition & 1 deletion modules/core/src/main/scala/forum.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ object ForumQuery:
multiMatchQuery(term).fields(searchableFields*)
} ::: List(
parsed(query.text)("user").map { termQuery(Fields.author, _) },
(!query.troll).option(termQuery(Fields.troll, false))
Option.when(!query.troll)(termQuery(Fields.troll, false))
).flatten
)

Expand Down
29 changes: 14 additions & 15 deletions modules/core/src/main/scala/game.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package lila.search
package game

import com.sksamuel.elastic4s.ElasticDsl.{ RichFuture as _, * }
import com.sksamuel.elastic4s.ElasticDsl.*
import org.joda.time.DateTime

import scala.concurrent.duration.*
import com.sksamuel.elastic4s.requests.searches.queries.Query
import com.sksamuel.elastic4s.requests.searches.term.TermQuery

case class Game(
user1: Option[String] = None,
Expand Down Expand Up @@ -74,7 +76,7 @@ object Mapping:
)

object GameQuery:
given query: lila.search.Queryable[Game] = new lila.search.Queryable[Game]:
given query: Queryable[Game] = new:

val timeout = 5.seconds
val index = "game"
Expand All @@ -88,26 +90,24 @@ object GameQuery:
.size(size.value)
.timeout(timeout)

def countDef(query: Game) = (search(index).query(makeQuery(query)).size(0)).timeout(timeout)
def countDef(query: Game) = search(index).query(makeQuery(query)).size(0).timeout(timeout)

private def makeQuery(query: Game) =
private def makeQuery(query: Game): Query =

import query.*
def usernames = List(user1, user2).flatten

def hasAiQueries =
hasAi.toList.map { a =>
hasAi.toList.map: a =>
a.fold(existsQuery(Fields.ai), not(existsQuery(Fields.ai)))
}

def toQueries(query: Option[?], name: String) =
query.toList.map {
def toQueries(query: Option[?], name: String): List[TermQuery] =
query.toList.map:
case s: String => termQuery(name, s.toLowerCase)
case x => termQuery(name, x)
}

List(
usernames.map { termQuery(Fields.uids, _) },
usernames.map(termQuery(Fields.uids, _)),
toQueries(winner, Fields.winner),
toQueries(loser, Fields.loser),
toQueries(winnerColor, Fields.winnerColor),
Expand All @@ -118,8 +118,8 @@ object GameQuery:
clock.inc.queries(Fields.clockInc),
date.map(Date.formatter.print).queries(Fields.date),
hasAiQueries,
(hasAi.getOrElse(true)).fold(aiLevel.queries(Fields.ai), Nil),
if perf.nonEmpty then List(termsQuery(Fields.perf, perf)) else Nil,
hasAi.getOrElse(true).fold(aiLevel.queries(Fields.ai), Nil),
perf.nonEmpty.fold(List(termsQuery(Fields.perf, perf)), Nil),
toQueries(source, Fields.source),
toQueries(rated, Fields.rated),
toQueries(status, Fields.status),
Expand All @@ -133,9 +133,8 @@ object GameQuery:
case class Sorting(f: String, order: String):
import com.sksamuel.elastic4s.requests.searches.sort.SortOrder
def definition =
fieldSort {
(Sorting.fieldKeys contains f).fold(f, Sorting.default.f)
}.order((order.toLowerCase == "asc").fold(SortOrder.ASC, SortOrder.DESC))
fieldSort(Sorting.fieldKeys.contains(f).fold(f, Sorting.default.f))
.order((order.toLowerCase == "asc").fold(SortOrder.ASC, SortOrder.DESC))

object Sorting:

Expand Down
6 changes: 1 addition & 5 deletions modules/core/src/main/scala/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,4 @@ package object search:
val format = "yyyy-MM-dd HH:mm:ss"
val formatter: DateTimeFormatter = DateTimeFormat.forPattern(format)

implicit class LilaPimpedBoolean(self: Boolean):

def fold[A](t: => A, f: => A): A = if self then t else f

def option[A](a: => A): Option[A] = if self then Some(a) else None
extension (self: Boolean) def fold[A](t: => A, f: => A): A = if self then t else f
6 changes: 3 additions & 3 deletions modules/core/src/main/scala/study.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package lila.search
package study

import com.sksamuel.elastic4s.ElasticDsl.{ RichFuture as _, * }
import com.sksamuel.elastic4s.requests.searches.queries.Query as QueryDefinition
import com.sksamuel.elastic4s.ElasticDsl.*
import com.sksamuel.elastic4s.requests.searches.queries.Query
import com.sksamuel.elastic4s.requests.searches.sort.SortOrder

case class Study(text: String, userId: Option[String])
Expand Down Expand Up @@ -54,7 +54,7 @@ object StudyQuery:
private def parsed(text: String) = QueryParser(text, List("owner", "member"))

private def makeQuery(query: Study) = {
val matcher: QueryDefinition =
val matcher: Query =
if parsed(query.text).terms.isEmpty then matchAllQuery()
else
multiMatchQuery(
Expand Down
2 changes: 1 addition & 1 deletion modules/core/src/main/scala/team.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package lila.search
package team

import com.sksamuel.elastic4s.ElasticDsl.{ RichFuture as _, * }
import com.sksamuel.elastic4s.ElasticDsl.*
import com.sksamuel.elastic4s.requests.searches.sort.SortOrder

case class Team(text: String)
Expand Down

0 comments on commit 7b4ee01

Please sign in to comment.