diff --git a/build.sbt b/build.sbt index 743eafcf..d9ad277f 100644 --- a/build.sbt +++ b/build.sbt @@ -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" ) ) diff --git a/modules/core/src/main/scala/ESClient.scala b/modules/core/src/main/scala/ESClient.scala index 0d28adc9..3219b852 100644 --- a/modules/core/src/main/scala/ESClient.scala +++ b/modules/core/src/main/scala/ESClient.scala @@ -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) } @@ -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 } } } diff --git a/play/app/src/main/scala/controllers/WebApi.scala b/play/app/src/main/scala/controllers/WebApi.scala index f5c4c8b4..0fa99d82 100644 --- a/play/app/src/main/scala/controllers/WebApi.scala +++ b/play/app/src/main/scala/controllers/WebApi.scala @@ -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) = @@ -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") } }