This repository has been archived by the owner on Oct 21, 2024. It is now read-only.
forked from olivere/elastic
-
Notifications
You must be signed in to change notification settings - Fork 4
/
search_filters_bool.go
135 lines (118 loc) · 3.19 KB
/
search_filters_bool.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
// 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 filter that matches documents matching boolean combinations
// of other queries. Similar in concept to Boolean query,
// except that the clauses are other filters.
// Can be placed within queries that accept a filter.
// For more details, see:
// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-bool-filter.html
type BoolFilter struct {
mustClauses []Filter
shouldClauses []Filter
mustNotClauses []Filter
cache *bool
cacheKey string
filterName string
}
// NewBoolFilter creates a new bool filter.
func NewBoolFilter() BoolFilter {
f := BoolFilter{
mustClauses: make([]Filter, 0),
shouldClauses: make([]Filter, 0),
mustNotClauses: make([]Filter, 0),
}
return f
}
func (f BoolFilter) Must(filters ...Filter) BoolFilter {
f.mustClauses = append(f.mustClauses, filters...)
return f
}
func (f BoolFilter) MustNot(filters ...Filter) BoolFilter {
f.mustNotClauses = append(f.mustNotClauses, filters...)
return f
}
func (f BoolFilter) Should(filters ...Filter) BoolFilter {
f.shouldClauses = append(f.shouldClauses, filters...)
return f
}
func (f BoolFilter) FilterName(filterName string) BoolFilter {
f.filterName = filterName
return f
}
func (f BoolFilter) Cache(cache bool) BoolFilter {
f.cache = &cache
return f
}
func (f BoolFilter) CacheKey(cacheKey string) BoolFilter {
f.cacheKey = cacheKey
return f
}
// Creates the query source for the bool query.
func (f BoolFilter) Source() interface{} {
// {
// "bool" : {
// "must" : {
// "term" : { "user" : "kimchy" }
// },
// "must_not" : {
// "range" : {
// "age" : { "from" : 10, "to" : 20 }
// }
// },
// "should" : [
// {
// "term" : { "tag" : "wow" }
// },
// {
// "term" : { "tag" : "elasticsearch" }
// }
// ],
// "_cache" : true
// }
// }
source := make(map[string]interface{})
boolClause := make(map[string]interface{})
source["bool"] = boolClause
// must
if len(f.mustClauses) == 1 {
boolClause["must"] = f.mustClauses[0].Source()
} else if len(f.mustClauses) > 1 {
clauses := make([]interface{}, 0)
for _, subQuery := range f.mustClauses {
clauses = append(clauses, subQuery.Source())
}
boolClause["must"] = clauses
}
// must_not
if len(f.mustNotClauses) == 1 {
boolClause["must_not"] = f.mustNotClauses[0].Source()
} else if len(f.mustNotClauses) > 1 {
clauses := make([]interface{}, 0)
for _, subQuery := range f.mustNotClauses {
clauses = append(clauses, subQuery.Source())
}
boolClause["must_not"] = clauses
}
// should
if len(f.shouldClauses) == 1 {
boolClause["should"] = f.shouldClauses[0].Source()
} else if len(f.shouldClauses) > 1 {
clauses := make([]interface{}, 0)
for _, subQuery := range f.shouldClauses {
clauses = append(clauses, subQuery.Source())
}
boolClause["should"] = clauses
}
if f.filterName != "" {
boolClause["_name"] = f.filterName
}
if f.cache != nil {
boolClause["_cache"] = *f.cache
}
if f.cacheKey != "" {
boolClause["_cache_key"] = f.cacheKey
}
return source
}