Skip to content

Commit

Permalink
Use count query for count
Browse files Browse the repository at this point in the history
  • Loading branch information
lenguyenthanh committed Jun 13, 2024
1 parent 10d41a0 commit 2e73ec9
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 16 deletions.
2 changes: 1 addition & 1 deletion modules/api/src/main/smithy/search.smithy
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ operation Count {

output := {
@required
count: Integer
count: Long
}

errors: [InternalServerError]
Expand Down
14 changes: 7 additions & 7 deletions modules/app/src/test/scala/IntegrationSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ object IntegrationSuite extends IOSuite:
y <- service.search(Query.team("team description"), 0, 12)
yield expect(x.hitIds.size == 1 && x == y)

test("study"): _ =>
test("study".only): _ =>
Clients
.search(uri)
.use: service =>
Expand All @@ -102,19 +102,19 @@ object IntegrationSuite extends IOSuite:
name = "study name",
owner = "study owner",
members = List("member1", "member2"),
chapterNames = "chapter names",
chapterTexts = "chapter texts",
chapterNames = "names",
chapterTexts = "texts",
likes = 100,
public = true,
topics = List("topic1", "topic2")
)
)
)
_ <- service.refresh(Index.Study)
x <- service.search(Query.study("study name"), 0, 12)
y <- service.search(Query.study("study description"), 0, 12)
z <- service.search(Query.study("topic1"), 0, 12)
yield expect(x.hitIds.size == 1 && x == y && z == x)
a <- service.search(Query.study("name"), 0, 12)
b <- service.search(Query.study("study description"), 0, 12)
c <- service.search(Query.study("topic1"), 0, 12)
yield expect(a.hitIds.size == 1 && b == a && c == a)

test("game"): _ =>
Clients
Expand Down
3 changes: 2 additions & 1 deletion modules/core/src/main/scala/Queryable.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package lila.search

import com.sksamuel.elastic4s.requests.count.CountRequest
import com.sksamuel.elastic4s.requests.searches.SearchRequest

trait Queryable[A]:

def searchDef(query: A)(from: From, size: Size): SearchRequest

def countDef(query: A): SearchRequest
def countDef(query: A): CountRequest

case class ParsedQuery(terms: List[String], filters: Map[String, String]):

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 @@ -38,7 +38,7 @@ object ForumQuery:
.start(from.value)
.size(size.value)

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

private def makeQuery(query: Forum) =
val parsed = QueryParser(query.text, List("user"))
Expand Down
2 changes: 1 addition & 1 deletion modules/core/src/main/scala/game.scala
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ object GameQuery:
.size(size.value)
.timeout(timeout)

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

private def makeQuery(query: Game): Query =

Expand Down
6 changes: 3 additions & 3 deletions modules/core/src/main/scala/model.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ object SearchResponse:
def apply(res: ESR): SearchResponse =
SearchResponse(res.hits.hits.toList.map(_.id))

case class CountResponse(count: Int)
case class CountResponse(count: Long)

object CountResponse:

def apply(res: ESR): CountResponse =
CountResponse(res.totalHits.toInt)
def apply(res: com.sksamuel.elastic4s.requests.count.CountResponse): CountResponse =
CountResponse(res.count)
2 changes: 1 addition & 1 deletion modules/core/src/main/scala/study.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ object StudyQuery:
.start(from.value)
.size(size.value)

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

private def makeQuery(query: Study) = {
val parsed = QueryParser(query.text, List("owner", "member"))
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
Expand Up @@ -33,7 +33,7 @@ object TeamQuery:
.start(from.value)
.size(size.value)

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

private def makeQuery(team: Team) =
QueryParser(team.text, Nil).terms.map(term => multiMatchQuery(term).fields(searchableFields*)).compile
Expand Down

0 comments on commit 2e73ec9

Please sign in to comment.