This commit is contained in:
hailin 2024-10-28 22:24:42 +08:00
parent ea4dee565a
commit 8db9449b36
1 changed files with 9 additions and 9 deletions

18
app.py
View File

@ -81,10 +81,9 @@ def load_indices_from_es(key: str, index: str, buf: Dict[str, Any]) -> Any:
return None return None
def extract_index_structure(es_client, index_name): def extract_index_structure(es_client, index_name):
""" """
扫描 Elasticsearch 索引中的一个文档提取字段结构包括嵌套字段生成包含所有字段的层级关系和类型信息 扫描 Elasticsearch 索引中的一个文档递归提取字段结构包括嵌套字段生成包含所有字段的层级关系和类型信息
:param es_client: Elasticsearch 客户端实例 :param es_client: Elasticsearch 客户端实例
:param index_name: 要提取的索引名称 :param index_name: 要提取的索引名称
@ -103,15 +102,17 @@ def extract_index_structure(es_client, index_name):
if isinstance(doc, dict): if isinstance(doc, dict):
for key, value in doc.items(): for key, value in doc.items():
full_key = f"{parent_key}.{key}" if parent_key else key full_key = f"{parent_key}.{key}" if parent_key else key
if full_key not in existing_fields:
existing_fields[full_key] = type(value).__name__
if isinstance(value, dict): if isinstance(value, dict):
existing_fields[full_key] = "dict"
parse_document(value, full_key, existing_fields) parse_document(value, full_key, existing_fields)
elif isinstance(value, list) and len(value) > 0: elif isinstance(value, list):
if isinstance(value[0], dict): if len(value) > 0 and isinstance(value[0], dict):
existing_fields[full_key] = "list[dict]"
parse_document(value[0], full_key, existing_fields) parse_document(value[0], full_key, existing_fields)
else: elif len(value) > 0:
existing_fields[full_key] = f"list[{type(value[0]).__name__}]" existing_fields[full_key] = f"list[{type(value[0]).__name__}]"
else:
existing_fields[full_key] = "list[empty]"
else: else:
existing_fields[full_key] = type(value).__name__ existing_fields[full_key] = type(value).__name__
elif isinstance(doc, list): elif isinstance(doc, list):
@ -119,7 +120,7 @@ def extract_index_structure(es_client, index_name):
if isinstance(doc[0], dict): if isinstance(doc[0], dict):
parse_document(doc[0], parent_key, existing_fields) parse_document(doc[0], parent_key, existing_fields)
else: else:
existing_fields[parent_key] = f"list[{type(doc[0]).__name__}]" existing_fields[f"{parent_key}[]"] = type(doc[0]).__name__
else: else:
existing_fields[parent_key] = type(doc).__name__ existing_fields[parent_key] = type(doc).__name__
@ -136,7 +137,6 @@ def extract_index_structure(es_client, index_name):
return {} return {}
@app.route('/') @app.route('/')
def index(): def index():
# 默认展示第一个客户 # 默认展示第一个客户