如何在Elasticsearch中存储嵌套字段

Shivangi Gupta

我正在使用Java高级REST客户端。这是其文档链接

我已经创建了一个客户。

trait HighLevelRestClient {

  def elasticSearchClient(): RestHighLevelClient = {
   new RestHighLevelClient(
      RestClient.builder(
        new HttpHost("localhost", ElasticSearchPort, "http")))
  }
}

索引数据时,嵌套字段将存储为String。以下代码说明了如何创建索引:

val indexRequest = new IndexRequest("my-index", "test-type").source(
  "person", person,
  "date", DateTime.now()
)

其中,人员是案例类,表示为:

Person(personId: String, name: String, address: Address) 

而Address本身是一个case类,表示为:

Address(city: String, zip: Int)

我的应用程序要求将人员存储为键值对,以便其字段可搜索。但是,当我使用上面的代码时,它被存储为String。

{
"person" : "Person(my-id, my-name, Address(my-city, zip-value))",
"date" :  "2017-12-12"
} 

所需的结构是:

{
"person" : {
    "personId" : "my-id",
    "name" : "person-name",
    "address": {
      "city" : "city-name",
      "zip" : 12345
          }
     },
"date" : "2017-12-12"
}

我希望我已经很好地解决了这个问题。任何帮助,将不胜感激。谢谢!

尼古拉·瓦西里耶夫(Nikolay Vasiliev)

你快到了。为了实现您的目标,您需要:

  1. 将对象序列化为JSON
  2. 指定请求的内容类型

实际上,它在Index API的页面中进行了描述

将案例类序列化为JSON的便捷库是json4s(您可以在此处看到一些序列化的示例)。

您的代码可能如下所示:

import org.apache.http.HttpHost
import org.elasticsearch.action.index.IndexRequest
import org.elasticsearch.client.{RestClient, RestHighLevelClient}
import org.elasticsearch.common.xcontent.XContentType
import org.joda.time.DateTime
import org.json4s.NoTypeHints
import org.json4s.jackson.Serialization
import org.json4s.jackson.Serialization.write

case class Address(city: String, zip: Int)

case class Person(personId: String, name: String, address: Address)

case class Doc(person: Person, date: String)

object HighClient {
  def main(args: Array[String]): Unit = {
    val client = new RestHighLevelClient(
      RestClient.builder(
        new HttpHost("localhost", 9206, "http")))

    implicit val formats = Serialization.formats(NoTypeHints)

    val doc = Doc(
      Person("blah1", "Peter Parker", Address("New-York", 33755)),
      DateTime.now().toString
    )

    val indexRequest = new IndexRequest("my-index", "test-type").source(
      write(doc), XContentType.JSON
    )

    client.index(indexRequest)

    client.close()
  }
}

请注意在这种情况下:

new IndexRequest("my-index", "test-type").source(
  write(doc), XContentType.JSON
)

此功能将用于: public IndexRequest source(String source, XContentType xContentType)

在您的情况下:

new IndexRequest("my-index", "test-type").source(
  "person", person,
  "date", DateTime.now()
)

它会打电话public IndexRequest source(Object... source)

希望有帮助!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Elasticsearch中突出显示嵌套字段

Elasticsearch:如何在嵌套字段中获得完全匹配

如何在 elasticsearch 中获取嵌套字段的不同值?

如何在elasticsearch中同时在嵌套字段中搜索单个对象的两个字段

如何在Elasticsearch中同时按父字段和嵌套字段排序?

如何在Elasticsearch中将父字段值与嵌套对象中的聚合值进行比较?

如何在ElasticSearch中查询可以是整数值的关键字嵌套字段?

如何在Elasticsearch中基于嵌套字段的最小值对结果集进行排序?

如何在Elasticsearch中基于存储的字段值获取前5个文档?

如何在 elasticsearch 中搜索嵌套属性

如何在Redis中存储嵌套的Hashmap?

如何在JsonObject中存储嵌套的ArrayList

如何在Firestore中查询嵌套字段?

如何在elasticsearch中获取空字段?

如何在Vespa中存储日期字段?

如何使用python在嵌套字典中存储字段值?

如何在Elasticsearch中按日期字段汇总不同的字段

如何在Elasticsearch中的嵌套文档中搜索精确文本

如何在Golang中的struct中访问嵌套字段

如何在ElasticSearch中仅从源中获取内部字段?

如何创建嵌套对象并将其添加到Elasticsearch的嵌套字段中?

如何在 ElasticSearch 中聚合嵌套文档的大小?

如何在elasticsearch中更新数组内的嵌套文档

如何在 ElasticSearch 中仅搜索和返回嵌套文档?

如何在ElasticSearch中聚合过滤的嵌套文档?

如何在Elasticsearch文档中获取嵌套对象的不同键?

如何在Elasticsearch中查询嵌套结构

如何在Java中的字段中存储变量类型的对象?

如何在GitHub中创建嵌套存储库?