# Buggy code
def _initialize_indices(self):
    """Check each required index and make sure it exists, if it doesn't then create it"""
    for index in self.natlasIndices:
        if not self.es.indices.exists(index):
            self.es.indices.create(index)

    # Avoid a race condition
    time.sleep(2)

    for index in self.natlasIndices:
        if self.esversion.match(">=7.0.0"):
            self.es.indices.put_mapping(
                index=index,
                doc_type="_doc",
                body=self.mapping,
                include_type_name=True,
            )
        else:
            self.es.indices.put_mapping(index=index, doc_type="_doc", body=self.mapping)


# Traceback (KeyError)
172.18.0.1 - - [26/Jun/2020 08:50:28] "POST /admin/scans/delete/60ab2a6a40250d8c6c13e9f6d1a39e84 HTTP/1.1" 500 -
Traceback (most recent call last):
File "/.venv/lib/python3.8/site-packages/flask/app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "/.venv/lib/python3.8/site-packages/flask/app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "/.venv/lib/python3.8/site-packages/flask/app.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "/.venv/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/.venv/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/.venv/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/.venv/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/.venv/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/.venv/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/.venv/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/.venv/lib/python3.8/site-packages/flask_login/utils.py", line 272, in decorated_view
return func(*args, **kwargs)
File "/opt/natlas/natlas-server/app/auth/wrappers.py", line 38, in decorated_function
return f(*args, **kwargs)
File "/opt/natlas/natlas-server/app/admin/routes.py", line 463, in delete_scan
deleted = current_app.elastic.delete_scan(scan_id)
File "/opt/natlas/natlas-server/app/elastic/interface.py", line 153, in delete_scan
result = self.client.execute_delete_by_query(
File "/opt/natlas/natlas-server/app/elastic/client.py", line 132, in execute_delete_by_query
self._attach_shard_span_attrs(span, results)
File "/opt/natlas/natlas-server/app/elastic/client.py", line 165, in _attach_shard_span_attrs
span.add_attribute("es.shards.total", results["_shards"]["total"])
KeyError: '_shards'

# Fixed code
def _initialize_indices(self):
    """Check each required index and make sure it exists, if it doesn't then create it"""
    for index in self.natlasIndices:
        if not self.es.indices.exists(index):
            self.es.indices.create(index)

    # Avoid a race condition
    time.sleep(2)

    for index in self.natlasIndices:
        if self.esversion.match("<7.0.0"):
            self.es.indices.put_mapping(
                index=index,
                doc_type="_doc",
                body=self.mapping,
                include_type_name=True,
            )
        else:
            self.es.indices.put_mapping(index=index, body=self.mapping)