| 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 |
) |
| 10 |
|
| 11 |
// QueryStringQuery uses the query parser in order to parse its content. |
| 12 |
// |
| 13 |
// For more details, see |
| 14 |
// https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-query-string-query.html |
| 15 |
type QueryStringQuery struct { |
| 16 |
queryString string |
| 17 |
defaultField string |
| 18 |
defaultOperator string |
| 19 |
analyzer string |
| 20 |
quoteAnalyzer string |
| 21 |
quoteFieldSuffix string |
| 22 |
autoGeneratePhraseQueries *bool |
| 23 |
allowLeadingWildcard *bool |
| 24 |
lowercaseExpandedTerms *bool |
| 25 |
enablePositionIncrements *bool |
| 26 |
analyzeWildcard *bool |
| 27 |
locale string // Deprecated: Decision is now made by the analyzer. |
| 28 |
boost *float64 |
| 29 |
fuzziness string |
| 30 |
fuzzyPrefixLength *int |
| 31 |
fuzzyMaxExpansions *int |
| 32 |
fuzzyRewrite string |
| 33 |
phraseSlop *int |
| 34 |
fields []string |
| 35 |
fieldBoosts map[string]*float64 |
| 36 |
useDisMax *bool |
| 37 |
tieBreaker *float64 |
| 38 |
rewrite string |
| 39 |
minimumShouldMatch string |
| 40 |
lenient *bool |
| 41 |
queryName string |
| 42 |
timeZone string |
| 43 |
maxDeterminizedStates *int |
| 44 |
escape *bool |
| 45 |
splitOnWhitespace *bool |
| 46 |
useAllFields *bool |
| 47 |
} |
| 48 |
|
| 49 |
// NewQueryStringQuery creates and initializes a new QueryStringQuery. |
| 50 |
func NewQueryStringQuery(queryString string) *QueryStringQuery { |
| 51 |
return &QueryStringQuery{ |
| 52 |
queryString: queryString, |
| 53 |
fields: make([]string, 0), |
| 54 |
fieldBoosts: make(map[string]*float64), |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
// DefaultField specifies the field to run against when no prefix field |
| 59 |
// is specified. Only relevant when not explicitly adding fields the query |
| 60 |
// string will run against. |
| 61 |
func (q *QueryStringQuery) DefaultField(defaultField string) *QueryStringQuery { |
| 62 |
q.defaultField = defaultField |
| 63 |
return q |
| 64 |
} |
| 65 |
|
| 66 |
// Field adds a field to run the query string against. |
| 67 |
func (q *QueryStringQuery) Field(field string) *QueryStringQuery { |
| 68 |
q.fields = append(q.fields, field) |
| 69 |
return q |
| 70 |
} |
| 71 |
|
| 72 |
// AllFields tells the query string query to use all fields explicitly, |
| 73 |
// even if _all is enabled. If the "default_field" parameter or "fields" |
| 74 |
// are specified, they will be ignored. |
| 75 |
func (q *QueryStringQuery) AllFields(useAllFields bool) *QueryStringQuery { |
| 76 |
q.useAllFields = &useAllFields |
| 77 |
return q |
| 78 |
} |
| 79 |
|
| 80 |
// FieldWithBoost adds a field to run the query string against with a specific boost. |
| 81 |
func (q *QueryStringQuery) FieldWithBoost(field string, boost float64) *QueryStringQuery { |
| 82 |
q.fields = append(q.fields, field) |
| 83 |
q.fieldBoosts[field] = &boost |
| 84 |
return q |
| 85 |
} |
| 86 |
|
| 87 |
// UseDisMax specifies whether to combine queries using dis max or boolean |
| 88 |
// query when more zhan one field is used with the query string. Defaults |
| 89 |
// to dismax (true). |
| 90 |
func (q *QueryStringQuery) UseDisMax(useDisMax bool) *QueryStringQuery { |
| 91 |
q.useDisMax = &useDisMax |
| 92 |
return q |
| 93 |
} |
| 94 |
|
| 95 |
// TieBreaker is used when more than one field is used with the query string, |
| 96 |
// and combined queries are using dismax. |
| 97 |
func (q *QueryStringQuery) TieBreaker(tieBreaker float64) *QueryStringQuery { |
| 98 |
q.tieBreaker = &tieBreaker |
| 99 |
return q |
| 100 |
} |
| 101 |
|
| 102 |
// DefaultOperator sets the boolean operator of the query parser used to |
| 103 |
// parse the query string. |
| 104 |
// |
| 105 |
// In default mode (OR) terms without any modifiers |
| 106 |
// are considered optional, e.g. "capital of Hungary" is equal to |
| 107 |
// "capital OR of OR Hungary". |
| 108 |
// |
| 109 |
// In AND mode, terms are considered to be in conjunction. The above mentioned |
| 110 |
// query is then parsed as "capital AND of AND Hungary". |
| 111 |
func (q *QueryStringQuery) DefaultOperator(operator string) *QueryStringQuery { |
| 112 |
q.defaultOperator = operator |
| 113 |
return q |
| 114 |
} |
| 115 |
|
| 116 |
// Analyzer is an optional analyzer used to analyze the query string. |
| 117 |
// Note, if a field has search analyzer defined for it, then it will be used |
| 118 |
// automatically. Defaults to the smart search analyzer. |
| 119 |
func (q *QueryStringQuery) Analyzer(analyzer string) *QueryStringQuery { |
| 120 |
q.analyzer = analyzer |
| 121 |
return q |
| 122 |
} |
| 123 |
|
| 124 |
// QuoteAnalyzer is an optional analyzer to be used to analyze the query string |
| 125 |
// for phrase searches. Note, if a field has search analyzer defined for it, |
| 126 |
// then it will be used automatically. Defaults to the smart search analyzer. |
| 127 |
func (q *QueryStringQuery) QuoteAnalyzer(quoteAnalyzer string) *QueryStringQuery { |
| 128 |
q.quoteAnalyzer = quoteAnalyzer |
| 129 |
return q |
| 130 |
} |
| 131 |
|
| 132 |
// AutoGeneratePhraseQueries indicates whether or not phrase queries will |
| 133 |
// be automatically generated when the analyzer returns more then one term |
| 134 |
// from whitespace delimited text. Set to false if phrase queries should only |
| 135 |
// be generated when surrounded by double quotes. |
| 136 |
func (q *QueryStringQuery) AutoGeneratePhraseQueries(autoGeneratePhraseQueries bool) *QueryStringQuery { |
| 137 |
q.autoGeneratePhraseQueries = &autoGeneratePhraseQueries |
| 138 |
return q |
| 139 |
} |
| 140 |
|
| 141 |
// MaxDeterminizedState protects against too-difficult regular expression queries. |
| 142 |
func (q *QueryStringQuery) MaxDeterminizedState(maxDeterminizedStates int) *QueryStringQuery { |
| 143 |
q.maxDeterminizedStates = &maxDeterminizedStates |
| 144 |
return q |
| 145 |
} |
| 146 |
|
| 147 |
// AllowLeadingWildcard specifies whether leading wildcards should be allowed |
| 148 |
// or not (defaults to true). |
| 149 |
func (q *QueryStringQuery) AllowLeadingWildcard(allowLeadingWildcard bool) *QueryStringQuery { |
| 150 |
q.allowLeadingWildcard = &allowLeadingWildcard |
| 151 |
return q |
| 152 |
} |
| 153 |
|
| 154 |
// LowercaseExpandedTerms indicates whether terms of wildcard, prefix, fuzzy |
| 155 |
// and range queries are automatically lower-cased or not. Default is true. |
| 156 |
func (q *QueryStringQuery) LowercaseExpandedTerms(lowercaseExpandedTerms bool) *QueryStringQuery { |
| 157 |
q.lowercaseExpandedTerms = &lowercaseExpandedTerms |
| 158 |
return q |
| 159 |
} |
| 160 |
|
| 161 |
// EnablePositionIncrements indicates whether to enable position increments |
| 162 |
// in result query. Defaults to true. |
| 163 |
// |
| 164 |
// When set, result phrase and multi-phrase queries will be aware of position |
| 165 |
// increments. Useful when e.g. a StopFilter increases the position increment |
| 166 |
// of the token that follows an omitted token. |
| 167 |
func (q *QueryStringQuery) EnablePositionIncrements(enablePositionIncrements bool) *QueryStringQuery { |
| 168 |
q.enablePositionIncrements = &enablePositionIncrements |
| 169 |
return q |
| 170 |
} |
| 171 |
|
| 172 |
// Fuzziness sets the edit distance for fuzzy queries. Default is "AUTO". |
| 173 |
func (q *QueryStringQuery) Fuzziness(fuzziness string) *QueryStringQuery { |
| 174 |
q.fuzziness = fuzziness |
| 175 |
return q |
| 176 |
} |
| 177 |
|
| 178 |
// FuzzyPrefixLength sets the minimum prefix length for fuzzy queries. |
| 179 |
// Default is 1. |
| 180 |
func (q *QueryStringQuery) FuzzyPrefixLength(fuzzyPrefixLength int) *QueryStringQuery { |
| 181 |
q.fuzzyPrefixLength = &fuzzyPrefixLength |
| 182 |
return q |
| 183 |
} |
| 184 |
|
| 185 |
func (q *QueryStringQuery) FuzzyMaxExpansions(fuzzyMaxExpansions int) *QueryStringQuery { |
| 186 |
q.fuzzyMaxExpansions = &fuzzyMaxExpansions |
| 187 |
return q |
| 188 |
} |
| 189 |
|
| 190 |
func (q *QueryStringQuery) FuzzyRewrite(fuzzyRewrite string) *QueryStringQuery { |
| 191 |
q.fuzzyRewrite = fuzzyRewrite |
| 192 |
return q |
| 193 |
} |
| 194 |
|
| 195 |
// PhraseSlop sets the default slop for phrases. If zero, then exact matches |
| 196 |
// are required. Default value is zero. |
| 197 |
func (q *QueryStringQuery) PhraseSlop(phraseSlop int) *QueryStringQuery { |
| 198 |
q.phraseSlop = &phraseSlop |
| 199 |
return q |
| 200 |
} |
| 201 |
|
| 202 |
// AnalyzeWildcard indicates whether to enabled analysis on wildcard and prefix queries. |
| 203 |
func (q *QueryStringQuery) AnalyzeWildcard(analyzeWildcard bool) *QueryStringQuery { |
| 204 |
q.analyzeWildcard = &analyzeWildcard |
| 205 |
return q |
| 206 |
} |
| 207 |
|
| 208 |
func (q *QueryStringQuery) Rewrite(rewrite string) *QueryStringQuery { |
| 209 |
q.rewrite = rewrite |
| 210 |
return q |
| 211 |
} |
| 212 |
|
| 213 |
func (q *QueryStringQuery) MinimumShouldMatch(minimumShouldMatch string) *QueryStringQuery { |
| 214 |
q.minimumShouldMatch = minimumShouldMatch |
| 215 |
return q |
| 216 |
} |
| 217 |
|
| 218 |
// Boost sets the boost for this query. |
| 219 |
func (q *QueryStringQuery) Boost(boost float64) *QueryStringQuery { |
| 220 |
q.boost = &boost |
| 221 |
return q |
| 222 |
} |
| 223 |
|
| 224 |
// QuoteFieldSuffix is an optional field name suffix to automatically |
| 225 |
// try and add to the field searched when using quoted text. |
| 226 |
func (q *QueryStringQuery) QuoteFieldSuffix(quoteFieldSuffix string) *QueryStringQuery { |
| 227 |
q.quoteFieldSuffix = quoteFieldSuffix |
| 228 |
return q |
| 229 |
} |
| 230 |
|
| 231 |
// Lenient indicates whether the query string parser should be lenient |
| 232 |
// when parsing field values. It defaults to the index setting and if not |
| 233 |
// set, defaults to false. |
| 234 |
func (q *QueryStringQuery) Lenient(lenient bool) *QueryStringQuery { |
| 235 |
q.lenient = &lenient |
| 236 |
return q |
| 237 |
} |
| 238 |
|
| 239 |
// QueryName sets the query name for the filter that can be used when |
| 240 |
// searching for matched_filters per hit. |
| 241 |
func (q *QueryStringQuery) QueryName(queryName string) *QueryStringQuery { |
| 242 |
q.queryName = queryName |
| 243 |
return q |
| 244 |
} |
| 245 |
|
| 246 |
// Locale specifies the locale to be used for string conversions. |
| 247 |
// |
| 248 |
// Deprecated: Decision is now made by the analyzer. |
| 249 |
func (q *QueryStringQuery) Locale(locale string) *QueryStringQuery { |
| 250 |
q.locale = locale |
| 251 |
return q |
| 252 |
} |
| 253 |
|
| 254 |
// TimeZone can be used to automatically adjust to/from fields using a |
| 255 |
// timezone. Only used with date fields, of course. |
| 256 |
func (q *QueryStringQuery) TimeZone(timeZone string) *QueryStringQuery { |
| 257 |
q.timeZone = timeZone |
| 258 |
return q |
| 259 |
} |
| 260 |
|
| 261 |
// Escape performs escaping of the query string. |
| 262 |
func (q *QueryStringQuery) Escape(escape bool) *QueryStringQuery { |
| 263 |
q.escape = &escape |
| 264 |
return q |
| 265 |
} |
| 266 |
|
| 267 |
// SplitOnWhitespace indicates whether query text should be split on whitespace |
| 268 |
// prior to analysis. |
| 269 |
func (q *QueryStringQuery) SplitOnWhitespace(splitOnWhitespace bool) *QueryStringQuery { |
| 270 |
q.splitOnWhitespace = &splitOnWhitespace |
| 271 |
return q |
| 272 |
} |
| 273 |
|
| 274 |
// Source returns JSON for the query. |
| 275 |
func (q *QueryStringQuery) Source() (interface{}, error) { |
| 276 |
source := make(map[string]interface{}) |
| 277 |
query := make(map[string]interface{}) |
| 278 |
source["query_string"] = query |
| 279 |
|
| 280 |
query["query"] = q.queryString |
| 281 |
|
| 282 |
if q.defaultField != "" { |
| 283 |
query["default_field"] = q.defaultField |
| 284 |
} |
| 285 |
|
| 286 |
if len(q.fields) > 0 { |
| 287 |
var fields []string |
| 288 |
for _, field := range q.fields { |
| 289 |
if boost, found := q.fieldBoosts[field]; found { |
| 290 |
if boost != nil { |
| 291 |
fields = append(fields, fmt.Sprintf("%s^%f", field, *boost)) |
| 292 |
} else { |
| 293 |
fields = append(fields, field) |
| 294 |
} |
| 295 |
} else { |
| 296 |
fields = append(fields, field) |
| 297 |
} |
| 298 |
} |
| 299 |
query["fields"] = fields |
| 300 |
} |
| 301 |
|
| 302 |
if q.tieBreaker != nil { |
| 303 |
query["tie_breaker"] = *q.tieBreaker |
| 304 |
} |
| 305 |
if q.useDisMax != nil { |
| 306 |
query["use_dis_max"] = *q.useDisMax |
| 307 |
} |
| 308 |
if q.defaultOperator != "" { |
| 309 |
query["default_operator"] = q.defaultOperator |
| 310 |
} |
| 311 |
if q.analyzer != "" { |
| 312 |
query["analyzer"] = q.analyzer |
| 313 |
} |
| 314 |
if q.quoteAnalyzer != "" { |
| 315 |
query["quote_analyzer"] = q.quoteAnalyzer |
| 316 |
} |
| 317 |
if q.autoGeneratePhraseQueries != nil { |
| 318 |
query["auto_generate_phrase_queries"] = *q.autoGeneratePhraseQueries |
| 319 |
} |
| 320 |
if q.maxDeterminizedStates != nil { |
| 321 |
query["max_determinized_states"] = *q.maxDeterminizedStates |
| 322 |
} |
| 323 |
if q.allowLeadingWildcard != nil { |
| 324 |
query["allow_leading_wildcard"] = *q.allowLeadingWildcard |
| 325 |
} |
| 326 |
if q.lowercaseExpandedTerms != nil { |
| 327 |
query["lowercase_expanded_terms"] = *q.lowercaseExpandedTerms |
| 328 |
} |
| 329 |
if q.enablePositionIncrements != nil { |
| 330 |
query["enable_position_increments"] = *q.enablePositionIncrements |
| 331 |
} |
| 332 |
if q.fuzziness != "" { |
| 333 |
query["fuzziness"] = q.fuzziness |
| 334 |
} |
| 335 |
if q.boost != nil { |
| 336 |
query["boost"] = *q.boost |
| 337 |
} |
| 338 |
if q.fuzzyPrefixLength != nil { |
| 339 |
query["fuzzy_prefix_length"] = *q.fuzzyPrefixLength |
| 340 |
} |
| 341 |
if q.fuzzyMaxExpansions != nil { |
| 342 |
query["fuzzy_max_expansions"] = *q.fuzzyMaxExpansions |
| 343 |
} |
| 344 |
if q.fuzzyRewrite != "" { |
| 345 |
query["fuzzy_rewrite"] = q.fuzzyRewrite |
| 346 |
} |
| 347 |
if q.phraseSlop != nil { |
| 348 |
query["phrase_slop"] = *q.phraseSlop |
| 349 |
} |
| 350 |
if q.analyzeWildcard != nil { |
| 351 |
query["analyze_wildcard"] = *q.analyzeWildcard |
| 352 |
} |
| 353 |
if q.rewrite != "" { |
| 354 |
query["rewrite"] = q.rewrite |
| 355 |
} |
| 356 |
if q.minimumShouldMatch != "" { |
| 357 |
query["minimum_should_match"] = q.minimumShouldMatch |
| 358 |
} |
| 359 |
if q.quoteFieldSuffix != "" { |
| 360 |
query["quote_field_suffix"] = q.quoteFieldSuffix |
| 361 |
} |
| 362 |
if q.lenient != nil { |
| 363 |
query["lenient"] = *q.lenient |
| 364 |
} |
| 365 |
if q.queryName != "" { |
| 366 |
query["_name"] = q.queryName |
| 367 |
} |
| 368 |
if q.locale != "" { |
| 369 |
query["locale"] = q.locale |
| 370 |
} |
| 371 |
if q.timeZone != "" { |
| 372 |
query["time_zone"] = q.timeZone |
| 373 |
} |
| 374 |
if q.escape != nil { |
| 375 |
query["escape"] = *q.escape |
| 376 |
} |
| 377 |
if q.splitOnWhitespace != nil { |
| 378 |
query["split_on_whitespace"] = *q.splitOnWhitespace |
| 379 |
} |
| 380 |
if q.useAllFields != nil { |
| 381 |
query["all_fields"] = *q.useAllFields |
| 382 |
} |
| 383 |
|
| 384 |
return source, nil |
| 385 |
} |