Created
June 14, 2019 12:57
-
-
Save pydevd/819516744843cde3667884ab1ac17cba to your computer and use it in GitHub Desktop.
Remove all records of specified _type from ElasticSearch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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