Files
opencloud/services/search/pkg/aggregation/aggregation.go
T
Dominik Schmidt b928dc8342 feat(search): geohash aggregation
A geohash_grid aggregation over a geopoint field, requested with
@libre.graph.geohashDefinition {precision}. OpenSearch answers it with
geohash_grid on the geopoint field, bleve folds the doc values of the
geohash sibling field. size limits the buckets to the top cells by count.
2026-12-03 14:45:45 +01:00

85 lines
2.2 KiB
Go

// Package aggregation holds what the engines and the service layer share
// about aggregations: the kind of an option, range bounds, metric
// accumulators and the cross-space merge.
package aggregation
import (
"fmt"
"math"
"strconv"
searchsvc "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/services/search/v0"
)
// Kind is what an aggregation option asks for.
type Kind int
const (
KindTerms Kind = iota
KindRange
KindMetric
KindGeohash
)
// MaxGeohashPrecision is the longest geohash a cell can have.
const MaxGeohashPrecision = 12
// KindOf tells the kind from the definitions an option carries.
func KindOf(opt *searchsvc.AggregationOption) Kind {
switch {
case opt.GetMetricDefinition() != nil:
return KindMetric
case opt.GetGeohashDefinition() != nil:
return KindGeohash
case len(opt.GetBucketDefinition().GetRanges()) > 0:
return KindRange
}
return KindTerms
}
// MaxBuckets is how many buckets the aggregations of one request may have,
// all levels counted, in one space and in the merged result: the
// search.max_buckets default of OpenSearch, held on every engine.
const MaxBuckets = math.MaxUint16
// MaxAggregations is how many aggregations one request may carry, all levels
// counted. MaxDepth is how deep buckets may nest; a metric is a leaf and adds
// no level.
const (
MaxAggregations = 16
MaxDepth = 3
)
// ErrTooManyBuckets is what an engine answers beyond MaxBuckets; the service
// layer turns it into a bad request.
var ErrTooManyBuckets = fmt.Errorf("the aggregations have more than %d buckets, narrow the query or the aggregations", MaxBuckets)
// CheckBuckets holds results to MaxBuckets.
func CheckBuckets(results []*searchsvc.AggregationResult) error {
if countBuckets(results) > MaxBuckets {
return ErrTooManyBuckets
}
return nil
}
func countBuckets(results []*searchsvc.AggregationResult) int {
n := 0
for _, r := range results {
n += len(r.GetBuckets())
for _, b := range r.GetBuckets() {
n += countBuckets(b.GetSubAggregations())
}
}
return n
}
// NumberKey and BoolKey spell the bucket key of a numeric and a bool value,
// the same on every engine.
func NumberKey(v float64) string {
return strconv.FormatFloat(v, 'f', -1, 64)
}
func BoolKey(v bool) string {
return strconv.FormatBool(v)
}