【问题标题】:Elastic search 5.4 Update \ add nested documentElastic search 5.4 更新\添加嵌套文档
【发布时间】:2018-08-02 03:46:57
【问题描述】:
{
        "id":100,
        "name":"xxx",
        "others":{
            "hobbies":["cricket","footbal"]
        }}
  1. 如何在爱好中增加新的价值(“爱好”:[“板球”,“足球”,“读书”])
  2. 如何在其他人中添加新字段(例如:位置)
  3. 如何从爱好中去除“板球”。

【问题讨论】:

    标签: hadoop elasticsearch elasticsearch-plugin


    【解决方案1】:

    您可以使用scripted update(documentation link) 来满足您的要求。

    1. 在爱好中增加价值的查询

      {
          "script" : {
              "source": "ctx._source.others.hobbies.add(params.hobby)",
              "lang": "painless",
              "params" : {
              "hobby": "reading books"
           }
         }
      }
      


    2. 在其他人中添加新字段

      {
          "script": {
              "source": "ctx._source.others.location = 'value_of_new_field'",
              "lang": "painless"
          }
      }
      


    3. 从爱好中删除板球

      {
          "script" : {
              "source": "int index = ctx._source.others.hobbies.indexOf(params.remove_string); if(index != -1){ctx._source.others.hobbies.remove(index);}",
              "lang": "painless",
              "params" : {
                  "remove_string" : "cricket"
              }
          }
      }
      



    UPDATE-1 根据下面 cmets 中的要求,是添加数组字段的方法。

    添加一个数组字段

        {
            "script": {
                "source": "ctx._source.extra.location = []",
                "lang": "painless"
            }
        }
    

    【讨论】:

    • 谢谢维尼特。在 Example #2 .. 中必须像这样创建 "Location":[] - 以数组的形式。其他 #1 ,#3 工作正常..@vinit payal
    • 非常感谢.. 发布另一个问题。 stackoverflow.com/questions/48926849/…
    • @LearnHadoop 你不喜欢这个答案吗?如果它解决了您的查询,那么它的支持在哪里?老兄,它至少可以帮助其他人也找出正确的答案?
    • 是的.. 它帮助别人。现在我做到了
    猜你喜欢
    • 2018-08-01
    • 1970-01-01
    • 2018-02-01
    • 1970-01-01
    • 2018-04-07
    • 1970-01-01
    • 2017-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多