Skip to content

Instantly share code, notes, and snippets.

@xcsrz
Created March 7, 2022 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xcsrz/3edc49bbf350d403bdca57f78d8df9d2 to your computer and use it in GitHub Desktop.
Save xcsrz/3edc49bbf350d403bdca57f78d8df9d2 to your computer and use it in GitHub Desktop.
Reconcile descrepencies in document counts within elasticsearch
from requests import get
from json import dumps, loads
from prettytable import PrettyTable
src_server = 'http://localhost:9200'
def docCount(idx=None, typ=None):
url = src_server
if idx:
url += "/%s" % idx
if typ:
url += "/%s" % typ
url += "/_count"
res = loads(get(url).content)
return res['count'] if 'count' in res else 0
tbl = PrettyTable(['Index', 'Count on Index', 'Docs In Types'])
tbl.add_row(['entire cluster', docCount(), 'n/a'])
tbl.add_row(['=====','=====','====='])
src_state=loads(get("%s/_cluster/state" % (src_server), timeout=600).content)
docsCountedInIndexes = 0
docsCountedInTypes = 0
for idx in src_state['metadata']['indices']:
docsInIndex = docCount(idx)
docsCountedInIndexes += docsInIndex
docsInTypes = 0
for typ in src_state['metadata']['indices'][idx]['mappings']:
docsInTypes += docCount(idx, typ)
docsCountedInTypes += docsInTypes
tbl.add_row([idx, docsInIndex, docsInTypes])
tbl.add_row(['-----','------','-----'])
tbl.add_row(['Totals', docsCountedInIndexes, docsCountedInTypes])
print(tbl)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment