Debian Codegraph @

search_queries_common_terms.go

raw
1 // Copyright 2012-present Oliver Eilhard. All rights reserved.
2 // Use of this source code is governed by a MIT-license.
3 // See http://olivere.mit-license.org/license.txt for details.
4
5 package elastic
6
7 // CommonTermsQuery is a modern alternative to stopwords
8 // which improves the precision and recall of search results
9 // (by taking stopwords into account), without sacrificing performance.
10 // For more details, see:
11 // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-common-terms-query.html
12 type CommonTermsQuery struct {
13 Query
14 name string
15 text interface{}
16 cutoffFreq *float64
17 highFreq *float64
18 highFreqOp string
19 highFreqMinimumShouldMatch string
20 lowFreq *float64
21 lowFreqOp string
22 lowFreqMinimumShouldMatch string
23 analyzer string
24 boost *float64
25 disableCoord *bool
26 queryName string
27 }
28
29 // NewCommonTermsQuery creates and initializes a new common terms query.
30 func NewCommonTermsQuery(name string, text interface{}) *CommonTermsQuery {
31 return &CommonTermsQuery{name: name, text: text}
32 }
33
34 func (q *CommonTermsQuery) CutoffFrequency(f float64) *CommonTermsQuery {
35 q.cutoffFreq = &f
36 return q
37 }
38
39 func (q *CommonTermsQuery) HighFreq(f float64) *CommonTermsQuery {
40 q.highFreq = &f
41 return q
42 }
43
44 func (q *CommonTermsQuery) HighFreqOperator(op string) *CommonTermsQuery {
45 q.highFreqOp = op
46 return q
47 }
48
49 func (q *CommonTermsQuery) HighFreqMinimumShouldMatch(minShouldMatch string) *CommonTermsQuery {
50 q.highFreqMinimumShouldMatch = minShouldMatch
51 return q
52 }
53
54 func (q *CommonTermsQuery) LowFreq(f float64) *CommonTermsQuery {
55 q.lowFreq = &f
56 return q
57 }
58
59 func (q *CommonTermsQuery) LowFreqOperator(op string) *CommonTermsQuery {
60 q.lowFreqOp = op
61 return q
62 }
63
64 func (q *CommonTermsQuery) LowFreqMinimumShouldMatch(minShouldMatch string) *CommonTermsQuery {
65 q.lowFreqMinimumShouldMatch = minShouldMatch
66 return q
67 }
68
69 func (q *CommonTermsQuery) Analyzer(analyzer string) *CommonTermsQuery {
70 q.analyzer = analyzer
71 return q
72 }
73
74 func (q *CommonTermsQuery) Boost(boost float64) *CommonTermsQuery {
75 q.boost = &boost
76 return q
77 }
78
79 func (q *CommonTermsQuery) DisableCoord(disableCoord bool) *CommonTermsQuery {
80 q.disableCoord = &disableCoord
81 return q
82 }
83
84 func (q *CommonTermsQuery) QueryName(queryName string) *CommonTermsQuery {
85 q.queryName = queryName
86 return q
87 }
88
89 // Creates the query source for the common query.
90 func (q *CommonTermsQuery) Source() (interface{}, error) {
91 // {
92 // "common": {
93 // "body": {
94 // "query": "this is bonsai cool",
95 // "cutoff_frequency": 0.001
96 // }
97 // }
98 // }
99 source := make(map[string]interface{})
100 body := make(map[string]interface{})
101 query := make(map[string]interface{})
102
103 source["common"] = body
104 body[q.name] = query
105 query["query"] = q.text
106
107 if q.cutoffFreq != nil {
108 query["cutoff_frequency"] = *q.cutoffFreq
109 }
110 if q.highFreq != nil {
111 query["high_freq"] = *q.highFreq
112 }
113 if q.highFreqOp != "" {
114 query["high_freq_operator"] = q.highFreqOp
115 }
116 if q.lowFreq != nil {
117 query["low_freq"] = *q.lowFreq
118 }
119 if q.lowFreqOp != "" {
120 query["low_freq_operator"] = q.lowFreqOp
121 }
122 if q.lowFreqMinimumShouldMatch != "" || q.highFreqMinimumShouldMatch != "" {
123 mm := make(map[string]interface{})
124 if q.lowFreqMinimumShouldMatch != "" {
125 mm["low_freq"] = q.lowFreqMinimumShouldMatch
126 }
127 if q.highFreqMinimumShouldMatch != "" {
128 mm["high_freq"] = q.highFreqMinimumShouldMatch
129 }
130 query["minimum_should_match"] = mm
131 }
132 if q.analyzer != "" {
133 query["analyzer"] = q.analyzer
134 }
135 if q.disableCoord != nil {
136 query["disable_coord"] = *q.disableCoord
137 }
138 if q.boost != nil {
139 query["boost"] = *q.boost
140 }
141 if q.queryName != "" {
142 query["_name"] = q.queryName
143 }
144
145 return source, nil
146 }