gpt4 book ai didi

scala - 从Scala中的 Elasticsearch 检索数据/值

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

我是scala和elasticsearch的新手。我在Elasticsearch中有以下数据,索引名称=“abc”,文档类型=“_doc”。

{
"took": 45,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "abc",
"_type": "_doc",
"_id": "1234",
"_score": 1,
"_source": {
"Code": "PDF",
"Description": "Portion Occured",
"Id": "1234",
"Operator": {
"Id": "1",
"PopulationLevel": "Key"
},
"Usage": [
{
"Allow": true
}
]
}
}
]
}
}
我的 Elasticsearch 客户端中有以下代码。
trait ElasticsearchClient {

lazy val esRestConnection: RestClient = createEsRestClient(elasticSearchsUri, elasticSearchProtocol)
lazy val esHttpConnection: HttpClient = HttpClient.fromRestClient(esRestConnection)


* Method to create instance of HTTP Client to access the given Elasticsearch URI.

def createEsRestClient(uri: String, protocol: String): RestClient = {
val hosts = ElasticsearchClientUri(uri).hosts.map {
case (host, port) =>
new HttpHost(host, port, protocol)
}
RestClient.builder(hosts: _*)
.build()
}
}
我正在使用sksamuel elastic4s-Elasticsearch Scala客户端
我需要编写一个代码,通过该代码我可以检索“Id”的值,即该特定索引的1234。我还有一些索引,我需要从中检索 Elasticsearch 的“Id”值。我需要从代码中传递索引名称。我怎样才能做到这一点

最佳答案

case class DocumentData(
id:Option[Int]
)
object DocumentData{
隐式val格式:Format [DocumentData] = new Format [DocumentData] {
override def reads(json: JsValue): JsResult[DocumentData] = ???

override def writes(o: DocumentData): JsValue = {
Json.obj(
"query" -> Json.obj(
"bool" -> Json.obj(
"must" -> Json.arr(
Seq(o.id.map(queryString =>
Json.obj("match" -> Json.obj(
"id"->queryString
))),
)
.flatten[JsObject])))
)

}
}
}

关于scala - 从Scala中的 Elasticsearch 检索数据/值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63903700/

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