forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTeamSearchApi.scala
51 lines (41 loc) · 1.29 KB
/
TeamSearchApi.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package lila.teamSearch
import akka.stream.scaladsl._
import play.api.libs.json._
import lila.search._
import lila.team.{ Team, TeamRepo }
final class TeamSearchApi(
client: ESClient,
teamRepo: TeamRepo
)(implicit
ec: scala.concurrent.ExecutionContext,
mat: akka.stream.Materializer
) extends SearchReadApi[Team, Query] {
def search(query: Query, from: From, size: Size) =
client.search(query, from, size) flatMap { res =>
teamRepo byOrderedIds res.ids
}
def count(query: Query) = client.count(query) dmap (_.count)
def store(team: Team) = client.store(Id(team.id), toDoc(team))
private def toDoc(team: Team) =
Json.obj(
Fields.name -> team.name,
Fields.description -> team.description.take(10000),
Fields.nbMembers -> team.nbMembers
)
def reset =
client match {
case c: ESClientHttp =>
c.putMapping >> {
logger.info(s"Index to ${c.index.name}")
teamRepo.cursor
.documentSource()
.via(lila.common.LilaStream.logRate[Team]("team index")(logger))
.map(t => Id(t.id) -> toDoc(t))
.grouped(200)
.mapAsync(1)(c.storeBulk)
.toMat(Sink.ignore)(Keep.right)
.run()
} >> client.refresh
case _ => funit
}
}