【问题标题】:Retrieving data/value from elastic search in scala从scala中的弹性搜索中检索数据/值
【发布时间】:2020-09-16 11:37:19
【问题描述】:

我是 scala 和 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
            }
          ]
        }
      }
    ]
  }
}

我的弹性搜索客户端中有以下代码。

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。我还有一些索引,我需要从弹性搜索中检索“Id”的值。我需要从我的代码中传递索引名称。我该怎么做呢

【问题讨论】:

    标签: scala elasticsearch


    【解决方案1】:
    case class DocumentData(
                           id:Option[Int]
                           )
    object DocumentData{
    

    隐式验证格式: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])))
      )
    
    }
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-06-17
      • 2018-08-15
      • 2014-10-23
      • 2019-01-08
      • 1970-01-01
      • 2021-12-16
      • 2013-03-03
      • 1970-01-01
      • 2018-07-21
      相关资源
      最近更新 更多