Skip to content

Commit

Permalink
Merge pull request #227 from lichess-org/optimize-some-queries
Browse files Browse the repository at this point in the history
Optimize some queries
  • Loading branch information
lenguyenthanh authored Jun 10, 2024
2 parents 204bb90 + 46fdb19 commit a2cc30a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 39 deletions.
17 changes: 7 additions & 10 deletions modules/core/src/main/scala/forum.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,12 @@ object ForumQuery:

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

private def parsed(text: String) = QueryParser(text, List("user"))

private def makeQuery(query: Forum) = boolQuery().must(
parsed(query.text).terms.map { term =>
multiMatchQuery(term).fields(searchableFields*)
} ::: List(
parsed(query.text)("user").map { termQuery(Fields.author, _) },
Option.when(!query.troll)(termQuery(Fields.troll, false))
).flatten
)
private def makeQuery(query: Forum) =
val parsed = QueryParser(query.text, List("user"))
List(
parsed.terms.map(term => multiMatchQuery(term).fields(searchableFields*)),
parsed("user").map(termQuery(Fields.author, _)).toList,
Option.unless(query.troll)(termQuery(Fields.troll, false)).toList
).flatten.compile

private val searchableFields = List(Fields.body, Fields.topic, Fields.author)
4 changes: 1 addition & 3 deletions modules/core/src/main/scala/game.scala
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,7 @@ object GameQuery:
toQueries(analysed, Fields.analysed),
toQueries(whiteUser, Fields.whiteUser),
toQueries(blackUser, Fields.blackUser)
).flatten match
case Nil => matchAllQuery()
case queries => boolQuery().must(queries)
).flatten.compile

case class Sorting(f: String, order: String):
import com.sksamuel.elastic4s.requests.searches.sort.SortOrder
Expand Down
21 changes: 14 additions & 7 deletions modules/core/src/main/scala/package.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package lila
package lila.search

package object search:
import com.sksamuel.elastic4s.ElasticDsl.*
import com.sksamuel.elastic4s.requests.searches.queries.Query

object Date:
import org.joda.time.format.{ DateTimeFormat, DateTimeFormatter }
val format = "yyyy-MM-dd HH:mm:ss"
val formatter: DateTimeFormatter = DateTimeFormat.forPattern(format)
object Date:
import org.joda.time.format.{ DateTimeFormat, DateTimeFormatter }
val format = "yyyy-MM-dd HH:mm:ss"
val formatter: DateTimeFormatter = DateTimeFormat.forPattern(format)

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

extension (queries: List[Query])
def compile: Query = queries match
case Nil => matchAllQuery()
case q :: Nil => q
case _ => boolQuery().filter(queries)
19 changes: 8 additions & 11 deletions modules/core/src/main/scala/study.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,20 @@ object StudyQuery:

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

private def parsed(text: String) = QueryParser(text, List("owner", "member"))

private def makeQuery(query: Study) = {
val parsed = QueryParser(query.text, List("owner", "member"))
val matcher: Query =
if parsed(query.text).terms.isEmpty then matchAllQuery()
if parsed.terms.isEmpty then matchAllQuery()
else
multiMatchQuery(
parsed(query.text).terms.mkString(" ")
parsed.terms.mkString(" ")
).fields(searchableFields*).analyzer("english").matchType("most_fields")
must {
boolQuery().filter {
matcher :: List(
parsed(query.text)("owner").map { termQuery(Fields.owner, _) },
parsed(query.text)("member").map { member =>
boolQuery()
.must(termQuery(Fields.members, member))
.not(termQuery(Fields.owner, member))
}
parsed("owner").map(termQuery(Fields.owner, _)),
parsed("member").map(member =>
boolQuery().must(termQuery(Fields.members, member)).not(termQuery(Fields.owner, member))
)
).flatten
} should List(
Some(selectPublic),
Expand Down
11 changes: 3 additions & 8 deletions modules/core/src/main/scala/team.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,9 @@ object TeamQuery:
.start(from.value)
.size(size.value)

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

private def parsed(query: Team) = QueryParser(query.text, Nil)

private def makeQuery(team: Team) = must {
parsed(team).terms.map { term =>
multiMatchQuery(term).fields(searchableFields*)
}
}
private def makeQuery(team: Team) =
QueryParser(team.text, Nil).terms.map(term => multiMatchQuery(term).fields(searchableFields*)).compile

private val searchableFields = List(Fields.name, Fields.description)

0 comments on commit a2cc30a

Please sign in to comment.