Skip to content

Instantly share code, notes, and snippets.

@pydevd
Created June 14, 2019 12:57
Show Gist options
  • Save pydevd/819516744843cde3667884ab1ac17cba to your computer and use it in GitHub Desktop.
Save pydevd/819516744843cde3667884ab1ac17cba to your computer and use it in GitHub Desktop.
Remove all records of specified _type from ElasticSearch
def drop_all_doc_types(es: Elasticsearch, type_=ES_DOC_TYPE):
while es.count()["count"] > 0:
r = es.search(
index=ES_INDEX,
doc_type=type_,
filter_path=["hits.hits._id"],
body={"query": {"match_all": {}}},
size=10000,
)
ids = [x["_id"] for x in r["hits"]["hits"]] if r else []
if not ids:
return
bulk_body = [
'{{"delete": {{"_index": "{}", "_type": "{}", "_id": "{}"}}}}'.format(
ES_INDEX, type_, x
)
for x in ids
]
es.bulk("\n".join(bulk_body))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment