This commit is contained in:
hailin 2024-10-28 22:28:34 +08:00
parent 8db9449b36
commit 8087eabd5e
1 changed files with 25 additions and 11 deletions

36
app.py
View File

@ -106,21 +106,35 @@ def extract_index_structure(es_client, index_name):
existing_fields[full_key] = "dict"
parse_document(value, full_key, existing_fields)
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)
elif len(value) > 0:
existing_fields[full_key] = f"list[{type(value[0]).__name__}]"
else:
# 遍历列表中的所有元素,确保全面检查类型
list_types = set()
for item in value:
list_types.add(type(item).__name__)
if isinstance(item, dict):
parse_document(item, full_key, existing_fields)
if len(list_types) == 0:
existing_fields[full_key] = "list[empty]"
elif len(list_types) == 1:
existing_fields[full_key] = f"list[{list_types.pop()}]"
else:
existing_fields[full_key] = "list[mixed]"
else:
existing_fields[full_key] = type(value).__name__
elif isinstance(doc, list):
if len(doc) > 0:
if isinstance(doc[0], dict):
parse_document(doc[0], parent_key, existing_fields)
else:
existing_fields[f"{parent_key}[]"] = type(doc[0]).__name__
# 如果是根级别的列表,遍历元素并记录类型
list_types = set()
for item in doc:
list_types.add(type(item).__name__)
if isinstance(item, dict):
parse_document(item, parent_key, existing_fields)
if len(list_types) == 0:
existing_fields[parent_key] = "list[empty]"
elif len(list_types) == 1:
existing_fields[parent_key] = f"list[{list_types.pop()}]"
else:
existing_fields[parent_key] = "list[mixed]"
else:
existing_fields[parent_key] = type(doc).__name__