簡體   English   中英

在 elasticsearch 中更新嵌套對象

[英]Updating a nested object in elasticsearch

我有以下索引映射

{
  "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

注意我最好為此使用部分文檔更新,因為嵌套對象有點復雜,並且無法編寫內聯腳本

Elasticsearch 版本 - 5.6

更新

做這樣的事情

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

但這實質上意味着每次我更新時,我都會重寫整個pairs字段(即使我只更新數組中的一個對象)-這對我來說看起來並不理想還是可以?

有一個頁面包含有關嵌套類型管理的快速教程: 如何管理 Elasticsearch 文檔中的嵌套對象 它解釋了如何添加、刪除和編輯嵌套對象。

沒有用於更新特定嵌套文檔的語法。 您只能指定一個嵌套字段值,它會更新所有嵌套文檔。 ES無論如何都會通過部分更新重新索引文檔。

您可以使用父子文檔以一定的代價實現這一點。 如果您有這么多嵌套記錄,那么它可能更適合您的情況。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM