From e63b1a20087e336eea24bea52dcecad45fedfe8e Mon Sep 17 00:00:00 2001 From: Dominik Schmidt Date: Fri, 27 Nov 2026 16:14:34 +0100 Subject: [PATCH] test(search): hold both engines to the same order in the parity suite A Sorting section runs every case against bleve and OpenSearch and compares the order of the matches: size, modification time, track length, year with a second key, the date a photo was taken, a file without the sort value, names in code point order, and the fields that are not sortable. Its rows keep the order of the answer in the README, and the fixtures list the values that are sorted by. --- services/search/pkg/parity/README.md | 77 +++++++---- services/search/pkg/parity/matrix_test.go | 40 +++++- services/search/pkg/parity/sort_test.go | 159 ++++++++++++++++++++++ 3 files changed, 249 insertions(+), 27 deletions(-) create mode 100644 services/search/pkg/parity/sort_test.go diff --git a/services/search/pkg/parity/README.md b/services/search/pkg/parity/README.md index 5465614142..1de12810bb 100644 --- a/services/search/pkg/parity/README.md +++ b/services/search/pkg/parity/README.md @@ -272,7 +272,7 @@ Fixtures: - `plain.txt`, ID = 1$1!plain.txt - `box`, ID = 1$1!box, folder - `boxed.txt`, ID = 1$1!boxed.txt, Path = ./box/boxed.txt -- `song.mp3`, ID = 1$1!song.mp3, MimeType = audio/mpeg +- `song.mp3`, ID = 1$1!song.mp3, MimeType = audio/mpeg, audio.duration = 200 | Case | Query | expected | bleve | OpenSearch | same? | |---|---|---|---|---|---| @@ -828,7 +828,7 @@ Fixtures: Fixtures: -- `some_song.mp3`, ID = 1$1!5, MimeType = audio/mpeg +- `some_song.mp3`, ID = 1$1!5, MimeType = audio/mpeg, audio.duration = 225000, audio.year = 2004 - `team.jpg`, ID = 1$1!6, MimeType = image/jpeg - `lost.jpg`, ID = 1$1!7, MimeType = image/jpeg @@ -845,17 +845,17 @@ Fixtures: Fixtures: -- `a.mp3`, MimeType = audio/mpeg, Tags = live, remaster -- `b.mp3`, MimeType = audio/mpeg, Tags = live -- `c.mp3`, MimeType = audio/mpeg -- `d.mp3`, MimeType = audio/mpeg -- `e.mp3`, MimeType = audio/mpeg -- `f.mp3`, MimeType = audio/mpeg -- `g.mp3`, MimeType = audio/mpeg -- `a.jpg`, MimeType = image/jpeg -- `b.jpg`, MimeType = image/jpeg -- `c.jpg`, MimeType = image/jpeg -- `d.jpg`, MimeType = image/jpeg +- `a.mp3`, MimeType = audio/mpeg, Tags = live, remaster, audio.year = 1971 +- `b.mp3`, MimeType = audio/mpeg, Tags = live, audio.year = 1975 +- `c.mp3`, MimeType = audio/mpeg, audio.year = 1982 +- `d.mp3`, MimeType = audio/mpeg, audio.year = 1999 +- `e.mp3`, MimeType = audio/mpeg, audio.year = 2001 +- `f.mp3`, MimeType = audio/mpeg, audio.year = 2005 +- `g.mp3`, MimeType = audio/mpeg, audio.year = 2009 +- `a.jpg`, MimeType = image/jpeg, photo.takenDateTime = 2018-08-11T09:15:00Z +- `b.jpg`, MimeType = image/jpeg, photo.takenDateTime = 2018-08-11T19:42:00Z +- `c.jpg`, MimeType = image/jpeg, photo.takenDateTime = 2018-09-01T12:00:00Z +- `d.jpg`, MimeType = image/jpeg, photo.takenDateTime = 2021-08-11T08:00:00Z - `live.txt`, Tags = Apple, apple, Banana | Case | Query | expected | bleve | OpenSearch | same? | @@ -906,16 +906,16 @@ Fixtures: Fixtures: -- `card-00000.mp3`, MimeType = audio/mpeg -- `card-00001.mp3`, MimeType = audio/mpeg -- `card-00002.mp3`, MimeType = audio/mpeg -- `card-00003.mp3`, MimeType = audio/mpeg -- `card-00004.mp3`, MimeType = audio/mpeg -- `card-00005.mp3`, MimeType = audio/mpeg -- `card-00006.mp3`, MimeType = audio/mpeg -- `card-00007.mp3`, MimeType = audio/mpeg -- `card-00008.mp3`, MimeType = audio/mpeg -- `card-00009.mp3`, MimeType = audio/mpeg +- `card-00000.mp3`, MimeType = audio/mpeg, audio.year = 1950 +- `card-00001.mp3`, MimeType = audio/mpeg, audio.year = 1951 +- `card-00002.mp3`, MimeType = audio/mpeg, audio.year = 1952 +- `card-00003.mp3`, MimeType = audio/mpeg, audio.year = 1953 +- `card-00004.mp3`, MimeType = audio/mpeg, audio.year = 1954 +- `card-00005.mp3`, MimeType = audio/mpeg, audio.year = 1955 +- `card-00006.mp3`, MimeType = audio/mpeg, audio.year = 1956 +- `card-00007.mp3`, MimeType = audio/mpeg, audio.year = 1957 +- `card-00008.mp3`, MimeType = audio/mpeg, audio.year = 1958 +- `card-00009.mp3`, MimeType = audio/mpeg, audio.year = 1959 - ... and 10040 more of the same | Case | Query | expected | bleve | OpenSearch | same? | @@ -949,3 +949,34 @@ Fixtures: | AGG-57 | `mediatype:image` reads `filtered to the cell below 0,1, which a point a billionth of a degree south of the edge lies in` | just-south.jpg | just-south.jpg | just-south.jpg | ✅ | | AGG-58 | `mediatype:image` reads `filtered to the last cell, which keeps its edges at 90 and 180` | north-east-corner.jpg | north-east-corner.jpg | north-east-corner.jpg | ✅ | | AGG-56 | `mediatype:image` reads `filtered to a key that is no geohash` | bad request | bad request | bad request | ✅ | + +## Sorting + +### sorting + +Fixtures: + +- `b.mp3`, MimeType = audio/mpeg, Size = 300, Mtime = 2024-03-01T00:00:00Z, audio.duration = 200000, audio.year = 1999 +- `a.mp3`, MimeType = audio/mpeg, Size = 100, Mtime = 2024-01-01T00:00:00Z, audio.duration = 300000, audio.year = 2005 +- `C.mp3`, MimeType = audio/mpeg, Size = 200, Mtime = 2024-02-01T00:00:00Z, audio.duration = 100000, audio.year = 1999 +- `d.mp3`, MimeType = audio/mpeg, Size = 200, Mtime = 2024-04-01T00:00:00Z +- `x.jpg`, MimeType = image/jpeg, photo.takenDateTime = 2020-06-01T12:00:00Z +- `y.jpg`, MimeType = image/jpeg, photo.takenDateTime = 2010-06-01T12:00:00Z +- `z.jpg`, MimeType = image/jpeg +- `apple.txt` +- `Zebra.txt` +- `Übung.txt` + +| Case | Query | expected | bleve | OpenSearch | same? | +|---|---|---|---|---|---| +| SORT-01 | `mediatype:audio` reads `by size, a tie in the order of the ids` | a.mp3, C.mp3, d.mp3, b.mp3 | a.mp3, C.mp3, d.mp3, b.mp3 | a.mp3, C.mp3, d.mp3, b.mp3 | ✅ | +| SORT-02 | `mediatype:audio` reads `by size descending, a tie still in the order of the ids` | b.mp3, C.mp3, d.mp3, a.mp3 | b.mp3, C.mp3, d.mp3, a.mp3 | b.mp3, C.mp3, d.mp3, a.mp3 | ✅ | +| SORT-03 | `mediatype:audio` reads `newest first` | d.mp3, b.mp3, C.mp3, a.mp3 | d.mp3, b.mp3, C.mp3, a.mp3 | d.mp3, b.mp3, C.mp3, a.mp3 | ✅ | +| SORT-04 | `mediatype:audio` reads `shortest track first, a file without a duration last` | C.mp3, b.mp3, a.mp3, d.mp3 | C.mp3, b.mp3, a.mp3, d.mp3 | C.mp3, b.mp3, a.mp3, d.mp3 | ✅ | +| SORT-05 | `mediatype:audio` reads `longest track first, a file without a duration still last` | a.mp3, b.mp3, C.mp3, d.mp3 | a.mp3, b.mp3, C.mp3, d.mp3 | a.mp3, b.mp3, C.mp3, d.mp3 | ✅ | +| SORT-06 | `mediatype:audio` reads `by year, within a year the longest track first` | b.mp3, C.mp3, a.mp3, d.mp3 | b.mp3, C.mp3, a.mp3, d.mp3 | b.mp3, C.mp3, a.mp3, d.mp3 | ✅ | +| SORT-07 | `name:"*.txt"` reads `by name in code point order: upper case before lower case, an umlaut after z` | Zebra.txt, apple.txt, Übung.txt | Zebra.txt, apple.txt, Übung.txt | Zebra.txt, apple.txt, Übung.txt | ✅ | +| SORT-08 | `mediatype:image` reads `latest photo first, a file without a date last` | x.jpg, y.jpg, z.jpg | x.jpg, y.jpg, z.jpg | x.jpg, y.jpg, z.jpg | ✅ | +| SORT-09 | `mediatype:image` reads `earliest photo first, a file without a date still last` | y.jpg, x.jpg, z.jpg | y.jpg, x.jpg, z.jpg | y.jpg, x.jpg, z.jpg | ✅ | +| SORT-10 | `mediatype:audio` reads `a field with several values is not sortable` | bad request | bad request | bad request | ✅ | +| SORT-11 | `mediatype:audio` reads `an unknown field is not sortable` | bad request | bad request | bad request | ✅ | diff --git a/services/search/pkg/parity/matrix_test.go b/services/search/pkg/parity/matrix_test.go index 0bfcf4b23e..62efd00365 100644 --- a/services/search/pkg/parity/matrix_test.go +++ b/services/search/pkg/parity/matrix_test.go @@ -39,6 +39,8 @@ type matrixRow struct { WantBadRequest bool // Overrides is what an engine is held to instead, rendered like expected Overrides map[string]string + // Ordered keeps the names in the order of the answer, for rows about it + Ordered bool GroupAt, CaseAt, QueryAt int } @@ -213,7 +215,7 @@ func writeMatrix(report types.Report) { query = row.Context + ", then " + query } - expected := matrixNames(row.Want) + expected := row.names(row.Want) switch { case row.WantBadRequest: expected = "bad request" @@ -223,7 +225,7 @@ func writeMatrix(report types.Report) { fmt.Fprintf(out, "| %s | %s | %s | %s | %s | %s |\n", row.ID, query, expected, - matrixNames(row.answered["bleve"]), matrixNames(row.answered["opensearch"]), matrixVerdict(row)) + row.names(row.answered["bleve"]), row.names(row.answered["opensearch"]), matrixVerdict(row)) } if os.Getenv("UPDATE_SEARCH_PARITY_MATRIX") != "" { @@ -284,6 +286,12 @@ func matrixFixtures(group string) string { } } + for _, g := range sortGroups() { + if g.name == group { + fixtures = g.fixtures + } + } + if len(fixtures) == 0 { return "Fixtures: none" } @@ -356,6 +364,18 @@ func fixtureFields(f search.Resource, withID bool) string { add("Mtime = %s", f.Mtime.Format(time.RFC3339)) } + if f.Audio != nil && f.Audio.Duration != nil { + add("audio.duration = %d", *f.Audio.Duration) + } + + if f.Audio != nil && f.Audio.Year != nil { + add("audio.year = %d", *f.Audio.Year) + } + + if f.Photo != nil && f.Photo.TakenDateTime != nil { + add("photo.takenDateTime = %s", f.Photo.TakenDateTime.Format(time.RFC3339)) + } + if f.Hidden { add("hidden") } @@ -389,6 +409,18 @@ func matrixScope(ref *searchMessage.Reference) string { // its head plus a count instead of flooding the README. const matrixNamesHead = 8 +func (r matrixRow) names(names []string) string { + if !r.Ordered || len(names) == 0 { + return matrixNames(names) + } + + shortened := make([]string, 0, len(names)) + for _, name := range names { + shortened = append(shortened, shorten(name)) + } + return strings.Join(shortened, ", ") +} + func matrixNames(names []string) string { if len(names) == 0 { return "no match" @@ -428,9 +460,9 @@ func matrixVerdict(row *matrixResult) string { var off []string known := true for _, engine := range engineNames { - answer := matrixNames(row.answered[engine]) + answer := row.names(row.answered[engine]) - agrees := answer == matrixNames(row.Want) + agrees := answer == row.names(row.Want) switch { case row.WantBadRequest: agrees = answer == "bad request" diff --git a/services/search/pkg/parity/sort_test.go b/services/search/pkg/parity/sort_test.go new file mode 100644 index 0000000000..e11fed8ba6 --- /dev/null +++ b/services/search/pkg/parity/sort_test.go @@ -0,0 +1,159 @@ +package parity + +import ( + "context" + "fmt" + "time" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + libregraph "github.com/opencloud-eu/libre-graph-api-go" + "github.com/opencloud-eu/reva/v3/pkg/errtypes" + + searchService "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/services/search/v0" + "github.com/opencloud-eu/opencloud/services/search/pkg/search" +) + +// sortCase holds the engines to the order of their matches. +type sortCase struct { + id int + query string + orderBy []*searchService.SortProperty + reads string + want []string + wantBadRequest bool +} + +type sortGroup struct { + name string + fixtures []search.Resource + cases []sortCase +} + +func (c sortCase) label() string { return fmt.Sprintf("SORT-%02d", c.id) } + +func asc(name string) *searchService.SortProperty { + return &searchService.SortProperty{Name: name} +} + +func desc(name string) *searchService.SortProperty { + return &searchService.SortProperty{Name: name, IsDescending: true} +} + +func track(name string, size uint64, mtime string, duration int64, year int32) search.Resource { + return fixtureDoc(name, withMime("audio/mpeg"), withSize(size), withMtime(mtime), + withAudio(&libregraph.Audio{Duration: libregraph.PtrInt64(duration), Year: libregraph.PtrInt32(year)})) +} + +func takenPhoto(name string, taken string) search.Resource { + t, err := time.Parse(time.RFC3339, taken) + if err != nil { + panic(err) + } + return fixtureDoc(name, withMime("image/jpeg"), withPhoto(&libregraph.Photo{TakenDateTime: &t})) +} + +func sortGroups() []sortGroup { + return []sortGroup{ + { + name: "sorting", + fixtures: []search.Resource{ + track("b.mp3", 300, "2024-03-01T00:00:00Z", 200000, 1999), + track("a.mp3", 100, "2024-01-01T00:00:00Z", 300000, 2005), + track("C.mp3", 200, "2024-02-01T00:00:00Z", 100000, 1999), + fixtureDoc("d.mp3", withMime("audio/mpeg"), withSize(200), withMtime("2024-04-01T00:00:00Z")), + takenPhoto("x.jpg", "2020-06-01T12:00:00Z"), + takenPhoto("y.jpg", "2010-06-01T12:00:00Z"), + fixtureDoc("z.jpg", withMime("image/jpeg")), + fixtureDoc("apple.txt"), + fixtureDoc("Zebra.txt"), + fixtureDoc("Übung.txt"), + }, + cases: []sortCase{ + {id: 1, query: `mediatype:audio`, orderBy: []*searchService.SortProperty{asc("size")}, + reads: "by size, a tie in the order of the ids", + want: []string{"a.mp3", "C.mp3", "d.mp3", "b.mp3"}}, + {id: 2, query: `mediatype:audio`, orderBy: []*searchService.SortProperty{desc("size")}, + reads: "by size descending, a tie still in the order of the ids", + want: []string{"b.mp3", "C.mp3", "d.mp3", "a.mp3"}}, + {id: 3, query: `mediatype:audio`, orderBy: []*searchService.SortProperty{desc("lastModifiedDateTime")}, + reads: "newest first", + want: []string{"d.mp3", "b.mp3", "C.mp3", "a.mp3"}}, + {id: 4, query: `mediatype:audio`, orderBy: []*searchService.SortProperty{asc("audio.duration")}, + reads: "shortest track first, a file without a duration last", + want: []string{"C.mp3", "b.mp3", "a.mp3", "d.mp3"}}, + {id: 5, query: `mediatype:audio`, orderBy: []*searchService.SortProperty{desc("audio.duration")}, + reads: "longest track first, a file without a duration still last", + want: []string{"a.mp3", "b.mp3", "C.mp3", "d.mp3"}}, + {id: 6, query: `mediatype:audio`, orderBy: []*searchService.SortProperty{asc("audio.year"), desc("audio.duration")}, + reads: "by year, within a year the longest track first", + want: []string{"b.mp3", "C.mp3", "a.mp3", "d.mp3"}}, + {id: 7, query: `name:"*.txt"`, orderBy: []*searchService.SortProperty{asc("name")}, + reads: "by name in code point order: upper case before lower case, an umlaut after z", + want: []string{"Zebra.txt", "apple.txt", "Übung.txt"}}, + {id: 8, query: `mediatype:image`, orderBy: []*searchService.SortProperty{desc("photo.takenDateTime")}, + reads: "latest photo first, a file without a date last", + want: []string{"x.jpg", "y.jpg", "z.jpg"}}, + {id: 9, query: `mediatype:image`, orderBy: []*searchService.SortProperty{asc("photo.takenDateTime")}, + reads: "earliest photo first, a file without a date still last", + want: []string{"y.jpg", "x.jpg", "z.jpg"}}, + {id: 10, query: `mediatype:audio`, orderBy: []*searchService.SortProperty{asc("Tags")}, + reads: "a field with several values is not sortable", wantBadRequest: true}, + {id: 11, query: `mediatype:audio`, orderBy: []*searchService.SortProperty{asc("nonexistent")}, + reads: "an unknown field is not sortable", wantBadRequest: true}, + }, + }, + } +} + +var _ = Describe("Sorting", func() { + for groupAt, group := range sortGroups() { + Describe(group.name, Ordered, ContinueOnFailure, func() { + var engines []testEngine + + BeforeAll(func() { + engines = newEngines("opencloud-test-engine-parity-sort-"+group.name, group.fixtures) + }) + + for caseAt, c := range group.cases { + row := matrixRow{ + Section: "Sorting", Group: group.name, ID: c.label(), + Query: c.query, Reads: c.reads, + Want: c.want, WantBadRequest: c.wantBadRequest, Ordered: true, + GroupAt: 200 + groupAt, CaseAt: caseAt, + } + planRow(row) + + Describe(c.label()+" "+c.reads, func() { + for _, name := range engineNames { + It("on "+name, func() { + e := engineNamed(engines, name) + if e.unavailable != "" { + recordSkip(row, name) + Skip(e.unavailable) + } + + request, err := query(c.query, nil, 0) + Expect(err).NotTo(HaveOccurred(), "the query of a sort case has to parse") + request.OrderBy = c.orderBy + resp, err := e.backend.Search(context.Background(), request) + if c.wantBadRequest { + recordAnswer(row, name, badRequestAnswer(err)) + Expect(err).To(BeAssignableToTypeOf(errtypes.BadRequest(""))) + return + } + + Expect(err).NotTo(HaveOccurred(), "the query has to answer") + answer := make([]string, 0, len(resp.Matches)) + for _, m := range resp.Matches { + answer = append(answer, m.GetEntity().GetName()) + } + recordAnswer(row, name, answer) + Expect(answer).To(Equal(c.want)) + }) + } + }) + } + }) + } +})