diff --git a/services/search/pkg/mapping/fieldindex.go b/services/search/pkg/mapping/fieldindex.go index 5bc5d21b66..c8bdd8e86d 100644 --- a/services/search/pkg/mapping/fieldindex.go +++ b/services/search/pkg/mapping/fieldindex.go @@ -34,3 +34,35 @@ func FieldNameIndex(t reflect.Type, overrides map[string]FieldOpts) map[string]s walk(t, "") return out } + +// NumericFields lists the fields of t (json names, nested as parent.child) that +// both backends index as numbers, so a range restriction can target them. The +// parts of a geopoint are not numbers of their own. +func NumericFields(t reflect.Type, overrides map[string]FieldOpts) map[string]struct{} { + out := map[string]struct{}{} + var walk func(t reflect.Type, prefix string) + walk = func(t reflect.Type, prefix string) { + _ = walkFields(t, func(fi fieldInfo) error { + name := fi.Name + if prefix != "" { + name = prefix + "." + fi.Name + } + + fieldType := overrides[name].Type + if fieldType == "" { + fieldType = inferType(fi.GoField.Type) + } + switch fieldType { + case TypeNumeric: + out[name] = struct{}{} + case TypeObject: + if sub := structType(fi.GoField.Type); sub != nil { + walk(sub, name) + } + } + return nil + }) + } + walk(t, "") + return out +} diff --git a/services/search/pkg/opensearch/internal/convert/kql_transpile.go b/services/search/pkg/opensearch/internal/convert/kql_transpile.go index 90e8df828e..61672f3313 100644 --- a/services/search/pkg/opensearch/internal/convert/kql_transpile.go +++ b/services/search/pkg/opensearch/internal/convert/kql_transpile.go @@ -3,7 +3,6 @@ package convert import ( "errors" "fmt" - "slices" "strconv" "strings" "time" @@ -219,7 +218,7 @@ func numberNodeQuery(node *ast.NumberNode) (osu.Builder, error) { return nil, fmt.Errorf("number node without operator: %w", ErrUnsupportedNodeType) } - if !slices.Contains([]string{"Size", "Type"}, node.Key) { + if !query.FieldIsNumeric(node.Key) { return osu.NewMatchNoneQuery(), nil } diff --git a/services/search/pkg/parity/README.md b/services/search/pkg/parity/README.md index 8980d7c439..f73000e16f 100644 --- a/services/search/pkg/parity/README.md +++ b/services/search/pkg/parity/README.md @@ -290,7 +290,7 @@ Fixtures: | FIELDS-12 | `id:"1$1!ab-23"` | no match | no match | no match | ✅ | | FIELDS-13 | `audio.artist:"Some Artist"` | song.mp3 | song.mp3 | song.mp3 | ✅ | | FIELDS-14 | `audio.artist:"some artist"` | song.mp3 | song.mp3 | song.mp3 | ✅ | -| FIELDS-15 | `audio.duration>100` | no match | no match | no match | ✅ | +| FIELDS-15 | `audio.duration>100` | song.mp3 | song.mp3 | song.mp3 | ✅ | ### deleted @@ -444,6 +444,23 @@ Fixtures: | RANGE-07 | `mtime>2021` | no match | no match | no match | ✅ | | RANGE-08 | `name>100` | no match | no match | no match | ✅ | +### photo + +Fixtures: + +- `bright.jpg`, MimeType = image/jpeg +- `dark.jpg`, MimeType = image/jpeg +- `plain.txt` + +| Case | Query | expected | bleve | OpenSearch | same? | +|---|---|---|---|---|---| +| PHOTO-01 | `photo.iso>=1000` | dark.jpg | dark.jpg | dark.jpg | ✅ | +| PHOTO-02 | `Photo.FNumber<2` | bright.jpg | bright.jpg | bright.jpg | ✅ | +| PHOTO-03 | `photo.focalLength>30 AND photo.iso<=3200` | dark.jpg | dark.jpg | dark.jpg | ✅ | +| PHOTO-04 | `photo.orientation>1` | dark.jpg | dark.jpg | dark.jpg | ✅ | +| PHOTO-05 | `photo.iso<=100` | bright.jpg | bright.jpg | bright.jpg | ✅ | +| PHOTO-06 | `photo.cameraMake>1` | no match | no match | no match | ✅ | + ### scope Fixtures: diff --git a/services/search/pkg/parity/fixtures_test.go b/services/search/pkg/parity/fixtures_test.go index 7f1569ebbe..9e75f037a1 100644 --- a/services/search/pkg/parity/fixtures_test.go +++ b/services/search/pkg/parity/fixtures_test.go @@ -50,6 +50,10 @@ func withAudio(audio *libregraph.Audio) fixtureOption { return func(r *search.Resource) { r.Audio = audio } } +func withPhoto(photo *libregraph.Photo) fixtureOption { + return func(r *search.Resource) { r.Photo = photo } +} + func withLocation(location *libregraph.GeoCoordinates) fixtureOption { return func(r *search.Resource) { r.Location = location } } diff --git a/services/search/pkg/parity/parity_test.go b/services/search/pkg/parity/parity_test.go index b424d71fed..55d85078c8 100644 --- a/services/search/pkg/parity/parity_test.go +++ b/services/search/pkg/parity/parity_test.go @@ -110,6 +110,7 @@ func queryGroups() []queryGroup { stressGroup(), everythingGroup(), rangeGroup(), + photoGroup(), scopeGroup(), invalidGroup(), quotingGroup(), diff --git a/services/search/pkg/parity/query_fields_test.go b/services/search/pkg/parity/query_fields_test.go index 5e3120b688..461ce4c0f5 100644 --- a/services/search/pkg/parity/query_fields_test.go +++ b/services/search/pkg/parity/query_fields_test.go @@ -36,7 +36,7 @@ func fieldsGroup() queryGroup { // a facet value keeps its case, the field is not marked lowercase {id: 13, query: `audio.artist:"Some Artist"`, want: []string{"song.mp3"}}, {id: 14, query: `audio.artist:"some artist"`, want: []string{"song.mp3"}}, // facets search case-insensitively - {id: 15, query: `audio.duration>100`}, // number queries are gated to Size and Type on both engines + {id: 15, query: `audio.duration>100`, want: []string{"song.mp3"}}, }, } } diff --git a/services/search/pkg/parity/query_photo_test.go b/services/search/pkg/parity/query_photo_test.go new file mode 100644 index 0000000000..716c413884 --- /dev/null +++ b/services/search/pkg/parity/query_photo_test.go @@ -0,0 +1,26 @@ +package parity + +import ( + libregraph "github.com/opencloud-eu/libre-graph-api-go" + + "github.com/opencloud-eu/opencloud/services/search/pkg/search" +) + +func photoGroup() queryGroup { + return queryGroup{ + name: "photo", + fixtures: []search.Resource{ + fixtureDoc("bright.jpg", withMime("image/jpeg"), withPhoto(&libregraph.Photo{CameraMake: libregraph.PtrString("Acme"), Iso: libregraph.PtrInt32(100), FNumber: libregraph.PtrFloat64(1.8), FocalLength: libregraph.PtrFloat64(26), Orientation: libregraph.PtrInt32(1)})), + fixtureDoc("dark.jpg", withMime("image/jpeg"), withPhoto(&libregraph.Photo{Iso: libregraph.PtrInt32(3200), FNumber: libregraph.PtrFloat64(4), FocalLength: libregraph.PtrFloat64(85), Orientation: libregraph.PtrInt32(6)})), + fixtureDoc("plain.txt"), + }, + cases: []queryCase{ + {id: 1, query: `photo.iso>=1000`, want: []string{"dark.jpg"}}, + {id: 2, query: `Photo.FNumber<2`, want: []string{"bright.jpg"}}, + {id: 3, query: `photo.focalLength>30 AND photo.iso<=3200`, want: []string{"dark.jpg"}}, + {id: 4, query: `photo.orientation>1`, want: []string{"dark.jpg"}}, + {id: 5, query: `photo.iso<=100`, want: []string{"bright.jpg"}}, + {id: 6, query: `photo.cameraMake>1`}, // a text field takes no range + }, + } +} diff --git a/services/search/pkg/query/bleve/compiler.go b/services/search/pkg/query/bleve/compiler.go index 6a4229bc9b..687baab508 100644 --- a/services/search/pkg/query/bleve/compiler.go +++ b/services/search/pkg/query/bleve/compiler.go @@ -2,7 +2,6 @@ package bleve import ( "fmt" - "slices" "strconv" "strings" @@ -202,11 +201,11 @@ func walk(offset int, nodes []ast.Node) (bleveQuery.Query, int, error) { } case *ast.NumberNode: var q bleveQuery.Query - if field := n.Key; slices.Contains([]string{"Size", "Type"}, field) { + if field := n.Key; searchQuery.FieldIsNumeric(field) { q = numberRange(field, n.Operator, n.Value) } else { - // same answer as the OpenSearch backend: unknown numeric keys - // match nothing instead of querying an arbitrary field + // same answer as the OpenSearch backend: a range on a non-numeric key + // matches nothing instead of querying an arbitrary field q = bleveQuery.NewMatchNoneQuery() } if q == nil { diff --git a/services/search/pkg/query/resolver.go b/services/search/pkg/query/resolver.go index 5628dd886f..1225076493 100644 --- a/services/search/pkg/query/resolver.go +++ b/services/search/pkg/query/resolver.go @@ -85,6 +85,18 @@ func FieldIsCaseInsensitive(field string) bool { return siblingFields()[field].Lowercase } +// numericFields are the fields indexed as numbers, derived from the resource struct. +var numericFields = sync.OnceValue(func() map[string]struct{} { + return mapping.NumericFields(reflect.TypeFor[search.Resource](), search.Resource{}.SearchFieldOverrides()) +}) + +// FieldIsNumeric reports whether a field is indexed as a number, so a range +// restriction can target it. +func FieldIsNumeric(field string) bool { + _, ok := numericFields()[field] + return ok +} + // FieldIsPath reports whether a field is a hierarchical path field. func FieldIsPath(field string) bool { _, ok := pathFields()[field] diff --git a/services/webdav/pkg/service/v0/search.go b/services/webdav/pkg/service/v0/search.go index 1dcf5a1c19..e38c4d1e4f 100644 --- a/services/webdav/pkg/service/v0/search.go +++ b/services/webdav/pkg/service/v0/search.go @@ -12,6 +12,7 @@ import ( "slices" "strconv" "strings" + "time" userv1beta1 "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" @@ -262,6 +263,9 @@ func matchToPropResponse(ctx context.Context, davPrefix, publicURL string, match propstatOK.Prop = append(propstatOK.Prop, prop.Escaped("oc:favorite", "1")) } + propstatOK.Prop = appendPhotoProps(propstatOK.Prop, match.GetEntity().GetPhoto()) + propstatOK.Prop = appendLocationProps(propstatOK.Prop, match.GetEntity().GetLocation()) + if len(propstatOK.Prop) > 0 { response.Propstat = append(response.Propstat, propstatOK) } @@ -269,6 +273,71 @@ func matchToPropResponse(ctx context.Context, davPrefix, publicURL string, match return &response, nil } +// decimal places of the photo props; six for a coordinate resolve about 10 cm +const ( + lensPrecision = 2 + exposurePrecision = 0 + coordinatePrecision = 6 + altitudePrecision = 1 +) + +func appendPhotoProps(props []prop.PropertyXML, photo *searchmsg.Photo) []prop.PropertyXML { + if photo == nil { + return props + } + if photo.GetTakenDateTime() != nil { + props = append(props, prop.Escaped("oc:photo-taken-date-time", photo.GetTakenDateTime().AsTime().Format(time.RFC3339))) + } + props = appendStringProp(props, "oc:photo-camera-make", photo.CameraMake) + props = appendStringProp(props, "oc:photo-camera-model", photo.CameraModel) + props = appendFloat32Prop(props, "oc:photo-fnumber", photo.FNumber, lensPrecision) + props = appendFloat32Prop(props, "oc:photo-focal-length", photo.FocalLength, lensPrecision) + props = appendInt32Prop(props, "oc:photo-iso", photo.Iso) + props = appendInt32Prop(props, "oc:photo-orientation", photo.Orientation) + props = appendFloat32Prop(props, "oc:photo-exposure-numerator", photo.ExposureNumerator, exposurePrecision) + props = appendFloat32Prop(props, "oc:photo-exposure-denominator", photo.ExposureDenominator, exposurePrecision) + return props +} + +func appendLocationProps(props []prop.PropertyXML, location *searchmsg.GeoCoordinates) []prop.PropertyXML { + if location == nil { + return props + } + // no zero check here: 0 is a valid coordinate (equator, prime meridian, sea level) + props = appendFloat64Prop(props, "oc:photo-location-latitude", location.Latitude, coordinatePrecision) + props = appendFloat64Prop(props, "oc:photo-location-longitude", location.Longitude, coordinatePrecision) + props = appendFloat64Prop(props, "oc:photo-location-altitude", location.Altitude, altitudePrecision) + return props +} + +func appendStringProp(props []prop.PropertyXML, name string, value *string) []prop.PropertyXML { + if value == nil || *value == "" { + return props + } + return append(props, prop.Escaped(name, *value)) +} + +func appendFloat32Prop(props []prop.PropertyXML, name string, value *float32, precision int) []prop.PropertyXML { + if value == nil || *value == 0 { + return props + } + return append(props, prop.Escaped(name, strconv.FormatFloat(float64(*value), 'f', precision, 32))) +} + +func appendInt32Prop(props []prop.PropertyXML, name string, value *int32) []prop.PropertyXML { + if value == nil || *value == 0 { + return props + } + return append(props, prop.Escaped(name, strconv.FormatInt(int64(*value), 10))) +} + +func appendFloat64Prop(props []prop.PropertyXML, name string, value *float64, precision int) []prop.PropertyXML { + if value == nil { + return props + } + return append(props, prop.Escaped(name, strconv.FormatFloat(*value, 'f', precision, 64))) +} + type report struct { SearchFiles *reportSearchFiles // FilterFiles TODO add this for tag based search diff --git a/services/webdav/pkg/service/v0/search_test.go b/services/webdav/pkg/service/v0/search_test.go index 5bfa2782d8..274cf8cf86 100644 --- a/services/webdav/pkg/service/v0/search_test.go +++ b/services/webdav/pkg/service/v0/search_test.go @@ -1,10 +1,18 @@ package svc import ( + "context" "testing" + "time" + userv1beta1 "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + "google.golang.org/protobuf/proto" + "google.golang.org/protobuf/types/known/timestamppb" + + searchmsg "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/messages/search/v0" + "github.com/opencloud-eu/opencloud/services/webdav/pkg/thumbnail" ) func TestSearch(t *testing.T) { @@ -33,3 +41,66 @@ var _ = Describe("SpacesSearchRegex", func() { Entry("unrelated path", "/dav/files/123", "", false), ) }) + +var _ = Describe("matchToPropResponse", func() { + propsOf := func(entity *searchmsg.Entity) map[string]string { + entity.Ref = &searchmsg.Reference{ResourceId: &searchmsg.ResourceID{StorageId: "storage", SpaceId: "space"}, Path: "./photo.jpg"} + entity.Id = &searchmsg.ResourceID{StorageId: "storage", SpaceId: "space", OpaqueId: "photo"} + entity.LastModifiedTime = timestamppb.Now() + res, err := matchToPropResponse(context.Background(), "", "https://cloud.example.com", &searchmsg.Match{Entity: entity}, &userv1beta1.User{Id: &userv1beta1.UserId{OpaqueId: "alice"}}, thumbnail.SourceTypes{}) + Expect(err).ToNot(HaveOccurred()) + Expect(res.Propstat).To(HaveLen(1)) + props := map[string]string{} + for _, p := range res.Propstat[0].Prop { + props[p.XMLName.Local] = string(p.InnerXML) + } + return props + } + + It("returns the photo and location metadata", func() { + props := propsOf(&searchmsg.Entity{ + Photo: &searchmsg.Photo{ + TakenDateTime: timestamppb.New(time.Date(2024, 5, 1, 12, 30, 0, 0, time.UTC)), + CameraMake: proto.String("Acme"), + CameraModel: proto.String("X100"), + FNumber: proto.Float32(1.8), + FocalLength: proto.Float32(26), + Iso: proto.Int32(400), + Orientation: proto.Int32(6), + ExposureNumerator: proto.Float32(1), + ExposureDenominator: proto.Float32(250), + }, + Location: &searchmsg.GeoCoordinates{Latitude: proto.Float64(48.137154), Longitude: proto.Float64(11.576124), Altitude: proto.Float64(519.5)}, + }) + Expect(props).To(HaveKeyWithValue("oc:photo-taken-date-time", "2024-05-01T12:30:00Z")) + Expect(props).To(HaveKeyWithValue("oc:photo-camera-make", "Acme")) + Expect(props).To(HaveKeyWithValue("oc:photo-camera-model", "X100")) + Expect(props).To(HaveKeyWithValue("oc:photo-fnumber", "1.80")) + Expect(props).To(HaveKeyWithValue("oc:photo-focal-length", "26.00")) + Expect(props).To(HaveKeyWithValue("oc:photo-iso", "400")) + Expect(props).To(HaveKeyWithValue("oc:photo-orientation", "6")) + Expect(props).To(HaveKeyWithValue("oc:photo-exposure-numerator", "1")) + Expect(props).To(HaveKeyWithValue("oc:photo-exposure-denominator", "250")) + Expect(props).To(HaveKeyWithValue("oc:photo-location-latitude", "48.137154")) + Expect(props).To(HaveKeyWithValue("oc:photo-location-longitude", "11.576124")) + Expect(props).To(HaveKeyWithValue("oc:photo-location-altitude", "519.5")) + }) + + It("keeps a zero coordinate but skips empty photo values", func() { + props := propsOf(&searchmsg.Entity{ + Photo: &searchmsg.Photo{CameraMake: proto.String(""), Iso: proto.Int32(0)}, + Location: &searchmsg.GeoCoordinates{Latitude: proto.Float64(0), Longitude: proto.Float64(0)}, + }) + Expect(props).To(HaveKeyWithValue("oc:photo-location-latitude", "0.000000")) + Expect(props).To(HaveKeyWithValue("oc:photo-location-longitude", "0.000000")) + Expect(props).ToNot(HaveKey("oc:photo-location-altitude")) + Expect(props).ToNot(HaveKey("oc:photo-camera-make")) + Expect(props).ToNot(HaveKey("oc:photo-iso")) + }) + + It("adds no photo props to a match without them", func() { + for name := range propsOf(&searchmsg.Entity{}) { + Expect(name).ToNot(HavePrefix("oc:photo-")) + } + }) +})