forked from mattbaird/elastigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
200 lines (177 loc) · 4.5 KB
/
filter.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
200
// Copyright 2013 Matthew Baird
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package search
import (
"encoding/json"
"fmt"
. "github.com/araddon/gou"
)
var (
_ = DEBUG
)
// A bool (and/or) clause
type BoolClause string
// Filter clause is either a boolClause or FilterOp
type FilterClause interface {
String() string
}
// A wrapper to allow for custom serialization
type FilterWrap struct {
boolClause string
filters []interface{}
}
func NewFilterWrap() *FilterWrap {
return &FilterWrap{filters: make([]interface{}, 0), boolClause: "and"}
}
func (f *FilterWrap) String() string {
return fmt.Sprintf(`fopv: %d:%v`, len(f.filters), f.filters)
}
// Custom marshalling to support the query dsl
func (f *FilterWrap) addFilters(fl []interface{}) {
if len(fl) > 1 {
fc := fl[0]
switch fc.(type) {
case BoolClause, string:
f.boolClause = fc.(string)
fl = fl[1:]
}
}
f.filters = append(f.filters, fl...)
}
// Custom marshalling to support the query dsl
func (f *FilterWrap) MarshalJSON() ([]byte, error) {
var root interface{}
if len(f.filters) > 1 {
root = map[string]interface{}{f.boolClause: f.filters}
} else if len(f.filters) == 1 {
root = f.filters[0]
}
return json.Marshal(root)
}
/*
"filter": {
"range": {
"@timestamp": {
"from": "2012-12-29T16:52:48+00:00",
"to": "2012-12-29T17:52:48+00:00"
}
}
}
"filter": {
"missing": {
"field": "repository.name"
}
}
"filter" : {
"terms" : {
"user" : ["kimchy", "elasticsearch"],
"execution" : "bool",
"_cache": true
}
}
"filter" : {
"term" : { "user" : "kimchy"}
}
"filter" : {
"and" : [
{
"range" : {
"postDate" : {
"from" : "2010-03-01",
"to" : "2010-04-01"
}
}
},
{
"prefix" : { "name.second" : "ba" }
}
]
}
*/
// Filter Operation
//
// Filter().Term("user","kimchy")
//
// // we use variadics to allow n arguments, first is the "field" rest are values
// Filter().Terms("user", "kimchy", "elasticsearch")
//
// Filter().Exists("repository.name")
//
func Filter() *FilterOp {
return &FilterOp{}
}
type FilterOp struct {
curField string
TermsMap map[string][]interface{} `json:"terms,omitempty"`
Range map[string]map[string]string `json:"range,omitempty"`
Exist map[string]string `json:"exists,omitempty"`
MisssingVal map[string]string `json:"missing,omitempty"`
}
// A range is a special type of Filter operation
//
// Range().Exists("repository.name")
func Range() *FilterOp {
return &FilterOp{Range: make(map[string]map[string]string)}
}
func (f *FilterOp) Field(fld string) *FilterOp {
f.curField = fld
if _, ok := f.Range[fld]; !ok {
m := make(map[string]string)
f.Range[fld] = m
}
return f
}
// Filter Terms
//
// Filter().Terms("user","kimchy")
//
// // we use variadics to allow n arguments, first is the "field" rest are values
// Filter().Terms("user", "kimchy", "elasticsearch")
//
func (f *FilterOp) Terms(field string, values ...interface{}) *FilterOp {
if len(f.TermsMap) == 0 {
f.TermsMap = make(map[string][]interface{})
}
for _, val := range values {
f.TermsMap[field] = append(f.TermsMap[field], val)
}
return f
}
func (f *FilterOp) From(from string) *FilterOp {
f.Range[f.curField]["from"] = from
return f
}
func (f *FilterOp) To(to string) *FilterOp {
f.Range[f.curField]["to"] = to
return f
}
func (f *FilterOp) Exists(name string) *FilterOp {
f.Exist = map[string]string{"field": name}
return f
}
func (f *FilterOp) Missing(name string) *FilterOp {
f.MisssingVal = map[string]string{"field": name}
return f
}
// Add another Filterop, "combines" two filter ops into one
func (f *FilterOp) Add(fop *FilterOp) *FilterOp {
// TODO, this is invalid, refactor
if len(fop.Exist) > 0 {
f.Exist = fop.Exist
}
if len(fop.MisssingVal) > 0 {
f.MisssingVal = fop.MisssingVal
}
if len(fop.Range) > 0 {
f.Range = fop.Range
}
return f
}