mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-12-05 05:43:36 -05:00
At most 16 aggregations and 3 bucket levels per request and 5 requests per query. The bleve collector stops at the bucket limit instead of checking after the walk, and the limit also holds for the result merged across spaces.
106 lines
5.5 KiB
Go
106 lines
5.5 KiB
Go
package aggregation_test
|
|
|
|
import (
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
|
|
searchsvc "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/services/search/v0"
|
|
"github.com/opencloud-eu/opencloud/services/search/pkg/aggregation"
|
|
"github.com/opencloud-eu/opencloud/services/search/pkg/mapping"
|
|
)
|
|
|
|
// fieldTypes stands in for the index mapping.
|
|
func fieldTypes(field string) string {
|
|
return map[string]string{
|
|
"audio.artist": mapping.TypeKeyword,
|
|
"audio.album": mapping.TypeKeyword,
|
|
"audio.hasDrm": mapping.TypeBool,
|
|
"audio.year": mapping.TypeNumeric,
|
|
"photo.takenDateTime": mapping.TypeDatetime,
|
|
"Content": mapping.TypeFulltext,
|
|
"Path": mapping.TypePath,
|
|
"audio": mapping.TypeObject,
|
|
"location": mapping.TypeGeopoint,
|
|
}[field]
|
|
}
|
|
|
|
var _ = Describe("ValidateOptions", func() {
|
|
numeric := &searchsvc.BucketDefinition{Ranges: []*searchsvc.BucketRange{{From: "1980", To: "1990"}}}
|
|
dates := &searchsvc.BucketDefinition{Ranges: []*searchsvc.BucketRange{{From: "2018-08-11T00:00:00Z"}}}
|
|
sum := &searchsvc.MetricDefinition{Kind: searchsvc.MetricKind_METRIC_KIND_SUM}
|
|
|
|
DescribeTable("accepts an aggregation that fits its field",
|
|
func(opt *searchsvc.AggregationOption) {
|
|
Expect(aggregation.ValidateOptions([]*searchsvc.AggregationOption{opt}, fieldTypes)).To(Succeed())
|
|
},
|
|
Entry("terms on a keyword", &searchsvc.AggregationOption{Field: "audio.artist"}),
|
|
Entry("terms on a bool", &searchsvc.AggregationOption{Field: "audio.hasDrm"}),
|
|
Entry("terms on a number", &searchsvc.AggregationOption{Field: "audio.year"}),
|
|
Entry("numeric ranges on a number", &searchsvc.AggregationOption{Field: "audio.year", BucketDefinition: numeric}),
|
|
Entry("date ranges on a date", &searchsvc.AggregationOption{Field: "photo.takenDateTime", BucketDefinition: dates}),
|
|
Entry("a metric on a number", &searchsvc.AggregationOption{Field: "audio.year", MetricDefinition: sum}),
|
|
Entry("a tree of them", &searchsvc.AggregationOption{Field: "audio.artist", SubAggregations: []*searchsvc.AggregationOption{
|
|
{Field: "audio.year", BucketDefinition: numeric, SubAggregations: []*searchsvc.AggregationOption{
|
|
{Field: "audio.album"}, {Field: "audio.year", MetricDefinition: sum},
|
|
}},
|
|
}}),
|
|
)
|
|
|
|
DescribeTable("rejects an aggregation the index cannot answer",
|
|
func(opt *searchsvc.AggregationOption) {
|
|
Expect(aggregation.ValidateOptions([]*searchsvc.AggregationOption{opt}, fieldTypes)).ToNot(Succeed())
|
|
},
|
|
Entry("an unknown field", &searchsvc.AggregationOption{Field: "audio.nonexistent"}),
|
|
Entry("terms on a date", &searchsvc.AggregationOption{Field: "photo.takenDateTime"}),
|
|
Entry("terms on full text", &searchsvc.AggregationOption{Field: "Content"}),
|
|
Entry("terms on a path", &searchsvc.AggregationOption{Field: "Path"}),
|
|
Entry("terms on a facet object", &searchsvc.AggregationOption{Field: "audio"}),
|
|
Entry("terms on a geopoint", &searchsvc.AggregationOption{Field: "location"}),
|
|
Entry("a metric on a keyword", &searchsvc.AggregationOption{Field: "audio.artist", MetricDefinition: sum}),
|
|
Entry("a metric on a date", &searchsvc.AggregationOption{Field: "photo.takenDateTime", MetricDefinition: sum}),
|
|
Entry("a metric with sub-aggregations", &searchsvc.AggregationOption{Field: "audio.year", MetricDefinition: sum,
|
|
SubAggregations: []*searchsvc.AggregationOption{{Field: "audio.artist"}}}),
|
|
Entry("numeric ranges on a keyword", &searchsvc.AggregationOption{Field: "audio.artist", BucketDefinition: numeric}),
|
|
Entry("numeric ranges on a date", &searchsvc.AggregationOption{Field: "photo.takenDateTime", BucketDefinition: numeric}),
|
|
Entry("date ranges on a number", &searchsvc.AggregationOption{Field: "audio.year", BucketDefinition: dates}),
|
|
Entry("an invalid aggregation two levels down", &searchsvc.AggregationOption{Field: "audio.artist", SubAggregations: []*searchsvc.AggregationOption{
|
|
{Field: "audio.album", SubAggregations: []*searchsvc.AggregationOption{{Field: "photo.takenDateTime"}}},
|
|
}}),
|
|
)
|
|
|
|
chain := func(levels int, leaf *searchsvc.AggregationOption) *searchsvc.AggregationOption {
|
|
opt := leaf
|
|
for range levels {
|
|
var subs []*searchsvc.AggregationOption
|
|
if opt != nil {
|
|
subs = append(subs, opt)
|
|
}
|
|
opt = &searchsvc.AggregationOption{Field: "audio.artist", SubAggregations: subs}
|
|
}
|
|
return opt
|
|
}
|
|
flat := func(n int) []*searchsvc.AggregationOption {
|
|
opts := make([]*searchsvc.AggregationOption, n)
|
|
for i := range opts {
|
|
opts[i] = &searchsvc.AggregationOption{Field: "audio.artist"}
|
|
}
|
|
return opts
|
|
}
|
|
|
|
It("holds the nesting to MaxDepth, a metric adds no level", func() {
|
|
metric := &searchsvc.AggregationOption{Field: "audio.year", MetricDefinition: sum}
|
|
Expect(aggregation.MaxDepth).To(Equal(3))
|
|
Expect(aggregation.ValidateOptions([]*searchsvc.AggregationOption{chain(3, nil)}, fieldTypes)).To(Succeed())
|
|
Expect(aggregation.ValidateOptions([]*searchsvc.AggregationOption{chain(3, metric)}, fieldTypes)).To(Succeed())
|
|
Expect(aggregation.ValidateOptions([]*searchsvc.AggregationOption{chain(4, nil)}, fieldTypes)).To(MatchError(ContainSubstring("3 levels")))
|
|
})
|
|
|
|
It("holds a request to MaxAggregations, all levels counted", func() {
|
|
Expect(aggregation.MaxAggregations).To(Equal(16))
|
|
Expect(aggregation.ValidateOptions(flat(16), fieldTypes)).To(Succeed())
|
|
Expect(aggregation.ValidateOptions(flat(17), fieldTypes)).To(MatchError(ContainSubstring("16 aggregations")))
|
|
nested := append(flat(14), chain(3, nil))
|
|
Expect(aggregation.ValidateOptions(nested, fieldTypes)).To(MatchError(ContainSubstring("16 aggregations")))
|
|
})
|
|
})
|