forked from olivere/elastic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_queries_filtered.go
86 lines (74 loc) · 1.99 KB
/
search_queries_filtered.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
// 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
// A query that applies a filter to the results of another query.
// For more details, see
// http://www.elasticsearch.org/guide/reference/query-dsl/filtered-query.html
type FilteredQuery struct {
Query
query Query
filters []Filter
boost *float32
}
// Creates a new filtered query.
func NewFilteredQuery(query Query) FilteredQuery {
q := FilteredQuery{
query: query,
filters: make([]Filter, 0),
}
return q
}
func (q FilteredQuery) Filter(filter Filter) FilteredQuery {
q.filters = append(q.filters, filter)
return q
}
func (q FilteredQuery) Boost(boost float32) FilteredQuery {
q.boost = &boost
return q
}
// Creates the query source for the filtered query.
func (q FilteredQuery) Source() interface{} {
// {
// "filtered" : {
// "query" : {
// "term" : { "tag" : "wow" }
// },
// "filter" : {
// "range" : {
// "age" : { "from" : 10, "to" : 20 }
// }
// }
// }
// }
source := make(map[string]interface{})
filtered := make(map[string]interface{})
source["filtered"] = filtered
filtered["query"] = q.query.Source()
if len(q.filters) == 1 {
filtered["filter"] = q.filters[0].Source()
} else if len(q.filters) > 1 {
filter := make(map[string]interface{})
filtered["filter"] = filter
and := make(map[string]interface{})
filter["and"] = and
filters := make([]interface{}, 0)
for _, f := range q.filters {
filters = append(filters, f.Source())
}
and["filters"] = filters
/*
anded := make([]map[string]interface{}, 0)
filtered["filter"] = anded
for _, f := range q.filters {
andElem := make(map[string]interface{})
andElem["and"] = f.Source()
anded = append(anded, andElem)
}
*/
}
if q.boost != nil {
filtered["boost"] = *q.boost
}
return source
}