You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python 2.7执行Elasticsearch删除请求及排查代码问题?

Answers to Your Elasticsearch & Python Questions

Hey there! Let's tackle your questions one by one, tailored to your Elasticsearch 6.1.0 and elasticsearch-py 5.4.0 setup:

1. How to Delete Single Documents by ID in Python (Equivalent to Your Curl Command)

Your curl command works great for single IDs, and in Python, you can use the es.delete() method to replicate this behavior. Here's how:

from elasticsearch import Elasticsearch

# Initialize the ES client (connects to localhost:9200 by default)
es = Elasticsearch()

# Delete a single document
es.delete(index="index_data", doc_type="type_data", id="a")

If you want to delete multiple IDs from your list x=['a','b','c','d'], loop through them with error handling to catch missing documents:

x = ['a','b','c','d']
for doc_id in x:
    try:
        es.delete(index="index_data", doc_type="type_data", id=doc_id)
        print(f"Successfully deleted document with ID: {doc_id}")
    except Exception as e:
        print(f"Failed to delete {doc_id}: {str(e)}")

2. How to Delete All Data in type_data (Or the Entire Type)

In Elasticsearch 6.x, types are still supported but being phased out (removed in 7.x). You have two options here:

Option 1: Delete All Documents in type_data

Use delete_by_query with a match_all query to remove every document in the type:

request_body = {"query": {"match_all": {}}}
es.delete_by_query(index="index_data", doc_type="type_data", body=request_body)

Option 2: Delete the Entire type_data Mapping (And All Its Data)

If you want to completely remove the type's mapping along with its data, use the indices.delete_mapping method:

es.indices.delete_mapping(index="index_data", doc_type="type_data")

Note: This will remove the type definition entirely, so you'll need to recreate it if you want to store data there again.

3. Why Your delete_by_query Code Isn't Working

Let's fix the issues in your code step by step:

Key Problems in Your Original Code:

  1. Undefined es_index variable: You need to replace this with your actual index name ("index_data").
  2. Missing doc_type parameter: In elasticsearch-py 5.x (paired with ES 6.x), you must explicitly specify the doc_type for delete_by_query operations.

Corrected Code:

from elasticsearch import Elasticsearch

es = Elasticsearch()
# Define your index and type names explicitly
es_index = "index_data"
doc_type = "type_data"

request_body = {
    "query": {
        "ids": {
            "values": ['a','b','c','d','e','f']
        }
    }
}

try:
    response = es.delete_by_query(index=es_index, doc_type=doc_type, body=request_body)
    print(f"Successfully deleted {response['deleted']} documents")
except Exception as e:
    print(f"Error during deletion: {str(e)}")

Additional Notes:

  • Make sure the IDs in your values list actually exist in the index/type, otherwise you'll get "document not found" errors (which the try/except block will catch).
  • Since you're using ES 6.1.0 and elasticsearch-py 5.4.0, this combination is compatible, but keep in mind that newer ES versions (7+) remove types entirely, so your code would need adjustments if you upgrade later.

内容的提问来源于stack exchange,提问作者Snehal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:01:12