lila/modules/search/src/main/ESClient.scala

92 lines
2.8 KiB
Scala
Raw Normal View History

package lila.search
2015-09-01 06:18:24 -06:00
import play.api.libs.json._
2019-11-28 18:34:46 -07:00
import play.api.libs.ws._
2020-08-07 08:22:26 -06:00
import play.api.libs.ws.JsonBodyWritables._
import scala.annotation.nowarn
2015-09-01 06:18:24 -06:00
sealed trait ESClient {
2015-09-01 07:52:20 -06:00
def search[Q: Writes](query: Q, from: From, size: Size): Fu[SearchResponse]
2015-09-01 06:18:24 -06:00
2015-09-01 07:52:20 -06:00
def count[Q: Writes](query: Q): Fu[CountResponse]
2015-09-01 06:18:24 -06:00
def store(id: Id, doc: JsObject): Funit
def deleteById(id: Id): Funit
2016-01-06 21:21:50 -07:00
def deleteByIds(ids: List[Id]): Funit
def refresh: Funit
}
2015-09-03 06:09:38 -06:00
final class ESClientHttp(
2020-08-07 08:22:26 -06:00
ws: StandaloneWSClient,
2019-11-28 18:34:46 -07:00
config: SearchConfig,
val index: Index
)(implicit ec: scala.concurrent.ExecutionContext)
extends ESClient {
2015-09-01 06:18:24 -06:00
2020-05-05 22:11:15 -06:00
def store(id: Id, doc: JsObject) =
config.writeable ?? monitor("store") {
HTTP(s"store/${index.name}/${id.value}", doc)
}
2015-09-01 06:18:24 -06:00
2020-05-05 22:11:15 -06:00
def search[Q: Writes](query: Q, from: From, size: Size) =
monitor("search") {
HTTP(s"search/${index.name}/${from.value}/${size.value}", query, SearchResponse.apply)
2021-07-21 03:39:02 -06:00
.dmap(~_)
2020-05-05 22:11:15 -06:00
}
2015-09-01 06:18:24 -06:00
2020-05-05 22:11:15 -06:00
def count[Q: Writes](query: Q) =
monitor("count") {
HTTP(s"count/${index.name}", query, CountResponse.apply)
2021-07-21 03:39:02 -06:00
.dmap(~_)
2020-05-05 22:11:15 -06:00
}
2015-09-01 06:18:24 -06:00
2019-12-13 07:30:20 -07:00
def deleteById(id: lila.search.Id) =
config.writeable ??
HTTP(s"delete/id/${index.name}/${id.value}", Json.obj())
2015-09-01 06:18:24 -06:00
2019-12-13 07:30:20 -07:00
def deleteByIds(ids: List[lila.search.Id]) =
config.writeable ??
HTTP(s"delete/ids/${index.name}", Json.obj("ids" -> ids.map(_.value)))
2015-09-01 06:18:24 -06:00
2016-01-06 23:14:46 -07:00
def putMapping =
2020-06-25 06:47:23 -06:00
HTTP(s"mapping/${index.name}", Json.obj())
2016-01-06 23:14:46 -07:00
def storeBulk(docs: Seq[(Id, JsObject)]) =
2020-05-05 22:11:15 -06:00
HTTP(
2020-06-25 06:47:23 -06:00
s"store/bulk/${index.name}",
2020-09-21 01:28:28 -06:00
JsObject(docs map { case (Id(id), doc) =>
id -> JsString(Json.stringify(doc))
2020-05-05 22:11:15 -06:00
})
)
2015-09-01 06:18:24 -06:00
def refresh =
HTTP(s"refresh/${index.name}", Json.obj())
2021-07-21 03:39:02 -06:00
private[search] def HTTP[D: Writes, R](url: String, data: D, read: String => R): Fu[Option[R]] =
2019-11-28 18:34:46 -07:00
ws.url(s"${config.endpoint}/$url").post(Json toJson data) flatMap {
2021-07-21 03:39:02 -06:00
case res if res.status == 200 => fuccess(read(res.body).some)
case res if res.status == 400 => fuccess(none)
2019-12-13 07:30:20 -07:00
case res => fufail(s"$url ${res.status}")
2015-09-01 06:18:24 -06:00
}
2021-07-21 03:39:02 -06:00
private[search] def HTTP(url: String, data: JsObject): Funit = HTTP(url, data, _ => ()).void
2015-09-01 06:18:24 -06:00
2016-03-12 02:25:42 -07:00
private def monitor[A](op: String)(f: Fu[A]) =
2019-12-10 14:01:18 -07:00
f.monTry(res => _.search.time(op, index.name, res.isSuccess))
2015-09-01 06:18:24 -06:00
}
2015-09-01 06:18:24 -06:00
final class ESClientStub extends ESClient {
2020-05-04 23:41:08 -06:00
def search[Q: Writes](query: Q, from: From, size: Size) = fuccess(SearchResponse(Nil))
def count[Q: Writes](query: Q) = fuccess(CountResponse(0))
def store(id: Id, doc: JsObject) = funit
@nowarn("msg=parameter value")
def storeBulk(docs: Seq[(Id, JsObject)]) = funit
def deleteById(id: Id) = funit
def deleteByIds(ids: List[Id]) = funit
def putMapping = funit
def refresh = funit
}