gpt4 book ai didi

scala - 怎么把Elastic4s的GetResponse转换成特定的案例类?

转载 作者:行者123 更新时间:2023-12-03 02:14:55 25 4
gpt4 key购买 nike

我已经使用elastic4s设置了用于elasticsearch的客户端,该客户端存储了Person case类:

case class Person(id: String, name: String)
如何通过ID获取特定人员并将GetResponse转换为该案例类?
我想做的是:
client.execute{
get(id).from(index, `type`).
}
返回Future [Response [GetResponse]]

最佳答案

我已经有一段时间没有接触过elastic4s了,但据我所知,您需要提供一个特定的HitReader进行响应

implicit object PersonHitReader extends PersonReader[Character] {
override def read(hit: Hit): Either[Throwable, Person] = {
val source = hit.sourceAsMap
Right(Person(source("id").toString, source("name").toString))
}
}

https://github.com/sksamuel/elastic4s#hitreader-typeclass

关于scala - 怎么把Elastic4s的GetResponse转换成特定的案例类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63790526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com