forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfacet_builder.go
101 lines (78 loc) · 1.96 KB
/
facet_builder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package ravendb
import "strings"
func isRqlKeyword(s string) bool {
s = strings.ToLower(s)
switch s {
case "as", "select", "where", "load", "group", "order", "include", "update":
return true
}
return false
}
type FacetBuilder struct {
_range *GenericRangeFacet
_default *Facet
}
func NewFacetBuilder() *FacetBuilder {
return &FacetBuilder{}
}
func (b *FacetBuilder) ByRanges(rng *RangeBuilder, ranges ...*RangeBuilder) *FacetBuilder {
if rng == nil {
//throw new IllegalArgumentError("Range cannot be null")
panic("Range cannot be null")
}
if b._range == nil {
b._range = NewGenericRangeFacet(nil)
}
b._range.addRange(rng)
for _, rng := range ranges {
b._range.addRange(rng)
}
return b
}
func (b *FacetBuilder) ByField(fieldName string) *FacetBuilder {
if b._default == nil {
b._default = NewFacet()
}
if isRqlKeyword(fieldName) {
fieldName = "'" + fieldName + "'"
}
b._default.FieldName = fieldName
return b
}
func (b *FacetBuilder) AllResults() *FacetBuilder {
if b._default == nil {
b._default = NewFacet()
}
b._default.FieldName = ""
return b
}
func (b *FacetBuilder) WithOptions(options *FacetOptions) *FacetBuilder {
b.GetFacet().SetOptions(options)
return b
}
func (b *FacetBuilder) WithDisplayName(displayName string) *FacetBuilder {
b.GetFacet().SetDisplayFieldName(displayName)
return b
}
func (b *FacetBuilder) SumOn(path string) *FacetBuilder {
b.GetFacet().GetAggregations()[FacetAggregationSum] = path
return b
}
func (b *FacetBuilder) MinOn(path string) *FacetBuilder {
b.GetFacet().GetAggregations()[FacetAggregationMin] = path
return b
}
func (b *FacetBuilder) MaxOn(path string) *FacetBuilder {
b.GetFacet().GetAggregations()[FacetAggregationMax] = path
return b
}
func (b *FacetBuilder) AverageOn(path string) *FacetBuilder {
b.GetFacet().GetAggregations()[FacetAggregationAverage] = path
return b
}
func (b *FacetBuilder) GetFacet() FacetBase {
if b._default != nil {
return b._default
}
return b._range
}