-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfacet.go
199 lines (161 loc) · 3.56 KB
/
facet.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package solr
// Faceter is an abstraction of a facet
// e.g. terms, query, stats, range, heatmap etc.
type Faceter interface {
// BuildFacet builds the facet
BuildFacet() M
// Name returns the name of the facet
Name() string
}
// TermsFacet is a terms facet
type TermsFacet struct {
// name is the facet name
name string
// terms facet params
field string
offset int
limit int
sort string
// overRequest int
// refine bool
// overRefine int
minCount int
// missing bool
// numBuckets bool
// allBuckets bool
// prefix string
// method string
// prelimSort string
facet M
domain M
}
var _ Faceter = (*TermsFacet)(nil)
// NewTermsFacet returns a new TermsFacet
func NewTermsFacet(name string) *TermsFacet {
return &TermsFacet{name: name, facet: M{}, domain: M{}}
}
// BuildFacet builds the facet
func (f *TermsFacet) BuildFacet() M {
m := M{"type": "terms"}
if f.field != "" {
m["field"] = f.field
}
if f.offset > 0 {
m["offset"] = f.offset
}
if f.limit > 0 {
m["limit"] = f.limit
}
if f.sort != "" {
m["sort"] = f.sort
}
if f.minCount > 0 {
m["mincount"] = f.minCount
}
if len(f.facet) > 0 {
m["facet"] = f.facet
}
if len(f.domain) > 0 {
m["domain"] = f.domain
}
return m
}
// Field sets the field param
func (f *TermsFacet) Field(field string) *TermsFacet {
f.field = field
return f
}
// Offset sets the offset param
func (f *TermsFacet) Offset(offset int) *TermsFacet {
f.offset = offset
return f
}
// Limit sets the limit param
func (f *TermsFacet) Limit(limit int) *TermsFacet {
f.limit = limit
return f
}
// Sort sets the sort param
func (f *TermsFacet) Sort(sort string) *TermsFacet {
f.sort = sort
return f
}
// AddFacets adds nested facets
func (f *TermsFacet) AddFacets(facets ...Faceter) *TermsFacet {
for _, facet := range facets {
f.facet[facet.Name()] = facet.BuildFacet()
}
return f
}
// AddToFacet adds a key-value pair to the facet map
func (f *TermsFacet) AddToFacet(key string, value interface{}) *TermsFacet {
f.facet[key] = value
return f
}
// AddToDomain adds a key-value pair to the domain map
func (f *TermsFacet) AddToDomain(key string, value interface{}) *TermsFacet {
f.domain[key] = value
return f
}
// Name returns the name of the facet
func (f *TermsFacet) Name() string {
return f.name
}
// MinCount sets the mincount param
func (f *TermsFacet) MinCount(minCount int) *TermsFacet {
f.minCount = minCount
return f
}
// QueryFacet is query facet
type QueryFacet struct {
name string
q string
facet M
}
var _ Faceter = (*QueryFacet)(nil)
// NewQueryFacet retunrs a new QueryFacet
func NewQueryFacet(name string) *QueryFacet {
return &QueryFacet{name: name, facet: M{}}
}
// BuildFacet builds the facet
func (f *QueryFacet) BuildFacet() M {
m := M{"type": "query"}
if f.q != "" {
m["q"] = f.q
}
if len(f.facet) > 0 {
m["facet"] = f.facet
}
return m
}
// Name returns the facet name
func (f *QueryFacet) Name() string {
return f.name
}
// Query sets the q param
func (f *QueryFacet) Query(q string) *QueryFacet {
f.q = q
return f
}
// AddFacet adds a nested facet
func (f *QueryFacet) AddFacet(facet Faceter) *QueryFacet {
f.facet[facet.Name()] = facet.BuildFacet()
return f
}
// AddToFacet adds a key-value pair to the facet map
func (f *QueryFacet) AddToFacet(key string, value interface{}) *QueryFacet {
f.facet[key] = value
return f
}
// // RangeFacet is a range facet
// type RangeFacet struct {
// field string
// start int
// end int
// gap int
// hardend bool
// other string
// include string
// ranges string
// facet M
// }