fix: 修复编码查询匹配失效

This commit is contained in:
皓月归尘 2025-02-17 19:38:34 +08:00
parent ec7f383c43
commit 25103727a0

View File

@ -255,7 +255,7 @@ async def get_code_list(request: Request,
query = { query = {
"query": { "query": {
"match": { "match": {
"brief_description": { "description": {
"query": description.strip(), "query": description.strip(),
"fuzziness": "AUTO" # 自动模糊匹配 "fuzziness": "AUTO" # 自动模糊匹配
} }
@ -275,14 +275,14 @@ async def get_code_list(request: Request,
max_score = data["hits"].get("max_score", 1) max_score = data["hits"].get("max_score", 1)
# 处理每一条匹配结果 # 处理每一条匹配结果
for hit in data["hits"]["hits"]: for hit in data["hits"]["hits"]:
code = await Code.get_or_none(code=hit["_source"]["hts8"]) code = await Code.get_or_none(code=str(hit["_source"]["code"]))
# 归一化匹配度,转换为百分比 # 归一化匹配度,转换为百分比
match_rate = round((hit["_score"] / max_score) * 100, 2) # 归一化后计算百分比 match_rate = round((hit["_score"] / max_score) * 100, 2) # 归一化后计算百分比
# 将匹配结果添加到列表中 # 将匹配结果添加到列表中
matches.append({ matches.append({
"id": code.id if code else None, "id": code.id if code else None,
"code": hit["_source"]["hts8"], # 获取商品编码 "code": hit["_source"]["code"], # 获取商品编码
"description": hit["_source"]["brief_description"], # 获取商品描述 "description": hit["_source"]["description"], # 获取商品描述
"match_rate": match_rate # 匹配度(百分比) "match_rate": match_rate # 匹配度(百分比)
}) })
query_code = await QueryCode.create( query_code = await QueryCode.create(
@ -355,7 +355,7 @@ async def get_code_list(request: Request,
query = { query = {
"query": { "query": {
"match": { "match": {
"brief_description": { "description": {
"query": row["text"].strip(), "query": row["text"].strip(),
"fuzziness": "AUTO" # 自动模糊匹配 "fuzziness": "AUTO" # 自动模糊匹配
} }
@ -375,14 +375,14 @@ async def get_code_list(request: Request,
max_score = data["hits"].get("max_score", 1) max_score = data["hits"].get("max_score", 1)
# 处理每一条匹配结果 # 处理每一条匹配结果
for hit in data["hits"]["hits"]: for hit in data["hits"]["hits"]:
code = await Code.get_or_none(code=hit["_source"]["hts8"]) code = await Code.get_or_none(code=str(hit["_source"]["code"]))
# 归一化匹配度,转换为百分比 # 归一化匹配度,转换为百分比
match_rate = round((hit["_score"] / max_score) * 100, 2) # 归一化后计算百分比 match_rate = round((hit["_score"] / max_score) * 100, 2) # 归一化后计算百分比
# 将匹配结果添加到列表中 # 将匹配结果添加到列表中
matches.append({ matches.append({
"id": code.id if code else None, "id": code.id if code else None,
"code": hit["_source"]["hts8"], # 获取商品编码 "code": hit["_source"]["code"], # 获取商品编码
"description": hit["_source"]["brief_description"], # 获取商品描述 "description": hit["_source"]["description"], # 获取商品描述
"match_rate": match_rate # 匹配度(百分比) "match_rate": match_rate # 匹配度(百分比)
}) })
query_code = await QueryCode.create( query_code = await QueryCode.create(