From 8db9449b3682a38b1716c5276bd48c22a308ca8e Mon Sep 17 00:00:00 2001 From: hailin Date: Mon, 28 Oct 2024 22:24:42 +0800 Subject: [PATCH] . --- app.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/app.py b/app.py index ee21705..062db06 100644 --- a/app.py +++ b/app.py @@ -81,10 +81,9 @@ def load_indices_from_es(key: str, index: str, buf: Dict[str, Any]) -> Any: return None - def extract_index_structure(es_client, index_name): """ - 扫描 Elasticsearch 索引中的一个文档,提取字段结构(包括嵌套字段),生成包含所有字段的层级关系和类型信息。 + 扫描 Elasticsearch 索引中的一个文档,递归提取字段结构(包括嵌套字段),生成包含所有字段的层级关系和类型信息。 :param es_client: Elasticsearch 客户端实例 :param index_name: 要提取的索引名称 @@ -103,15 +102,17 @@ def extract_index_structure(es_client, index_name): if isinstance(doc, dict): for key, value in doc.items(): 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): + existing_fields[full_key] = "dict" parse_document(value, full_key, existing_fields) - elif isinstance(value, list) and len(value) > 0: - if isinstance(value[0], dict): + elif isinstance(value, list): + if len(value) > 0 and isinstance(value[0], dict): + existing_fields[full_key] = "list[dict]" parse_document(value[0], full_key, existing_fields) - else: + elif len(value) > 0: existing_fields[full_key] = f"list[{type(value[0]).__name__}]" + else: + existing_fields[full_key] = "list[empty]" else: existing_fields[full_key] = type(value).__name__ elif isinstance(doc, list): @@ -119,7 +120,7 @@ def extract_index_structure(es_client, index_name): if isinstance(doc[0], dict): parse_document(doc[0], parent_key, existing_fields) else: - existing_fields[parent_key] = f"list[{type(doc[0]).__name__}]" + existing_fields[f"{parent_key}[]"] = type(doc[0]).__name__ else: existing_fields[parent_key] = type(doc).__name__ @@ -136,7 +137,6 @@ def extract_index_structure(es_client, index_name): return {} - @app.route('/') def index(): # 默认展示第一个客户