-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
search_queries_common.go
144 lines (121 loc) · 3.19 KB
/
search_queries_common.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
// 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
// The common terms query is a modern alternative to stopwords
// which improves the precision and recall of search results
// (by taking stopwords into account), without sacrificing performance.
// For more details, see:
// http://www.elasticsearch.org/guide/reference/query-dsl/common-terms-query/
type CommonQuery struct {
Query
name string
query string
cutoffFreq *float64
highFreq *float64
highFreqOp string
highFreqMinMatch interface{}
lowFreq *float64
lowFreqOp string
lowFreqMinMatch interface{}
analyzer string
boost *float64
disableCoords *bool
}
// Creates a new common query.
func NewCommonQuery(name string, query string) CommonQuery {
q := CommonQuery{name: name, query: query}
return q
}
func (q *CommonQuery) CutoffFrequency(f float64) *CommonQuery {
q.cutoffFreq = &f
return q
}
func (q *CommonQuery) HighFreq(f float64) *CommonQuery {
q.highFreq = &f
return q
}
func (q *CommonQuery) HighFreqOperator(op string) *CommonQuery {
q.highFreqOp = op
return q
}
func (q *CommonQuery) HighFreqMinMatch(min interface{}) *CommonQuery {
q.highFreqMinMatch = min
return q
}
func (q *CommonQuery) LowFreq(f float64) *CommonQuery {
q.lowFreq = &f
return q
}
func (q *CommonQuery) LowFreqOperator(op string) *CommonQuery {
q.lowFreqOp = op
return q
}
func (q *CommonQuery) LowFreqMinMatch(min interface{}) *CommonQuery {
q.lowFreqMinMatch = min
return q
}
func (q *CommonQuery) Analyzer(analyzer string) *CommonQuery {
q.analyzer = analyzer
return q
}
func (q *CommonQuery) Boost(boost float64) *CommonQuery {
q.boost = &boost
return q
}
func (q *CommonQuery) DisableCoords(disable bool) *CommonQuery {
q.disableCoords = &disable
return q
}
// Creates the query source for the common query.
func (q CommonQuery) Source() interface{} {
// {
// "common": {
// "body": {
// "query": "this is bonsai cool",
// "cutoff_frequency": 0.001
// }
// }
// }
source := make(map[string]interface{})
body := make(map[string]interface{})
query := make(map[string]interface{})
source["common"] = body
body[q.name] = query
query["query"] = q.query
if q.cutoffFreq != nil {
query["cutoff_frequency"] = *(q.cutoffFreq)
}
if q.highFreq != nil {
query["high_freq"] = *(q.highFreq)
}
if q.highFreqOp != "" {
query["high_freq_operator"] = q.highFreqOp
}
if q.lowFreq != nil {
query["low_freq"] = *(q.lowFreq)
}
if q.lowFreqOp != "" {
query["low_freq_operator"] = q.lowFreqOp
}
if q.lowFreqMinMatch != nil || q.highFreqMinMatch != nil {
mm := make(map[string]interface{})
if q.lowFreqMinMatch != nil {
mm["low_freq"] = q.lowFreqMinMatch
}
if q.highFreqMinMatch != nil {
mm["high_freq"] = q.highFreqMinMatch
}
query["minimum_should_match"] = mm
}
if q.analyzer != "" {
query["analyzer"] = q.analyzer
}
if q.disableCoords != nil {
query["disable_coords"] = *(q.disableCoords)
}
if q.boost != nil {
query["boost"] = *(q.boost)
}
return source
}