How to delete data in Elasticsearch based on certain conditions?

The Delete By Query API in Elasticsearch can be used to delete data based on certain conditions. Here is an example:

from elasticsearch import Elasticsearch

# 连接到Elasticsearch实例
es = Elasticsearch(['localhost:9200'])

# 删除匹配条件的文档
delete_query = {
    "query": {
        "match": {
            "field1": "value1"
        }
    }
}

response = es.delete_by_query(index='your_index', body=delete_query)
print(response)

In the examples above, we used a match query to specify the deletion criteria. “Field1” is the field name to be matched, and “value1” is the value to be matched. You can modify the query criteria according to your needs.

Please note that the deletion action is irreversible, please use caution.

bannerAds