github.com/weaviate/weaviate@v1.24.6/entities/aggregation/params.go (about)

     1  //                           _       _
     2  // __      _____  __ ___   ___  __ _| |_ ___
     3  // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
     4  //  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
     5  //   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
     6  //
     7  //  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
     8  //
     9  //  CONTACT: hello@weaviate.io
    10  //
    11  
    12  package aggregation
    13  
    14  import (
    15  	"fmt"
    16  
    17  	"github.com/weaviate/weaviate/entities/filters"
    18  	"github.com/weaviate/weaviate/entities/schema"
    19  	"github.com/weaviate/weaviate/entities/searchparams"
    20  )
    21  
    22  type Params struct {
    23  	Filters          *filters.LocalFilter       `json:"filters"`
    24  	ClassName        schema.ClassName           `json:"className"`
    25  	Properties       []ParamProperty            `json:"properties"`
    26  	GroupBy          *filters.Path              `json:"groupBy"`
    27  	IncludeMetaCount bool                       `json:"includeMetaCount"`
    28  	Limit            *int                       `json:"limit"`
    29  	ObjectLimit      *int                       `json:"objectLimit"`
    30  	SearchVector     []float32                  `json:"searchVector"`
    31  	TargetVector     string                     `json:"targetVector"`
    32  	Certainty        float64                    `json:"certainty"`
    33  	Tenant           string                     `json:"tenant"`
    34  	ModuleParams     map[string]interface{}     `json:"moduleParams"`
    35  	NearVector       *searchparams.NearVector   `json:"nearVector"`
    36  	NearObject       *searchparams.NearObject   `json:"nearObject"`
    37  	Hybrid           *searchparams.HybridSearch `json:"hybrid"`
    38  }
    39  
    40  type ParamProperty struct {
    41  	Name        schema.PropertyName `json:"name"`
    42  	Aggregators []Aggregator        `json:"aggregators"`
    43  }
    44  
    45  type Aggregator struct {
    46  	Type  string `json:"type"`
    47  	Limit *int   `json:"limit"` // used on TopOccurrence Agg
    48  }
    49  
    50  func (a Aggregator) String() string {
    51  	return a.Type
    52  }
    53  
    54  // Aggregators used in every prop
    55  var (
    56  	CountAggregator = Aggregator{Type: "count"}
    57  	TypeAggregator  = Aggregator{Type: "type"}
    58  )
    59  
    60  // Aggregators used in numerical props
    61  var (
    62  	SumAggregator     = Aggregator{Type: "sum"}
    63  	MeanAggregator    = Aggregator{Type: "mean"}
    64  	ModeAggregator    = Aggregator{Type: "mode"}
    65  	MedianAggregator  = Aggregator{Type: "median"}
    66  	MaximumAggregator = Aggregator{Type: "maximum"}
    67  	MinimumAggregator = Aggregator{Type: "minimum"}
    68  )
    69  
    70  // Aggregators used in boolean props
    71  var (
    72  	TotalTrueAggregator       = Aggregator{Type: "totalTrue"}
    73  	PercentageTrueAggregator  = Aggregator{Type: "percentageTrue"}
    74  	TotalFalseAggregator      = Aggregator{Type: "totalFalse"}
    75  	PercentageFalseAggregator = Aggregator{Type: "percentageFalse"}
    76  )
    77  
    78  const TopOccurrencesType = "topOccurrences"
    79  
    80  // NewTopOccurrencesAggregator creates a TopOccurrencesAggregator, we cannot
    81  // use a singleton for this as the desired limit can be different each time
    82  func NewTopOccurrencesAggregator(limit *int) Aggregator {
    83  	return Aggregator{Type: TopOccurrencesType, Limit: limit}
    84  }
    85  
    86  // Aggregators used in ref props
    87  var (
    88  	PointingToAggregator = Aggregator{Type: "pointingTo"}
    89  )
    90  
    91  func ParseAggregatorProp(name string) (Aggregator, error) {
    92  	switch name {
    93  	// common
    94  	case CountAggregator.String():
    95  		return CountAggregator, nil
    96  	case TypeAggregator.String():
    97  		return TypeAggregator, nil
    98  
    99  	// numerical
   100  	case MeanAggregator.String():
   101  		return MeanAggregator, nil
   102  	case MedianAggregator.String():
   103  		return MedianAggregator, nil
   104  	case ModeAggregator.String():
   105  		return ModeAggregator, nil
   106  	case MaximumAggregator.String():
   107  		return MaximumAggregator, nil
   108  	case MinimumAggregator.String():
   109  		return MinimumAggregator, nil
   110  	case SumAggregator.String():
   111  		return SumAggregator, nil
   112  
   113  	// boolean
   114  	case TotalTrueAggregator.String():
   115  		return TotalTrueAggregator, nil
   116  	case TotalFalseAggregator.String():
   117  		return TotalFalseAggregator, nil
   118  	case PercentageTrueAggregator.String():
   119  		return PercentageTrueAggregator, nil
   120  	case PercentageFalseAggregator.String():
   121  		return PercentageFalseAggregator, nil
   122  
   123  	// string/text
   124  	case TopOccurrencesType:
   125  		return NewTopOccurrencesAggregator(ptInt(5)), nil // default to limit 5, can be overwritten
   126  
   127  	// ref
   128  	case PointingToAggregator.String():
   129  		return PointingToAggregator, nil
   130  
   131  	default:
   132  		return Aggregator{}, fmt.Errorf("unrecognized aggregator prop '%s'", name)
   133  	}
   134  }
   135  
   136  func ptInt(in int) *int {
   137  	return &in
   138  }