fix(v071): 全新目标实例的空索引按空基线处理
build / build (push) Successful in 1m15s

全新 WeKnora 会用空映射({})先建好索引,范围字段等第一次写入才动态生成。
首次同步在读目标当前状态时就报 "missing release scope fields" 并失败,等于
"往空的生产实例做第一次同步"这条路根本走不通。

现在:"没有范围字段 + 索引确实 0 篇文档" = 空基线;有文档却看不出归属仍然报错
(绝不把无法归属的文档当空库覆盖)。Count 与整库导出共用同一判断。

试过:make verify 15 项全绿;新增空/非空两种索引的用例;真实 sync 复现原错误。
This commit is contained in:
姚章浩
2026-10-29 06:17:26 +08:00
parent 84e8a0dfdc
commit 7d4ffdbfe5
5 changed files with 109 additions and 1 deletions
+18 -1
View File
@@ -67,6 +67,23 @@ type searchFieldNames struct {
// errIndexMissing 表示目标索引根本不存在(空生产基线)。
var errIndexMissing = errors.New("Elasticsearch index missing")
// errScopeFieldsMissing 表示索引存在但还没有发布范围字段:
// 全新的 WeKnora 实例会先用空映射建好索引(`{}`),字段要等第一次写入才动态生成。
// 这跟"索引被改坏了"必须分开:只有在索引里确实一篇文档都没有时才能当成空基线。
var errScopeFieldsMissing = errors.New("Elasticsearch index mapping is missing release scope fields")
// emptyScopeIndex 判断"没有范围字段"的索引是不是真的空。
// 有文档却没有范围字段 = 无法判断这批文档属于谁,必须报错,不能当成 0。
func (exporter *SearchExporter) emptyScopeIndex(ctx context.Context) (bool, error) {
var response struct {
Count int64 `json:"count"`
}
if err := exporter.requestJSON(ctx, http.MethodGet, "/"+exporter.index+"/_count", nil, &response); err != nil {
return false, err
}
return response.Count == 0, nil
}
type mappingInspection struct {
digest string
fields searchFieldNames
@@ -127,7 +144,7 @@ func (exporter *SearchExporter) inspectMapping(ctx context.Context) (mappingInsp
base: resolve("knowledge_base_id"),
}
if fields.knowledge == "" || fields.base == "" {
return mappingInspection{}, errors.New("Elasticsearch index mapping is missing release scope fields")
return mappingInspection{}, errScopeFieldsMissing
}
return mappingInspection{digest: hex.EncodeToString(digest[:]), fields: fields}, nil
}