首页 > 解决方案 > 在弹性搜索中更新嵌套对象

问题描述

我有以下索引映射

{
  "mappings": {
    "xxxxx": {
      "properties": {
        "ID": {
          "type": "text"
        },
        "pairs": {
          "type": "nested"
        },
        "xxxxx": {
          "type": "text"
        }
      }
    }
  }
}

pairs字段是具有以下结构的对象数组

{
    "id": "",
    "Answer": "",
    "Question": []
}

我想要做的是检索一个特定的嵌套对象并更新它。我已经尝试updateByQuery使用部分文档/脚本的方法,但我无法更新

脚本

var theScript = {
    "inline": "ctx._source.Answer = 'Elastic search update Test'"
}

client.updateByQuery({
    index: 'sample',
    _source: false,
    body: {
        query: {
            bool: {
                must: [
                    {
                        "match": {
                            "ID": '2rXdCf5OM9g1ebPNFdZNqW'
                        }
                    },
                    {
                        "nested": {
                            "path": "pairs",
                            "query": {
                                "match": {
                                    "pairs.id": "c1vNGnnQLuk"
                                }
                            },
                            "inner_hits": {}
                        }
                    }
                ]
            }
        },
        "script": theScript
    }
},function(err, body){
    if(err){
        throw err;
    }
    console.log('body: ', body)
    console.log('data: ', body.hits.hits)
})

部分文档

client.updateByQuery({
    index: 'sample',
    _source: false,
    body: {
        query: {
            bool: {
                must: [
                    {
                        "match": {
                            "ID": '2rXdCf5OM9g1ebPNFdZNqW'
                        }
                    },
                    {
                        "nested": {
                            "path": "pairs",
                            "query": {
                                "match": {
                                    "pairs.id": "c1vNGnnQLuk"
                                }
                            },
                            "inner_hits": {}
                        }
                    }
                ]
            }
        },
       "doc": {
        "Answer": 'Elastic search update Test'
       }
    }
},function(err, body){
    if(err){
        throw err;
    }
    console.log('body: ', body)
    console.log('data: ', body.hits.hits)
})

我收到以下错误:

部分更新

Error: [parsing_exception] Unknown key for a START_OBJECT in [doc]., with { line=1 & col=191 }

脚本

Error: [illegal_argument_exception] [sample][conversation][AWa-p9zBTJHq-_gvo-Af] didn't store _source

注意理想情况下,我希望为此使用部分文档更新,因为嵌套对象有点复杂,并且不可能编写内联脚本

弹性搜索版本 - 5.6

更新

做这样的事情

{
    "script" : {
        "inline": "ctx._source.pairs= params.pairs",
        "lang": "painless",
        "params" : {
            "pairs" : [{...}, {...}, ...]
        }
    }
}

但这本质上意味着每次我更新时,我都在重写整个pairs字段(即使我只更新数组中的一个对象)——这对我来说看起来并不理想还是很好?

标签: elasticsearch

解决方案


有一个页面包含有关嵌套类型管理的快速教程:如何管理 Elasticsearch 文档中的嵌套对象。它解释了如何添加、删除和编辑嵌套对象。


推荐阅读