【问题标题】:access array element in elasticsearch using elastic4s使用elastic4s访问elasticsearch中的数组元素
【发布时间】:2018-04-20 09:26:33
【问题描述】:

我想从数组中删除一个元素,如下所示,我在使用 elastic4s 的 elasticsearch 中插入了一个类型为 test 的 testindiex 和一个包含数组的文档。

我想从成员中删除一个值 ex: tony - 我该怎么做

   val insert = client.execute {
  indexInto("testindex"/"test") id 1 fields(
      "members" -> Array(
      "tony",
      "salvidor",
      "bobby"
    ),
    "crews" -> Seq(
      "gualtieri",
      "baccalieri",
      "barese",
      "moltisanti"
    )
  )
}.await

我做了什么:

 val deleted=client.execute {
    deleteIn("testindex"/"test").by(termQuery("crews","gualtieri"))
  }.await

但这会删除整个文档而不是单个记录,请帮助我。

【问题讨论】:

    标签: elasticsearch elasticsearch-plugin elasticsearch-5 elastic4s


    【解决方案1】:

    从 Elasticsearch 的嵌套/数组 JSON 中删除/更新元素

    输入:JSON 文档

    {
      "members" -> Array(
        "tony",
        "salvidor",
        "bobby"
      ),
      "crews" ->
        "gualtieri"
    
    )
    

    以下用于从数组中添加和删除元素的命令

    add 命令:向 JSON 文档中的数组添加元素

     val resp = client.execute { updateIn(indexname,doctype).query("crews", "gualtieri")  script {
      script("ctx._source.members.add(params.key)").params(Map("key"->"space"))
    } }.await
    

    remove 命令:从 JSON 文档中的数组中删除元素

    val resp1 = client.execute { updateIn(indexname,doctype).query("crews", "gualtieri")  script {
      script("ctx._source.members.remove(ctx._source.members.indexOf('salvidor'))")
        //.params(Map("key"->"abc"))
    } }.await
    

    removeAll 命令:从数组中删除所有相同的元素。

    val resp2 = client.execute { updateIn("testindex","test").query("crews", "gualtieri")  script {
      script("ctx._source.members.removeAll(Collections.singleton('space'))")
    } }.await
    

    【讨论】:

    • 如何使用具有设置和映射细节的 json 中的 elastic4s 创建模板。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-28
    相关资源
    最近更新 更多