使用 delete_by_query 方法可以删除 Elasticsearch 中符合多个条件的数据。以下步骤展示如何实现:

  1. 构造查询条件

根据需要删除数据的多个字段,构建一个符合要求的查询条件。可以使用 bool 查询来组合多个条件。

  1. 执行 delete_by_query 操作

使用 delete_by_query 方法,传入查询条件,即可删除符合条件的所有文档。

以下是示例代码:

from elasticsearch import Elasticsearch

es = Elasticsearch()

# 构造查询条件
query = {
  'query': {
    'bool': {
      'must': [
        {'match': {'field1': 'value1'}},
        {'match': {'field2': 'value2'}}
      ]
    }
  }
}

# 执行 delete_by_query 操作
response = es.delete_by_query(index='my_index', body=query)

print(response)

这段代码会删除 my_index 中所有 field1 为 value1 且 field2 为 value2 的文档。

Python Elasticsearch: 删除符合多个条件的数据