根据查询从elasticsearch中的数组中移除对象

0yg35tkg  于 2021-06-10  发布在  ElasticSearch
关注(0)|答案(1)|浏览(366)

我想从elastic doc的嵌套结构中删除一个对象,这就是我的elastic doc在索引'submissions'中的样子。根据条件,我想从所有文档中删除一个对象。

{
  "took": 21,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 11,
    "max_score": 1,
    "hits": [
      {
        "_index": "submissions",
        "_type": "_doc",
        "_id": "15_12069",
        "_score": 1,
        "_source": {
          "id": "15_12069",
          "account_id": 2,
          "survey_id": 15,
          "submission_id": 12069,
          "answers": [
            {
              "question_id": 142,     //
              "skipped": false,       //<------ remove object with question_id: 142
              "answer_txt": "product" //
            },
            {
              "question_id": 153,
              "skipped": false,
              "answer_txt": "happy"
            }
          ]
        }
      },
      {
        "_index": "submissions",
        "_type": "_doc",
        "_id": "15_12073",
        "_score": 1,
        "_source": {
          "id": "15_12073",
          "account_id": 2,
          "survey_id": 15,
          "submission_id": 12073,
          "answers": [
            {
              "question_id": 142,       //
              "skipped": false,         //<------ remove object with question_id: 142
              "answer_txt": "coherent"  //
            },
            {
              "question_id": 153,
              "skipped": false,
              "answer_txt": "cool"
            }
          ]
        }
      }
    ]
  }
}

我想尝试updatebyquery api(\u update \u by \u query)和ctx.\u source.remove with query

{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "must": [
              {
                "match": {
                  "account_id": 2
                }
              },
              {
                "match": {
                  "survey_id": 15
                }
              }
            ]
          }
        },
        {
          "nested": {
            "path": "answers",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "answers.question_id": 142
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

对此有什么见解吗?或者我有更好的方法吗?

pb3skfrl

pb3skfrl1#

您可以按以下方式使用updatebyqueryapi
添加索引数据、Map和查询的工作示例
索引Map:

{
  "mappings": {
    "properties": {
      "answers": {
        "type": "nested"
      }
    }
  }
}

索引数据:

{
  "id": "15_12069",
  "account_id": 2,
  "survey_id": 15,
  "submission_id": 12069,
  "answers": [
    {
      "question_id": 142, 
      "skipped": false, 
      "answer_txt": "product" 
    },
    {
      "question_id": 153,
      "skipped": false,
      "answer_txt": "happy"
    }
  ]
}
{
      "id": "15_12073",
      "account_id": 2,
      "survey_id": 16,
      "submission_id": 12073,
      "answers": [
        {
          "question_id": 142,
          "skipped": false,
          "answer_txt": "coherent"
        },
        {
          "question_id": 153,
          "skipped": false,
          "answer_txt": "cool"
        }
      ]
    }

查询:

POST /index/_update_by_query
{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "must": [
              {
                "match": {
                  "account_id": 2
                }
              },
              {
                "match": {
                  "survey_id": 15
                }
              }
            ]
          }
        },
        {
          "nested": {
            "path": "answers",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "answers.question_id": 142
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  },
  "script": {
    "source": "ctx._source.answers.removeIf(question_id -> question_id.question_id == params.remove_id);",
    "params": {
      "remove_id": 142
    }
  }
}

执行上述查询后,满足所有查询条件的文档。 "account_id": 2 以及 "survey_id": 15 以及 "answers.question_id": 142 ,从具有 question_id: 142 已删除。
因此,从第一个文档(如上索引所示),包含 "answers.question_id": 142 现在文档包含以下数据(运行查询后)

{
  "_index": "64898361",
  "_type": "_doc",
  "_id": "1",
  "_version": 8,
  "_seq_no": 13,
  "_primary_term": 1,
  "found": true,
  "_source": {
    "survey_id": 15,
    "submission_id": 12069,
    "account_id": 2,
    "answers": [
      {
        "answer_txt": "happy",
        "question_id": 153,
        "skipped": false
      }
    ],
    "id": "15_12069"
  }
}

第二个文档不会有任何更改,因为它不满足所有查询条件。

相关问题