Created
March 7, 2022 15:57
-
-
Save xcsrz/3edc49bbf350d403bdca57f78d8df9d2 to your computer and use it in GitHub Desktop.
Reconcile descrepencies in document counts within 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
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