forked from olivere/elastic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_facets_range.go
158 lines (135 loc) · 3.59 KB
/
search_facets_range.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright 2012-2015 Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.
package elastic
import (
"time"
)
// Range facet allows to specify a set of ranges and get both the
// number of docs (count) that fall within each range,
// and aggregated data either based on the field, or using another field.
// See: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-facets-range-facet.html
type RangeFacet struct {
facetFilter Filter
global *bool
nested string
mode string
keyField string
valueField string
entries []rangeFacetEntry
}
type rangeFacetEntry struct {
From interface{}
To interface{}
}
func NewRangeFacet() RangeFacet {
return RangeFacet{
entries: make([]rangeFacetEntry, 0),
}
}
func (f RangeFacet) FacetFilter(filter Facet) RangeFacet {
f.facetFilter = filter
return f
}
func (f RangeFacet) Global(global bool) RangeFacet {
f.global = &global
return f
}
func (f RangeFacet) Nested(nested string) RangeFacet {
f.nested = nested
return f
}
func (f RangeFacet) Mode(mode string) RangeFacet {
f.mode = mode
return f
}
func (f RangeFacet) Field(field string) RangeFacet {
f.keyField = field
f.valueField = field
return f
}
func (f RangeFacet) KeyField(keyField string) RangeFacet {
f.keyField = keyField
return f
}
func (f RangeFacet) ValueField(valueField string) RangeFacet {
f.valueField = valueField
return f
}
func (f RangeFacet) AddRange(from, to interface{}) RangeFacet {
f.entries = append(f.entries, rangeFacetEntry{From: from, To: to})
return f
}
func (f RangeFacet) AddUnboundedTo(from interface{}) RangeFacet {
f.entries = append(f.entries, rangeFacetEntry{From: from, To: nil})
return f
}
func (f RangeFacet) AddUnboundedFrom(to interface{}) RangeFacet {
f.entries = append(f.entries, rangeFacetEntry{From: nil, To: to})
return f
}
func (f RangeFacet) Lt(to interface{}) RangeFacet {
f.entries = append(f.entries, rangeFacetEntry{From: nil, To: to})
return f
}
func (f RangeFacet) Between(from, to interface{}) RangeFacet {
f.entries = append(f.entries, rangeFacetEntry{From: from, To: to})
return f
}
func (f RangeFacet) Gt(from interface{}) RangeFacet {
f.entries = append(f.entries, rangeFacetEntry{From: from, To: nil})
return f
}
func (f RangeFacet) addFilterFacetAndGlobal(source map[string]interface{}) {
if f.facetFilter != nil {
source["facet_filter"] = f.facetFilter.Source()
}
if f.nested != "" {
source["nested"] = f.nested
}
if f.global != nil {
source["global"] = *f.global
}
if f.mode != "" {
source["mode"] = f.mode
}
}
func (f RangeFacet) Source() interface{} {
source := make(map[string]interface{})
f.addFilterFacetAndGlobal(source)
opts := make(map[string]interface{})
source["range"] = opts
if f.valueField != "" && f.keyField != f.valueField {
opts["key_field"] = f.keyField
opts["value_field"] = f.valueField
} else {
opts["field"] = f.keyField
}
ranges := make([]interface{}, 0)
for _, ent := range f.entries {
r := make(map[string]interface{})
if ent.From != nil {
switch from := ent.From.(type) {
case int, int16, int32, int64, float32, float64:
r["from"] = from
case time.Time:
r["from"] = from.Format(time.RFC3339)
case string:
r["from"] = from
}
}
if ent.To != nil {
switch to := ent.To.(type) {
case int, int16, int32, int64, float32, float64:
r["to"] = to
case time.Time:
r["to"] = to.Format(time.RFC3339)
case string:
r["to"] = to
}
}
ranges = append(ranges, r)
}
opts["ranges"] = ranges
return source
}