in src/es_pii_tool/helpers/elastic_api.py [0:0]
def report_segment_count(client: 'Elasticsearch', index: str) -> str:
"""
Report the count of segments from index
:param client: A client connection object
:param index: The index to check
:type client: :py:class:`~.elasticsearch.Elasticsearch`
:type index: str
:returns: Formatted message describing shard count and segment count for index
"""
shardcount = 0
segmentcount = 0
try:
output = client.cat.shards(
index=index, format='json', h=['index', 'shard', 'prirep', 'sc']
)
except Exception as exc:
logger.error('Exception: %s', exc)
raise BadClientResult('Unable to get cat shards output', exc)
for shard in output:
if shard['prirep'] == 'r': # type: ignore
# Skip replica shards
continue
if index != shard['index']: # type: ignore
logger.warning(
'Index name %s does not match what was returned by the _cat API: %s',
index,
shard['index'], # type: ignore
)
shardcount += 1
segmentcount += int(shard['sc']) # type: ignore
logger.debug(
'Index %s, shard %s has %s segments',
index,
shard["shard"], # type: ignore
shard["sc"], # type: ignore
)
return (
f'index {index} has {shardcount} shards and a total of {segmentcount} '
f'segments, averaging {float(segmentcount/shardcount)} segments per shard'
)