Skip to content

Commit

Permalink
Remove play json from core
Browse files Browse the repository at this point in the history
  • Loading branch information
lenguyenthanh committed May 7, 2024
1 parent 868e768 commit a5119cd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
1 change: 0 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ lazy val core = project
libraryDependencies ++= Seq(
"com.github.ornicar" %% "scalalib" % "7.1.0",
"com.sksamuel.elastic4s" %% "elastic4s-client-esjava" % "7.17.4",
"com.typesafe.play" %% "play-json" % "2.9.4",
"joda-time" % "joda-time" % "2.12.7"
)
)
Expand Down
15 changes: 8 additions & 7 deletions modules/core/src/main/scala/ESClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package lila.search
import com.sksamuel.elastic4s.ElasticDsl.{ RichFuture => _, _ }
import com.sksamuel.elastic4s.fields.ElasticField
import com.sksamuel.elastic4s.{ ElasticClient, ElasticDsl, Index => ESIndex, Response }
import play.api.libs.json._
import scala.concurrent.{ ExecutionContext, Future }

case class JsonObject(json: String) extends AnyVal

case class Index(name: String) extends AnyVal {
def toES: ESIndex = ESIndex(name)
}
Expand All @@ -25,18 +26,18 @@ final class ESClient(client: ElasticClient)(implicit ec: ExecutionContext) {
q.countDef(query)(index)
} flatMap toResult map CountResponse.apply

def store(index: Index, id: Id, obj: JsObject) =
def store(index: Index, id: Id, obj: JsonObject) =
client execute {
indexInto(index.name) source Json.stringify(obj) id id.value
indexInto(index.name) source obj.json id id.value
}

def storeBulk(index: Index, objs: JsObject) =
if (objs.fields.isEmpty) funit
def storeBulk(index: Index, objs: List[(String, JsonObject)]) =
if (objs.isEmpty) funit
else
client execute {
ElasticDsl.bulk {
objs.fields.collect { case (id, JsString(doc)) =>
indexInto(index.name) source doc id id
objs.map { case (id, obj) =>
indexInto(index.name) source obj.json id id
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions play/app/src/main/scala/controllers/WebApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class WebApi @Inject() (cc: ControllerComponents, client: ESClient)(implicit ec:

def store(index: String, id: String) =
JsObjectBody { obj =>
client.store(Index(index), Id(id), obj) inject Ok(s"inserted $index/$id")
client.store(Index(index), Id(id), JsonObject(Json.stringify(obj))) inject Ok(s"inserted $index/$id")
}

def deleteById(index: String, id: String) =
Expand Down Expand Up @@ -65,8 +65,11 @@ class WebApi @Inject() (cc: ControllerComponents, client: ESClient)(implicit ec:

def storeBulk(index: String) =
JsObjectBody { objs =>
val jsonObjs = objs.fields.collect { case (id, obj) =>
(id, JsonObject(Json.stringify(obj)))
}.toList
Chronometer(s"bulk ${objs.fields.size} $index") {
client.storeBulk(Index(index), objs) map { _ =>
client.storeBulk(Index(index), jsonObjs) map { _ =>
Ok("thx")
}
}
Expand Down

0 comments on commit a5119cd

Please sign in to comment.