From b71b2c53ef1b7c84d536855729bf962460f21acc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=99=93=E9=A3=9E?= Date: Fri, 11 Oct 2024 10:07:27 +0800 Subject: [PATCH] =?UTF-8?q?Bug=E4=BF=AE=E5=A4=8D-Open=20search=20=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E7=BB=93=E6=9E=9C=E9=94=99=E4=BD=8D=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20(#2833)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --------- Co-authored-by: 王飞 --- sql/engines/elasticsearch.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/sql/engines/elasticsearch.py b/sql/engines/elasticsearch.py index a4ecd98e47..075989a8f1 100644 --- a/sql/engines/elasticsearch.py +++ b/sql/engines/elasticsearch.py @@ -387,6 +387,8 @@ def query( hits = response.get("hits", {}).get("hits", []) # 处理查询结果,将列表和字典转换为 JSON 字符串 rows = [] + all_search_keys = {} # 用于收集所有字段的集合 + all_search_keys["_id"] = None for hit in hits: # 获取文档 ID 和 _source 数据 doc_id = hit.get("_id") @@ -394,6 +396,7 @@ def query( # 转换需要转换为 JSON 字符串的字段 for key, value in source_data.items(): + all_search_keys[key] = None # 收集所有字段名 if isinstance(value, (list, dict)): # 如果字段是列表或字典 source_data[key] = json.dumps(value) # 转换为 JSON 字符串 @@ -401,15 +404,13 @@ def query( row = {"_id": doc_id, **source_data} rows.append(row) - # 如果有结果,获取字段名作为列名 - if rows: - first_row = rows[0] - column_list = list(first_row.keys()) - else: - column_list = [] - + column_list = list(all_search_keys.keys()) # 构建结果集 - result_set.rows = [tuple(row.values()) for row in rows] # 只获取值 + result_set.rows = [] + for row in rows: + # 按照 column_list 的顺序填充每一行 + result_row = tuple(row.get(key, None) for key in column_list) + result_set.rows.append(result_row) result_set.column_list = column_list result_set.affected_rows = len(result_set.rows) return result_set