mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-12-05 14:39:23 -05:00
The failure runs the classifier on golden vs generated: additive means regenerate only, breaking means bump too.
263 lines
9.4 KiB
Go
263 lines
9.4 KiB
Go
package bleve_test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
|
|
bleveSearch "github.com/blevesearch/bleve/v2"
|
|
"github.com/blevesearch/bleve/v2/analysis/analyzer/custom"
|
|
"github.com/blevesearch/bleve/v2/analysis/token/lowercase"
|
|
"github.com/blevesearch/bleve/v2/analysis/tokenizer/unicode"
|
|
bleveMapping "github.com/blevesearch/bleve/v2/mapping"
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
|
|
"github.com/opencloud-eu/opencloud/pkg/log"
|
|
"github.com/opencloud-eu/opencloud/services/search/pkg/bleve"
|
|
searchmapping "github.com/opencloud-eu/opencloud/services/search/pkg/mapping"
|
|
"github.com/opencloud-eu/opencloud/services/search/pkg/search"
|
|
)
|
|
|
|
var _ = Describe("Index", func() {
|
|
Describe("NewIndex", func() {
|
|
It("puts the index into a directory of its own generation", func() {
|
|
root := GinkgoT().TempDir()
|
|
|
|
index, _, err := bleve.NewIndex(root, log.NopLogger())
|
|
Expect(err).ToNot(HaveOccurred())
|
|
DeferCleanup(index.Close)
|
|
|
|
Expect(index.Name()).To(Equal(filepath.Join(root, fmt.Sprintf("bleve-v%d", search.SchemaVersion))))
|
|
Expect(filepath.Join(root, fmt.Sprintf("bleve-v%d", search.SchemaVersion))).To(BeADirectory())
|
|
})
|
|
|
|
It("opens the index that is already there", func() {
|
|
root := GinkgoT().TempDir()
|
|
|
|
index, _, err := bleve.NewIndex(root, log.NopLogger())
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(index.Close()).To(Succeed())
|
|
|
|
reopened, _, err := bleve.NewIndex(root, log.NopLogger())
|
|
Expect(err).ToNot(HaveOccurred())
|
|
DeferCleanup(reopened.Close)
|
|
|
|
Expect(reopened.Name()).To(Equal(filepath.Join(root, fmt.Sprintf("bleve-v%d", search.SchemaVersion))))
|
|
})
|
|
})
|
|
})
|
|
|
|
var _ = Describe("NewIndex", func() {
|
|
var root string
|
|
|
|
BeforeEach(func() {
|
|
root = GinkgoT().TempDir()
|
|
})
|
|
|
|
codeMapping := func() *bleveMapping.IndexMappingImpl {
|
|
m, err := bleve.NewMapping()
|
|
Expect(err).ToNot(HaveOccurred())
|
|
impl, ok := m.(*bleveMapping.IndexMappingImpl)
|
|
Expect(ok).To(BeTrue())
|
|
return impl
|
|
}
|
|
|
|
// buildIndex simulates an index left behind by an older release
|
|
buildIndex := func(m bleveMapping.IndexMapping, docs map[string]map[string]any) {
|
|
idx, err := bleveSearch.New(filepath.Join(root, fmt.Sprintf("bleve-v%d", search.SchemaVersion)), m)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
for id, doc := range docs {
|
|
Expect(idx.Index(id, doc)).To(Succeed())
|
|
}
|
|
Expect(idx.Close()).To(Succeed())
|
|
}
|
|
|
|
It("creates a fresh index", func() {
|
|
idx, classification, err := bleve.NewIndex(root, log.NopLogger())
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(classification.Verdict).To(Equal(searchmapping.VerdictEqual))
|
|
Expect(idx.Close()).To(Succeed())
|
|
})
|
|
|
|
It("opens an index with an identical schema", func() {
|
|
buildIndex(codeMapping(), nil)
|
|
|
|
idx, classification, err := bleve.NewIndex(root, log.NopLogger())
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(classification.Verdict).To(Equal(searchmapping.VerdictEqual))
|
|
Expect(classification.NewFields).To(BeEmpty())
|
|
Expect(idx.Close()).To(Succeed())
|
|
})
|
|
|
|
It("treats a genuinely new field as additive", func() {
|
|
old := codeMapping()
|
|
Expect(old.DefaultMapping.Properties).To(HaveKey("Title"))
|
|
delete(old.DefaultMapping.Properties, "Title")
|
|
buildIndex(old, nil)
|
|
|
|
idx, classification, err := bleve.NewIndex(root, log.NopLogger())
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(classification.Verdict).To(Equal(searchmapping.VerdictAdditive))
|
|
Expect(classification.NewFields).To(ConsistOf("Title"))
|
|
Expect(idx.Index("1", map[string]any{"Title": "hello"})).To(Succeed())
|
|
Expect(idx.Close()).To(Succeed())
|
|
})
|
|
|
|
It("treats a new nested field as additive", func() {
|
|
old := codeMapping()
|
|
photo := old.DefaultMapping.Properties["photo"]
|
|
Expect(photo).ToNot(BeNil())
|
|
Expect(photo.Properties).To(HaveKey("cameraMake"))
|
|
delete(photo.Properties, "cameraMake")
|
|
buildIndex(old, nil)
|
|
|
|
idx, classification, err := bleve.NewIndex(root, log.NopLogger())
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(classification.Verdict).To(Equal(searchmapping.VerdictAdditive))
|
|
Expect(classification.NewFields).To(ConsistOf("photo.cameraMake"))
|
|
Expect(idx.Close()).To(Succeed())
|
|
})
|
|
|
|
It("persists an additive schema change so later startups classify it as equal", func() {
|
|
old := codeMapping()
|
|
delete(old.DefaultMapping.Properties, "Title")
|
|
buildIndex(old, nil)
|
|
|
|
idx, classification, err := bleve.NewIndex(root, log.NopLogger())
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(classification.Verdict).To(Equal(searchmapping.VerdictAdditive))
|
|
Expect(idx.Index("1", map[string]any{"Title": "hello"})).To(Succeed())
|
|
Expect(idx.Close()).To(Succeed())
|
|
|
|
idx, classification, err = bleve.NewIndex(root, log.NopLogger())
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(classification.Verdict).To(Equal(searchmapping.VerdictEqual))
|
|
Expect(idx.Close()).To(Succeed())
|
|
})
|
|
|
|
It("refuses when a new field already has data in the index", func() {
|
|
old := codeMapping()
|
|
Expect(old.DefaultMapping.Properties).To(HaveKey("Mtime"))
|
|
delete(old.DefaultMapping.Properties, "Mtime")
|
|
buildIndex(old, map[string]map[string]any{"1": {"Mtime": "2026-01-02T03:04:05Z"}})
|
|
|
|
idx, _, err := bleve.NewIndex(root, log.NopLogger())
|
|
Expect(err).To(MatchError(searchmapping.ErrManualActionRequired))
|
|
Expect(idx).To(BeNil())
|
|
})
|
|
|
|
It("refuses when a new object field already has nested data in the index", func() {
|
|
old := codeMapping()
|
|
Expect(old.DefaultMapping.Properties).To(HaveKey("photo"))
|
|
delete(old.DefaultMapping.Properties, "photo")
|
|
buildIndex(old, map[string]map[string]any{"1": {"photo": map[string]any{"cameraMake": "ACME"}}})
|
|
|
|
_, _, err := bleve.NewIndex(root, log.NopLogger())
|
|
Expect(err).To(MatchError(searchmapping.ErrManualActionRequired))
|
|
})
|
|
|
|
It("refuses on a changed field definition", func() {
|
|
old := codeMapping()
|
|
name := old.DefaultMapping.Properties["Name"]
|
|
Expect(name).ToNot(BeNil())
|
|
Expect(name.Fields).ToNot(BeEmpty())
|
|
name.Fields[0].Analyzer = "standard"
|
|
buildIndex(old, nil)
|
|
|
|
_, _, err := bleve.NewIndex(root, log.NopLogger())
|
|
Expect(err).To(MatchError(searchmapping.ErrManualActionRequired))
|
|
})
|
|
|
|
It("refuses when a stored field was removed from the code schema", func() {
|
|
old := codeMapping()
|
|
old.DefaultMapping.AddFieldMappingsAt("Legacy", bleveSearch.NewTextFieldMapping())
|
|
buildIndex(old, nil)
|
|
|
|
_, _, err := bleve.NewIndex(root, log.NopLogger())
|
|
Expect(err).To(MatchError(searchmapping.ErrManualActionRequired))
|
|
})
|
|
|
|
It("refuses when a default_mapping attribute changed", func() {
|
|
old := codeMapping()
|
|
old.DefaultMapping.Dynamic = false
|
|
buildIndex(old, nil)
|
|
|
|
_, _, err := bleve.NewIndex(root, log.NopLogger())
|
|
Expect(err).To(MatchError(searchmapping.ErrManualActionRequired))
|
|
})
|
|
|
|
It("refuses on a changed analyzer definition", func() {
|
|
old := codeMapping()
|
|
Expect(old.CustomAnalysis.Analyzers).To(HaveKey(searchmapping.WordsAnalyzer))
|
|
old.CustomAnalysis.Analyzers[searchmapping.WordsAnalyzer] = map[string]any{
|
|
"type": custom.Name,
|
|
"tokenizer": unicode.Name,
|
|
"token_filters": []string{lowercase.Name},
|
|
}
|
|
buildIndex(old, nil)
|
|
|
|
_, _, err := bleve.NewIndex(root, log.NopLogger())
|
|
Expect(err).To(MatchError(searchmapping.ErrManualActionRequired))
|
|
})
|
|
})
|
|
|
|
var _ = Describe("NewMapping", func() {
|
|
It("only references registered analyzers", func() {
|
|
m, err := bleve.NewMapping()
|
|
Expect(err).ToNot(HaveOccurred())
|
|
impl, ok := m.(*bleveMapping.IndexMappingImpl)
|
|
Expect(ok).To(BeTrue())
|
|
Expect(impl.Validate()).To(Succeed())
|
|
})
|
|
|
|
// A diff here means existing indexes will classify as breaking (schema or
|
|
// bleve marshaling changed). Update the golden deliberately; on a marshaling
|
|
// change, bump search.SchemaVersion too or existing indexes refuse to start.
|
|
It("matches the committed golden mapping", func() {
|
|
m, err := bleve.NewMapping()
|
|
Expect(err).ToNot(HaveOccurred())
|
|
b, err := json.Marshal(m)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
var got, golden map[string]any
|
|
Expect(json.Unmarshal(b, &got)).To(Succeed())
|
|
|
|
if os.Getenv("UPDATE_GOLDEN") != "" {
|
|
pretty, err := json.MarshalIndent(m, "", " ")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(os.WriteFile("testdata/mapping.golden.json", append(pretty, '\n'), 0o644)).To(Succeed())
|
|
}
|
|
|
|
goldenB, err := os.ReadFile("testdata/mapping.golden.json")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(json.Unmarshal(goldenB, &golden)).To(Succeed())
|
|
|
|
Expect(got).To(Equal(golden), goldenAdvice(golden, got))
|
|
})
|
|
})
|
|
|
|
// goldenAdvice classifies a golden diff so the failure says whether the change
|
|
// is additive (regenerate with UPDATE_GOLDEN=1) or breaking (bump
|
|
// search.SchemaVersion too).
|
|
func goldenAdvice(golden, got map[string]any) string {
|
|
dig := func(m map[string]any, path ...string) map[string]any {
|
|
for _, k := range path {
|
|
m, _ = m[k].(map[string]any)
|
|
}
|
|
return m
|
|
}
|
|
c := searchmapping.Classify(dig(golden, "default_mapping", "properties"), dig(got, "default_mapping", "properties"), nil)
|
|
if !reflect.DeepEqual(dig(golden, "analysis"), dig(got, "analysis")) {
|
|
c.AddBreaking("the analysis definitions changed")
|
|
}
|
|
switch c.Verdict {
|
|
case searchmapping.VerdictAdditive:
|
|
return fmt.Sprintf("additive schema change (new fields: %v): regenerate the golden with UPDATE_GOLDEN=1, no SchemaVersion bump needed", c.NewFields)
|
|
case searchmapping.VerdictBreaking:
|
|
return fmt.Sprintf("breaking schema change (%v): regenerate the golden with UPDATE_GOLDEN=1 and bump search.SchemaVersion", c.Reasons)
|
|
}
|
|
return "the mapping changed outside the classified tree (bleve marshaling drift?): regenerate the golden with UPDATE_GOLDEN=1 and see the SchemaVersion note above"
|
|
}
|