简体   繁体   English

如何更新 Elasticsearch 文档中的嵌套对象?

[英]How to Update nested objects in Elasticsearch documents?

I am trying to find the best practice to update a document with nested objects: I am using the ES as a view cache:我正在尝试找到使用嵌套对象更新文档的最佳实践:我正在使用 ES 作为视图缓存:

let say I have tow entities in my SQL DB: the First one is Address and the Homes and the relation between them is one too many假设我的 SQL 数据库中有两个实体:第一个是地址房屋,它们之间的关系太多了

Address Table has columns=> Id, Street, ZipCode

Home Table has colmns=> Id, AddressId(FK), price

And I store them in ES aggregated:我将它们存储在聚合的 ES 中:

Home : {
Id: 1,
price : 3000,
Address :{
   id: 1,
   street: "blabla",
   zibcode : 12345

  }
}

if I made any change on Address I want to update all Home Documents that contains the same Address in the Home index with the new value.如果我对地址进行了任何更改,我想用新值更新主页索引中包含相同地址的所有主页文档 What I need to know how can I update all Documents in the same index in one request to ES.我需要知道如何在对 ES 的一次请求中更新同一索引中的所有文档。

_update_by_query is probably what you're after. _update_by_query可能是您所追求的。

This ain't pretty but it should do the job:这不是很漂亮,但它应该可以完成工作:

PUT test/_doc/1
{
  "Id": 1,
  "price": 3000,
  "Address": {
    "id": 1,
    "street": "blabla",
    "zipcode": 12345
  }
}

POST test/_update_by_query
{
  "query": { 
    "term": {
      "Address.id": 1
    }
  },
  "script": {
    "source": "ctx._source['Address']['id'] = 2;ctx._source['Address']['street'] = 'foo';ctx._source['Address']['zipcode'] = 5678"
  }
}

GET test/_doc/1

And the output then is:然后 output 是:

{
  "_index" : "test",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 9,
  "_seq_no" : 8,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "Address" : {
      "zipcode" : 5678,
      "street" : "foo",
      "id" : 2
    },
    "price" : 3000,
    "Id" : 1
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM