elasticsearch删除具有特定日期时间的所有嵌套对象

oprakyz7  于 2021-06-10  发布在  ElasticSearch
关注(0)|答案(2)|浏览(324)

我用的是elasticsearch 5.6 schedule 嵌套对象的嵌套字段

{
              "status": "open",
              "starts_at": "2020-10-13T17:00:00-05:00",
              "ends_at": "2020-10-13T18:00:00-05:00"
            },
            {
              "status": "open",
              "starts_at": "2020-10-13T18:00:00-05:00",
              "ends_at": "2020-10-13T19:30:00-05:00"
            }

我要找的是一个 Painless 将删除多个等于 starts_at 现场。我尝试了多种方法,但都没有成功,它们运行正常,但不会删除目标对象

azpvetkf

azpvetkf1#

您可以使用updatebyquery进行相同的查询。

POST <indexName>/<type>/_update_by_query
{
  "query":{ // <======== Filter out the parent documents containing the specified nested date
    "match": {
      "schedule.starts_at": "2020-10-13T17:00:00-05:00"
    }
  },
  "script":{ // <============ use the script to remove the schedule containing specific start date
    "inline": "ctx._source.schedule.removeIf(e -> e.starts_at == '2020-10-13T17:00:00-05:00')"
  }
}
gajydyqb

gajydyqb2#

可以通过循环和使用 SimpleDateFormat ```
POST index/_update_by_query
{
"script": {"source": "for(int i=0;i< ctx._source.schedule.length;i++){
SimpleDateFormat sdformat = new SimpleDateFormat('yyyy-MM-dd\'T\'HH:mm:ss');
boolean equalDateTime = sdformat.parse(ctx._source.schedule[i].starts_at).compareTo(sdformat.parse(params.starts_at)) == 0;
if(equalDateTime) {
ctx._source.schedule.remove(i)
}
}",
"params": {
"starts_at": "2020-10-13T17:00:00-05:00"
},
"lang": "painless"
},
"query":{
"bool": {"must":[
{"terms":{"_id":["12345"]}}
]}}
}

相关问题