<p>由于几个原因,已从版本2中的ES核心中删除了Delete By Query API。这个函数变成了一个插件。您可以在此处查找更多详细信息:</p>
<p><a href="https://www.elastic.co/guide/en/elasticsearch/plugins/2.0/delete-by-query-plugin-reason.html" rel="noreferrer">Why Delete-By-Query is a plugin</a></p>
<p><a href="https://www.elastic.co/guide/en/elasticsearch/plugins/2.3/plugins-delete-by-query.html" rel="noreferrer">Delete By Query Plugin</a></p>
<p>因为我不想添加另一个依赖项(因为我以后需要在docker映像中运行这个),所以我编写了一个自己的函数来解决这个问题。我的解决方案是搜索具有指定索引和类型的所有引号。之后,我使用Bulk API删除它们:</p>
<pre><code>def delete_es_type(es, index, type_):
try:
count = es.count(index, type_)['count']
response = es.search(
index=index,
filter_path=["hits.hits._id"],
body={"size": count, "query": {"filtered" : {"filter" : {
"type" : {"value": type_ }}}}})
ids = [x["_id"] for x in response["hits"]["hits"]]
if len(ids) > 0:
return
bulk_body = [
'{{"delete": {{"_index": "{}", "_type": "{}", "_id": "{}"}}}}'
.format(index, type_, x) for x in ids]
es.bulk('\n'.join(bulk_body))
# es.indices.flush_synced([index])
except elasticsearch.exceptions.TransportError as ex:
print("Elasticsearch error: " + ex.error)
raise ex
</code></pre>
<p>我希望这能帮助未来的谷歌用户;)</p>