真人照片转二次元后怎么换画风?原照与二次元成图的两种用法
2026-11-21
2026-11-27 0
看到分数断言超出容差时,先记录父 ID、嵌套片段及其分数,再核对查询是否启用了重评分。9.4 分支的兼容性用例曾连续九次报错。
嵌套 kNN 的 score_mode 只支持 max。父文档凭最匹配的嵌套向量参与排序,inner_hits 返回父文档内的具体片段及其分数。量化索引可以先用压缩表示找候选,再按配置用原始向量对候选重评分。比较分数时,要确认比较的是同一个片段,并核对两个阶段使用的向量表示。
下面的脚本使用 Python 3 标准库。在本地一次性 Elasticsearch 9.4.0 实例运行,预先把 ES_URL 指向该实例,并确保 nested-score-demo 尚未创建。脚本建单分片索引,写入两个父文档和三个四维向量,刷新后合并为单段。两次查询只更改 rescore_vector.oversample。
import json
import os
from urllib.request import Request, urlopen
base = os.environ["ES_URL"]
def call(method, path, body=None):
data = json.dumps(body).encode() if body is not None else None
request = Request(base + path, data, {"Content-Type": "application/json"}, method=method)
with urlopen(request) as response:
return json.load(response)
index = "/nested-score-demo"
mapping = {
"settings": {"number_of_shards": 1, "number_of_replicas": 0},
"mappings": {"properties": {"chunks": {
"type": "nested", "properties": {
"label": {"type": "keyword"},
"vec": {"type": "dense_vector", "dims": 4, "similarity": "l2_norm",
"index_options": {"type": "int8_hnsw"}}
}
}}}
}
call("PUT", index, mapping)
for parent, vectors in {
"a": [("near", [0.12, 0.21, 0.35, 0.51]), ("far", [0.9, 0.1, 0.3, 0.6])],
"b": [("middle", [0.4, 0.2, 0.3, 0.5])],
}.items():
call("PUT", f"{index}/_doc/{parent}", {
"chunks": [{"label": label, "vec": vector} for label, vector in vectors]
})
call("POST", index + "/_refresh")
call("POST", index + "/_forcemerge?max_num_segments=1")
for oversample in (0, 2):
result = call("POST", index + "/_search", {"query": {"nested": {
"path": "chunks", "score_mode": "max",
"query": {"knn": {"field": "chunks.vec", "query_vector": [0.1, 0.2, 0.3, 0.5],
"k": 2, "num_candidates": 10,
"rescore_vector": {"oversample": oversample}}},
"inner_hits": {"name": "matches", "size": 2}
}}})
hits = result["hits"]["hits"]
assert [hit["_id"] for hit in hits] == ["a", "b"]
for hit in hits:
children = hit["inner_hits"]["matches"]["hits"]["hits"]
assert children[0]["_source"]["label"] == {"a": "near", "b": "middle"}[hit["_id"]]
print(oversample, hit["_id"], hit["_score"],
[(child["_source"]["label"], child["_score"]) for child in children])
实测中,oversample=0 时,两个父文档的分数均与各自首个嵌套命中相等。改成 2 后,父文档 a 从 0.9967931 变为 0.99690956,首个嵌套命中仍为 0.9967931。父文档顺序和首个片段身份均保持一致。

用 a 的 near 片段手算复核。查询向量与它的平方距离为 0.0031,按 l2_norm 的计分公式 1 / (1 + 0.0031) 得约 0.99690958,与重评分后的父分数吻合。这个对照表明,在本机 9.4.0 的这个映射和查询下,开启重评分后需要分别核对两级分数。
上游的评分修复让 inner_hits 遵循查询阶段的重评分决策,优先读取请求中的覆盖值,其次读取字段配置。没有启用重评分时仍使用量化分数。该改动明确排除了由各段自行决定重评分的 bbq_disk。随后,上游曾暂停相关兼容性用例,主分支的用例后来恢复运行。本地实验只覆盖发布版的 REST API,未运行上游 Gradle CI 套件。
复验兼容性用例时,先锁定原分支和随机种子,并用固定向量识别返回片段。检查阈值时,把空的 inner_hits 和分数偏差分别记录。