Debian Codegraph @

search_queries_simple_query_string.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 import (
8 "fmt"
9 "strings"
10 )
11
12 // SimpleQueryStringQuery is a query that uses the SimpleQueryParser
13 // to parse its context. Unlike the regular query_string query,
14 // the simple_query_string query will never throw an exception,
15 // and discards invalid parts of the query.
16 //
17 // For more details, see
18 // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-simple-query-string-query.html
19 type SimpleQueryStringQuery struct {
20 queryText string
21 analyzer string
22 quoteFieldSuffix string
23 operator string
24 fields []string
25 fieldBoosts map[string]*float64
26 minimumShouldMatch string
27 flags string
28 boost *float64
29 lowercaseExpandedTerms *bool
30 lenient *bool
31 analyzeWildcard *bool
32 locale string
33 queryName string
34 }
35
36 // NewSimpleQueryStringQuery creates and initializes a new SimpleQueryStringQuery.
37 func NewSimpleQueryStringQuery(text string) *SimpleQueryStringQuery {
38 return &SimpleQueryStringQuery{
39 queryText: text,
40 fields: make([]string, 0),
41 fieldBoosts: make(map[string]*float64),
42 }
43 }
44
45 // Field adds a field to run the query against.
46 func (q *SimpleQueryStringQuery) Field(field string) *SimpleQueryStringQuery {
47 q.fields = append(q.fields, field)
48 return q
49 }
50
51 // Field adds a field to run the query against with a specific boost.
52 func (q *SimpleQueryStringQuery) FieldWithBoost(field string, boost float64) *SimpleQueryStringQuery {
53 q.fields = append(q.fields, field)
54 q.fieldBoosts[field] = &boost
55 return q
56 }
57
58 // Boost sets the boost for this query.
59 func (q *SimpleQueryStringQuery) Boost(boost float64) *SimpleQueryStringQuery {
60 q.boost = &boost
61 return q
62 }
63
64 // QueryName sets the query name for the filter that can be used when
65 // searching for matched_filters per hit.
66 func (q *SimpleQueryStringQuery) QueryName(queryName string) *SimpleQueryStringQuery {
67 q.queryName = queryName
68 return q
69 }
70
71 // Analyzer specifies the analyzer to use for the query.
72 func (q *SimpleQueryStringQuery) Analyzer(analyzer string) *SimpleQueryStringQuery {
73 q.analyzer = analyzer
74 return q
75 }
76
77 // QuoteFieldSuffix is an optional field name suffix to automatically
78 // try and add to the field searched when using quoted text.
79 func (q *SimpleQueryStringQuery) QuoteFieldSuffix(quoteFieldSuffix string) *SimpleQueryStringQuery {
80 q.quoteFieldSuffix = quoteFieldSuffix
81 return q
82 }
83
84 // DefaultOperator specifies the default operator for the query.
85 func (q *SimpleQueryStringQuery) DefaultOperator(defaultOperator string) *SimpleQueryStringQuery {
86 q.operator = defaultOperator
87 return q
88 }
89
90 // Flags sets the flags for the query.
91 func (q *SimpleQueryStringQuery) Flags(flags string) *SimpleQueryStringQuery {
92 q.flags = flags
93 return q
94 }
95
96 // LowercaseExpandedTerms indicates whether terms of wildcard, prefix, fuzzy
97 // and range queries are automatically lower-cased or not. Default is true.
98 func (q *SimpleQueryStringQuery) LowercaseExpandedTerms(lowercaseExpandedTerms bool) *SimpleQueryStringQuery {
99 q.lowercaseExpandedTerms = &lowercaseExpandedTerms
100 return q
101 }
102
103 func (q *SimpleQueryStringQuery) Locale(locale string) *SimpleQueryStringQuery {
104 q.locale = locale
105 return q
106 }
107
108 // Lenient indicates whether the query string parser should be lenient
109 // when parsing field values. It defaults to the index setting and if not
110 // set, defaults to false.
111 func (q *SimpleQueryStringQuery) Lenient(lenient bool) *SimpleQueryStringQuery {
112 q.lenient = &lenient
113 return q
114 }
115
116 // AnalyzeWildcard indicates whether to enabled analysis on wildcard and prefix queries.
117 func (q *SimpleQueryStringQuery) AnalyzeWildcard(analyzeWildcard bool) *SimpleQueryStringQuery {
118 q.analyzeWildcard = &analyzeWildcard
119 return q
120 }
121
122 func (q *SimpleQueryStringQuery) MinimumShouldMatch(minimumShouldMatch string) *SimpleQueryStringQuery {
123 q.minimumShouldMatch = minimumShouldMatch
124 return q
125 }
126
127 // Source returns JSON for the query.
128 func (q *SimpleQueryStringQuery) Source() (interface{}, error) {
129 // {
130 // "simple_query_string" : {
131 // "query" : "\"fried eggs\" +(eggplant | potato) -frittata",
132 // "analyzer" : "snowball",
133 // "fields" : ["body^5","_all"],
134 // "default_operator" : "and"
135 // }
136 // }
137
138 source := make(map[string]interface{})
139
140 query := make(map[string]interface{})
141 source["simple_query_string"] = query
142
143 query["query"] = q.queryText
144
145 if len(q.fields) > 0 {
146 var fields []string
147 for _, field := range q.fields {
148 if boost, found := q.fieldBoosts[field]; found {
149 if boost != nil {
150 fields = append(fields, fmt.Sprintf("%s^%f", field, *boost))
151 } else {
152 fields = append(fields, field)
153 }
154 } else {
155 fields = append(fields, field)
156 }
157 }
158 query["fields"] = fields
159 }
160
161 if q.flags != "" {
162 query["flags"] = q.flags
163 }
164 if q.analyzer != "" {
165 query["analyzer"] = q.analyzer
166 }
167 if q.operator != "" {
168 query["default_operator"] = strings.ToLower(q.operator)
169 }
170 if q.lowercaseExpandedTerms != nil {
171 query["lowercase_expanded_terms"] = *q.lowercaseExpandedTerms
172 }
173 if q.lenient != nil {
174 query["lenient"] = *q.lenient
175 }
176 if q.analyzeWildcard != nil {
177 query["analyze_wildcard"] = *q.analyzeWildcard
178 }
179 if q.locale != "" {
180 query["locale"] = q.locale
181 }
182 if q.queryName != "" {
183 query["_name"] = q.queryName
184 }
185 if q.minimumShouldMatch != "" {
186 query["minimum_should_match"] = q.minimumShouldMatch
187 }
188 if q.quoteFieldSuffix != "" {
189 query["quote_field_suffix"] = q.quoteFieldSuffix
190 }
191 if q.boost != nil {
192 query["boost"] = *q.boost
193 }
194
195 return source, nil
196 }