/
query.go
252 lines (206 loc) · 6.26 KB
/
query.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package elasticsearch
import (
"encoding/json"
"fmt"
)
// Query DSL - Queries
// Query represents a query in the elasticsearch DSL.
type Query interface {
json.Marshaler
}
// Filter represents a filter in the elasticsearch DSL.
type Filter interface {
json.Marshaler
}
// Function is a function definition for use with a FunctionScoreQuery.
type Function interface{}
// BoostField creates a string which represents a field name with a boost value.
func BoostField(field string, boost float64) string {
return fmt.Sprintf("%s^%f", field, boost)
}
// MatchAllQuery provides a query that matches all
// documents in the index.
type MatchAllQuery struct {
}
func (m MatchAllQuery) MarshalJSON() ([]byte, error) {
return marshalNamedObject("match_all", struct{}{})
}
// MatchQuery provides a query that matches against
// a complete field.
type MatchQuery struct {
Field string
Query string
Type string
}
func (m MatchQuery) MarshalJSON() ([]byte, error) {
params := map[string]interface{}{"query": m.Query}
if m.Type != "" {
params["type"] = m.Type
}
return marshalNamedObject("match", map[string]interface{}{m.Field: params})
}
// MultiMatchQuery provides a query that matches on a number of fields.
type MultiMatchQuery struct {
Query string
Fields []string
}
func (m MultiMatchQuery) MarshalJSON() ([]byte, error) {
return marshalNamedObject("multi_match", map[string]interface{}{
"query": m.Query,
"fields": m.Fields,
})
}
// FilteredQuery provides a query that includes a filter.
type FilteredQuery struct {
Query Query
Filter Filter
}
func (f FilteredQuery) MarshalJSON() ([]byte, error) {
return marshalNamedObject("filtered", map[string]interface{}{
"query": f.Query,
"filter": f.Filter,
})
}
// FunctionScoreQuery provides a query that adjusts the scoring of a
// query by applying functions to it.
type FunctionScoreQuery struct {
Query Query
Functions []Function
}
func (f FunctionScoreQuery) MarshalJSON() ([]byte, error) {
return marshalNamedObject("function_score", map[string]interface{}{
"query": f.Query,
"functions": f.Functions,
})
}
// TermQuery provides a query that matches a term in a field.
type TermQuery struct {
Field string
Value string
}
func (t TermQuery) MarshalJSON() ([]byte, error) {
return marshalNamedObject("term", map[string]interface{}{
t.Field: t.Value,
})
}
// DecayFunction provides a function that boosts depending on
// the difference in values of a certain field. See
// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#_decay_functions
// for details.
type DecayFunction struct {
Function string
Field string
Scale string
}
func (f DecayFunction) MarshalJSON() ([]byte, error) {
return marshalNamedObject(f.Function, map[string]interface{}{
f.Field: map[string]interface{}{
"scale": f.Scale,
},
})
}
// BoostFactorFunction provides a function that boosts results by the specified amount.
type BoostFactorFunction struct {
Filter Filter `json:"filter,omitempty"`
BoostFactor float64 `json:"boost_factor"`
}
// FieldValueFactorFunction boosts the results by the value of a field in the document.
type FieldValueFactorFunction struct {
Field string `json:"field"`
Factor float64 `json:"factor,omitempty"`
Modifier string `json:"modifier,omitempty"`
}
func (f FieldValueFactorFunction) MarshalJSON() ([]byte, error) {
type ffvf FieldValueFactorFunction
return marshalNamedObject("field_value_factor", ffvf(f))
}
// AndFilter provides a filter that matches if all of the internal
// filters match.
type AndFilter []Filter
func (a AndFilter) MarshalJSON() ([]byte, error) {
return marshalNamedObject("and", map[string]interface{}{
"filters": []Filter(a),
})
}
// OrFilter provides a filter that matches if any of the internal
// filters match.
type OrFilter []Filter
func (o OrFilter) MarshalJSON() ([]byte, error) {
return marshalNamedObject("or", map[string]interface{}{
"filters": []Filter(o),
})
}
// NotFilter provides a filter that matches the opposite of the
// wrapped filter.
type NotFilter struct {
Filter Filter
}
func (n NotFilter) MarshalJSON() ([]byte, error) {
return marshalNamedObject("not", n.Filter)
}
// QueryFilter provides a filter that matches when a query matches
// on a result
type QueryFilter struct {
Query Query
}
func (q QueryFilter) MarshalJSON() ([]byte, error) {
return marshalNamedObject("query", q.Query)
}
// RegexpFilter provides a filter that matches a field against a
// regular expression.
type RegexpFilter struct {
Field string
Regexp string
}
func (r RegexpFilter) MarshalJSON() ([]byte, error) {
return marshalNamedObject("regexp", map[string]string{r.Field: r.Regexp})
}
// TermFilter provides a filter that requires a field to match.
type TermFilter struct {
Field string
Value string
}
func (t TermFilter) MarshalJSON() ([]byte, error) {
return marshalNamedObject("term", map[string]string{t.Field: t.Value})
}
// ExistsFilter provides a filter that requres a field to be present.
type ExistsFilter string
func (f ExistsFilter) MarshalJSON() ([]byte, error) {
return marshalNamedObject("exists", map[string]string{"field": string(f)})
}
// QueryDSL provides a structure to put together a query using the
// elasticsearch DSL.
type QueryDSL struct {
Fields []string `json:"fields"`
From int `json:"from,omitempty"`
Size int `json:"size,omitempty"`
Query Query `json:"query,omitempty"`
Sort []Sort `json:"sort,omitempty"`
}
type Sort struct {
Field string
Order Order
}
type Order struct {
Order string `json:"order"`
}
func (s Sort) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]Order{
s.Field: {s.Order.Order},
})
}
// Ascending is an Order that orders a sort by ascending through the values.
var Ascending = Order{"asc"}
// Descending is an Order that orders a sort by descending throuth the values.
var Descending = Order{"desc"}
// marshalNamedObject provides a helper that creates json objects in a form
// often required by the elasticsearch query DSL. The objects created
// take the following form:
// {
// name: obj
// }
func marshalNamedObject(name string, obj interface{}) ([]byte, error) {
return json.Marshal(map[string]interface{}{name: obj})
}