这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,11 @@ This endpoint returns the latest cluster status.
```json
{
"aggregations": {
"id_range": {
"high": 28,
"low": 1,
"medium": 13
},
"text_terms": {
"also": 56,
"external": 57,
Expand Down
20 changes: 20 additions & 0 deletions examples/search_with_aggregation.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,26 @@
"max_length": 20,
"size": 10
}
},
"id_range": {
"type": "range",
"options": {
"field": "id",
"ranges": {
"low": {
"from": 0,
"to": 500
},
"medium": {
"from": 500,
"to": 1000
},
"high": {
"from": 1000,
"to": 1500
}
}
}
}
}
}
3 changes: 3 additions & 0 deletions search/aggregations/aggregations.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@ type AggregationType int
const (
AggregationTypeUnknown AggregationType = iota
AggregationTypeTerms
AggregationTypeRange
)

// Maps for AggregationType.
var (
AggregationType_name = map[AggregationType]string{
AggregationTypeUnknown: "unknown",
AggregationTypeTerms: "terms",
AggregationTypeRange: "range",
}
AggregationType_value = map[string]AggregationType{
"unknown": AggregationTypeUnknown,
"terms": AggregationTypeTerms,
"range": AggregationTypeRange,
}
)

Expand Down
68 changes: 68 additions & 0 deletions search/aggregations/range.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package aggregations

import (
"fmt"

"github.com/blugelabs/bluge/search"
"github.com/blugelabs/bluge/search/aggregations"
)

// Create new RangeAggregation with given options.
// Options example:
// {
// "field": "id",
// "ranges": {
// "low": {
// "from": 0,
// "to": 500
// },
// "medium": {
// "from": 500,
// "to": 1000
// },
// "high": {
// "from": 1000,
// "to": 1500
// }
// }
// }
func NewRangeAggregationWithOptions(opts map[string]interface{}) (*aggregations.RangeAggregation, error) {
fieldValue, ok := opts["field"]
if !ok {
return nil, fmt.Errorf("field option does not exist")
}
field, ok := fieldValue.(string)
if !ok {
return nil, fmt.Errorf("field option is unexpected: %v", fieldValue)
}
if len(field) == 0 {
return nil, fmt.Errorf("field option is empty")
}

rangesAgg := aggregations.Ranges(search.Field(field))

ranges, ok := opts["ranges"].(map[string]interface{})
if !ok {
return nil, fmt.Errorf("ranges option does not exist")
}
for name, rangeValue := range ranges {
rangeMap, ok := rangeValue.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("range %v option is unexpected: %v", name, rangeValue)
}

from, ok := rangeMap["from"].(float64)
if !ok {
return nil, fmt.Errorf("range %v from option is unexpected: %v", name, rangeMap["from"])
}

to, ok := rangeMap["to"].(float64)
if !ok {
return nil, fmt.Errorf("range %v to option is unexpected: %v", name, rangeMap["to"])
}

rangesAgg.AddRange(aggregations.NamedRange(name, from, to))
}

return rangesAgg, nil
}
27 changes: 27 additions & 0 deletions server/index_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,33 @@ func (s *IndexService) Search(req *proto.SearchRequest) (*proto.SearchResponse,
}

blugeRequest.AddAggregation(name, termsAgg)
case phalanxaggregations.AggregationType_name[phalanxaggregations.AggregationTypeRange]:
opts := make(map[string]interface{})
if err := json.Unmarshal(agg.Options, &opts); err != nil {
s.logger.Error(err.Error(), zap.String("type", agg.Type), zap.String("options", string(agg.Options)))
responsesChan <- searchResponse{
nodeName: nodeName,
indexName: request.IndexName,
shardNames: request.ShardNames,
resp: nil,
err: err,
}
return err
}
rangeAgg, err := phalanxaggregations.NewRangeAggregationWithOptions(opts)
if err != nil {
s.logger.Error(err.Error(), zap.String("type", agg.Type), zap.String("options", string(agg.Options)))
responsesChan <- searchResponse{
nodeName: nodeName,
indexName: request.IndexName,
shardNames: request.ShardNames,
resp: nil,
err: err,
}
return err
}

blugeRequest.AddAggregation(name, rangeAgg)
}
}

Expand Down