Skip to content

Instantly share code, notes, and snippets.

@palawer
Created January 22, 2021 08:00
Show Gist options
  • Select an option

  • Save palawer/e3406383d0cc9a9372fe35028b3632e5 to your computer and use it in GitHub Desktop.

Select an option

Save palawer/e3406383d0cc9a9372fe35028b3632e5 to your computer and use it in GitHub Desktop.
from elasticsearch import Elasticsearch
from elasticsearch.helpers import bulk, scan
es = Elasticsearch()
BULK_SIZE = 100
INDEX_NAME = 'reviews'
ES_QUERY = {
"query": {
"query_string": {
#"query": "*"#WARNING!
"query": "location_id:2121230"
}
},
"_source" : False
}
docs = []
for doc in scan(es, index=INDEX_NAME, query=ES_QUERY):
doc['_op_type'] = 'delete'
docs.append(doc)
if len(docs) >= BULK_SIZE:
success, fail = bulk(es, docs, stats_only=True)
print('DELETE Index: {}, Success: {}, Fail: {}'.format(INDEX_NAME, success, fail))
docs = []
if docs:#remaining records
success, fail = bulk(es, docs, stats_only=True)
print('DELETE Index: {}, Success: {}, Fail: {}'.format(INDEX_NAME, success, fail))
print('Done')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment