7d4ffdbfe5
build / build (push) Successful in 1m15s
全新 WeKnora 会用空映射({})先建好索引,范围字段等第一次写入才动态生成。
首次同步在读目标当前状态时就报 "missing release scope fields" 并失败,等于
"往空的生产实例做第一次同步"这条路根本走不通。
现在:"没有范围字段 + 索引确实 0 篇文档" = 空基线;有文档却看不出归属仍然报错
(绝不把无法归属的文档当空库覆盖)。Count 与整库导出共用同一判断。
试过:make verify 15 项全绿;新增空/非空两种索引的用例;真实 sync 复现原错误。
60 lines
1.9 KiB
Go
60 lines
1.9 KiB
Go
package v071
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
// 全新的 WeKnora 实例会先用空映射建索引(`{}`),字段等第一次写入才动态生成。
|
|
// 这种索引不能被当成"索引被改坏了":只有确实 0 篇文档时才是空基线;
|
|
// 有文档却没有范围字段 = 不知道这批文档属于谁,必须报错。
|
|
func TestEmptyScopeIndexCountsAsEmptyBaselineOnlyWhenTrulyEmpty(t *testing.T) {
|
|
for _, testCase := range []struct {
|
|
name string
|
|
docCount int
|
|
wantErr bool
|
|
}{
|
|
{"空索引当空基线", 0, false},
|
|
{"有文档但没有范围字段要报错", 7, true},
|
|
} {
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
switch r.URL.Path {
|
|
case "/chunks/_mapping":
|
|
_, _ = w.Write([]byte(`{"chunks":{"mappings":{}}}`))
|
|
case "/chunks/_count":
|
|
_ = json.NewEncoder(w).Encode(map[string]any{"count": testCase.docCount})
|
|
default:
|
|
http.NotFound(w, r)
|
|
}
|
|
}))
|
|
defer server.Close()
|
|
exporter, err := NewSearchExporter(SearchExporterConfig{BaseURL: server.URL, Index: "chunks", PageSize: 100})
|
|
if err != nil {
|
|
t.Fatalf("exporter: %v", err)
|
|
}
|
|
identity := SearchIdentity{TenantID: 10001, KnowledgeBaseID: "kb-1"}
|
|
count, countErr := exporter.Count(context.Background(), identity)
|
|
if testCase.wantErr {
|
|
if countErr == nil {
|
|
t.Fatalf("有文档却当成空基线:count=%d", count)
|
|
}
|
|
} else {
|
|
if countErr != nil || count != 0 {
|
|
t.Fatalf("空索引没有被当成空基线:count=%d err=%v", count, countErr)
|
|
}
|
|
}
|
|
_, exportErr := exporter.ExportLibrary(context.Background(), identity, 100)
|
|
if testCase.wantErr && exportErr == nil {
|
|
t.Fatal("导出把有文档的空映射索引当成空库")
|
|
}
|
|
if !testCase.wantErr && exportErr != nil {
|
|
t.Fatalf("空索引导出失败:%v", exportErr)
|
|
}
|
|
})
|
|
}
|
|
}
|