forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Indexer.scala
76 lines (65 loc) · 2.16 KB
/
Indexer.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package lila.teamSearch
import akka.actor._
import akka.pattern.pipe
import com.sksamuel.elastic4s.ElasticClient
import com.sksamuel.elastic4s.ElasticDsl._
import com.sksamuel.elastic4s.IndexType
import com.sksamuel.elastic4s.mappings.FieldType._
import lila.search.actorApi._
import lila.team.actorApi._
import lila.team.Team
private[teamSearch] final class Indexer(
client: ElasticClient,
indexName: String,
typeName: String) extends Actor {
private val indexType = s"$indexName/$typeName"
def readIndexType = IndexType(indexName, typeName)
def receive = {
case Search(definition) => client execute definition(readIndexType) pipeTo sender
case Count(definition) => client execute definition(readIndexType) pipeTo sender
case InsertTeam(team) => client execute store(team)
case RemoveTeam(id) => client execute {
delete id id from indexType
}
case Reset =>
lila.search.ElasticSearch.createType(client, indexName, typeName)
try {
client execute {
put mapping indexName / typeName as Seq(
Fields.name typed StringType boost 3,
Fields.description typed StringType boost 2,
Fields.location typed StringType,
Fields.nbMembers typed ShortType
)
}
import scala.concurrent.Await
import scala.concurrent.duration._
import play.api.libs.json.Json
import lila.db.api._
import lila.team.tube.teamTube
Await.result(
$enumerate.bulk[Option[Team]]($query[Team](Json.obj("enabled" -> true)), 100) { teamOptions =>
client execute {
bulk {
(teamOptions.flatten map store): _*
}
} void
}, 20 minutes)
sender ! (())
}
catch {
case e: Exception =>
println(e)
sender ! Status.Failure(e)
}
}
private def store(team: Team) =
index into indexType fields {
List(
Fields.name -> team.name,
Fields.description -> team.description.take(10000),
Fields.location -> team.location,
Fields.nbMembers -> team.nbMembers
): _*
} id team.id
}