Skip to content

Commit

Permalink
upgrade to new search service
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Jan 7, 2016
1 parent 159b3ed commit 3f71bd4
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 19 deletions.
13 changes: 5 additions & 8 deletions modules/forum/src/main/PostRepo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,9 @@ sealed abstract class PostRepo(troll: Boolean) {

def sortQuery = $sort.createdAsc

def userIdsByTopicId(topicId: String): Fu[List[String]] = {
val col = postTube.coll
import reactivemongo.api.collections.bson.BSONBatchCommands.AggregationFramework, AggregationFramework.{ Match, GroupField }

col.aggregate(Match(BSONDocument("topicId" -> topicId)),
List(GroupField("userId")())).map(
_.documents.map(_.getAs[String]("_id")).flatten)
}
def userIdsByTopicId(topicId: String): Fu[List[String]] =
postTube.coll.distinct("userId", BSONDocument("topicId" -> topicId).some) map lila.db.BSON.asStrings

def idsByTopicId(topicId: String): Fu[List[String]] =
postTube.coll.distinct("_id", BSONDocument("topicId" -> topicId).some) map lila.db.BSON.asStrings
}
10 changes: 6 additions & 4 deletions modules/forum/src/main/TopicApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,12 @@ private[forum] final class TopicApi(
maxPerPage = maxPerPage)

def delete(categ: Categ, topic: Topic): Funit =
(PostRepo removeByTopic topic.id zip $remove(topic)) >>
(env.categApi denormalize categ) >>-
(indexer ! RemoveTopic(topic.id)) >>
env.recent.invalidate
PostRepo.idsByTopicId(topic.id) flatMap { postIds =>
(PostRepo removeByTopic topic.id zip $remove(topic)) >>
(env.categApi denormalize categ) >>-
(indexer ! RemovePosts(postIds)) >>
env.recent.invalidate
}

def toggleClose(categ: Categ, topic: Topic, mod: User): Funit =
TopicRepo.close(topic.id, topic.open) >> {
Expand Down
2 changes: 1 addition & 1 deletion modules/forum/src/main/actorApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package actorApi

case class InsertPost(post: Post)
case class RemovePost(id: String)
case class RemoveTopic(id: String)
case class RemovePosts(ids: List[String])
2 changes: 1 addition & 1 deletion modules/forumSearch/src/main/Env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ final class Env(
def receive = {
case InsertPost(post) => api store post
case RemovePost(id) => client deleteById Id(id)
case RemoveTopic(id) => client deleteByQuery StringQuery(s"${Fields.topicId}:$id")
case RemovePosts(ids) => client deleteByIds ids.map(Id.apply)
}
}), name = ActorName)
}
Expand Down
2 changes: 1 addition & 1 deletion modules/gameSearch/src/main/GameSearchApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ final class GameSearchApi(client: ESClient) extends SearchReadApi[Game, Query] {
Fields.winnerColor -> game.winner.fold(3)(_.color.fold(1, 2)),
Fields.averageRating -> game.averageUsersRating,
Fields.ai -> game.aiLevel,
Fields.date -> (lila.search.Date.formatter print game.createdAt),
Fields.date -> (lila.search.Date.formatter print game.updatedAtOrCreatedAt),
Fields.duration -> game.estimateTotalTime,
Fields.opening -> (game.opening map (_.code.toLowerCase)),
Fields.analysed -> analysed,
Expand Down
8 changes: 4 additions & 4 deletions modules/search/src/main/ESClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ sealed trait ESClient {

def deleteById(id: Id): Funit

def deleteByQuery(query: StringQuery): Funit
def deleteByIds(ids: List[Id]): Funit
}

final class ESClientHttp(
Expand All @@ -34,8 +34,8 @@ final class ESClientHttp(
def deleteById(id: lila.search.Id) = writeable ??
HTTP(s"delete/id/${index.name}/${id.value}", Json.obj())

def deleteByQuery(query: lila.search.StringQuery) = writeable ??
HTTP(s"delete/query/${index.name}/${query.value}", Json.obj())
def deleteByIds(ids: List[lila.search.Id]) = writeable ??
HTTP(s"delete/ids/${index.name}", Json.obj("ids" -> ids.map(_.value)))

def createTempIndex = {
val tempIndex = Index(s"${index.name}_${ornicar.scalalib.Random.nextString(4)}")
Expand Down Expand Up @@ -80,6 +80,6 @@ final class ESClientStub extends ESClient {
def store(id: Id, doc: JsObject) = funit
def storeBulk(docs: Seq[(Id, JsObject)]) = funit
def deleteById(id: Id) = funit
def deleteByQuery(query: StringQuery) = funit
def deleteByIds(ids: List[Id]) = funit
def putMapping = funit
}

0 comments on commit 3f71bd4

Please sign in to comment.