Skip to content

Commit

Permalink
Bug修复-Open search 查询结果错位问题 (#2833)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: 王飞 <[email protected]>
  • Loading branch information
feiazifeiazi and 王飞 authored Oct 11, 2024
1 parent f473743 commit b71b2c5
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions sql/engines/elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,29 +387,30 @@ 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")
source_data = hit.get("_source", {})

# 转换需要转换为 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 字符串

# 构建结果行
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
Expand Down

0 comments on commit b71b2c5

Please sign in to comment.