-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimple_query_string.go
321 lines (304 loc) · 12.6 KB
/
simple_query_string.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package es
import Operator "github.com/Trendyol/es-query-builder/es/enums/operator"
type simpleQueryStringType Object
// SimpleQueryString creates a new es.simpleQueryStringType object with the specified query string.
//
// This function initializes an es.simpleQueryStringType object with a simple query string, which
// is typically used to perform simple text search queries in Elasticsearch. The query string
// can contain multiple terms and operators, allowing for basic search expressions.
//
// Example usage:
//
// q := es.SimpleQueryString("Foo + Bar")
// // q now contains an es.simpleQueryStringType object with a simple query string query.
//
// Parameters:
// - query: The query string to be used in the search. The type is generic and can be
// any type that represents a query string.
//
// Returns:
//
// An es.simpleQueryStringType object containing the specified query string.
func SimpleQueryString[T any](query T) simpleQueryStringType {
return simpleQueryStringType{
"simple_query_string": Object{
"query": query,
},
}
}
// Fields sets the fields to be searched within the es.simpleQueryStringType object.
//
// This method specifies a list of fields that the query string should search.
// If multiple fields are provided, the query will search across all of them,
// allowing for more flexible and comprehensive search queries.
//
// Example usage:
//
// q := es.SimpleQueryString("Foo Bar").Fields([]string{"title", "content"})
// // q now searches within the "title" and "content" fields.
//
// Parameters:
// - value: A slice of strings representing the field names to be searched.
//
// Returns:
//
// The updated es.simpleQueryStringType object with the "fields" option set.
func (q simpleQueryStringType) Fields(value []string) simpleQueryStringType {
return q.putInTheField("fields", value)
}
// Analyzer sets the analyzer to be used for the es.simpleQueryStringType object.
//
// This method specifies the analyzer that should be applied to the query string.
// Analyzers are used to process the text, such as tokenizing and normalizing it,
// allowing for more refined and accurate search queries based on the specified analyzer.
//
// Example usage:
//
// q := es.SimpleQueryString("Foo Bar").Analyzer("standard")
// // q now uses the "standard" analyzer for processing the query string.
//
// Parameters:
// - value: A string representing the name of the analyzer to be used.
//
// Returns:
//
// The updated es.simpleQueryStringType object with the "analyzer" set.
func (q simpleQueryStringType) Analyzer(value string) simpleQueryStringType {
return q.putInTheField("analyzer", value)
}
// DefaultOperator sets the default operator for the es.simpleQueryStringType object.
//
// This method specifies the default operator to be used between terms in the query string
// when no explicit operator is provided. The default operator can be operator.And or operator.Or,
// determining whether all terms (and) or any term (or) must be matched in the search results.
//
// Example usage:
//
// q := es.SimpleQueryString("Foo Bar").
// DefaultOperator(operator.Or)
//
// q now uses "or" as the default operator, meaning any term can match in the query.
//
// Parameters:
// - operator: A operator.Operator representing the default operator to be used ("and" or "or").
//
// Returns:
//
// The updated es.simpleQueryStringType object with the "default_operator" set.
func (q simpleQueryStringType) DefaultOperator(operator Operator.Operator) simpleQueryStringType {
return q.putInTheField("default_operator", operator)
}
// MinimumShouldMatch sets the minimum number of clauses that must match for the es.simpleQueryStringType object.
//
// This method specifies the minimum number of clauses that must match in order
// for a document to be considered a match. This can be expressed as an absolute number or a percentage,
// allowing for fine-tuned control over query matching behavior.
//
// Example usage:
//
// q := es.SimpleQueryString("Foo Bar Baz").MinimumShouldMatch("2")
// // q now requires that at least 2 of the terms match for a document to be considered a match.
//
// Parameters:
// - value: A string representing the minimum number or percentage of clauses that must match.
//
// Returns:
//
// The updated es.simpleQueryStringType object with the "minimum_should_match" option set.
func (q simpleQueryStringType) MinimumShouldMatch(value string) simpleQueryStringType {
return q.putInTheField("minimum_should_match", value)
}
// FuzzyMaxExpansions sets the maximum number of expansions for fuzzy matching in the es.simpleQueryStringType object.
//
// This method specifies the maximum number of terms that the query will expand to
// when performing fuzzy matching. This setting controls the number of variations
// of the search term that will be considered in the query, affecting both performance
// and accuracy of fuzzy searches.
//
// Example usage:
//
// q := es.SimpleQueryString("Foo~").FuzzyMaxExpansions(50)
// // q now allows up to 50 expansions for fuzzy matching.
//
// Parameters:
// - value: An integer representing the maximum number of term expansions for fuzzy matching.
//
// Returns:
//
// The updated es.simpleQueryStringType object with the "fuzzy_max_expansions" option set.
func (q simpleQueryStringType) FuzzyMaxExpansions(value int) simpleQueryStringType {
return q.putInTheField("fuzzy_max_expansions", value)
}
// FuzzyPrefixLength sets the prefix length for fuzzy matching in the es.simpleQueryStringType object.
//
// This method specifies the length of the initial characters that must match exactly
// before applying any fuzziness in the query. Increasing the prefix length can improve
// performance by reducing the number of potential matches, while still allowing for
// approximate matching beyond the prefix.
//
// Example usage:
//
// q := es.SimpleQueryString("Foo~").FuzzyPrefixLength(2)
// // q now requires the first 2 characters to match exactly before applying fuzziness.
//
// Parameters:
// - value: An integer representing the number of initial characters that must match exactly.
//
// Returns:
//
// The updated es.simpleQueryStringType object with the "fuzzy_prefix_length" option set.
func (q simpleQueryStringType) FuzzyPrefixLength(value int) simpleQueryStringType {
return q.putInTheField("fuzzy_prefix_length", value)
}
// FuzzyTranspositions sets the option to allow transpositions in fuzzy matching for the es.simpleQueryStringType object.
//
// This method enables or disables the allowance of transpositions (swapping of adjacent characters)
// in fuzzy matching. When set to true, terms that are similar but have transposed characters
// (e.g., "ab" vs. "ba") will still be considered a match, increasing the flexibility of the search.
//
// Example usage:
//
// q := es.SimpleQueryString("Foo~").FuzzyTranspositions(true)
// // q now allows transpositions in fuzzy matching.
//
// Parameters:
// - value: A boolean indicating whether transpositions are allowed (true) or not (false).
//
// Returns:
//
// The updated es.simpleQueryStringType object with the "fuzzy_transpositions" option set.
func (q simpleQueryStringType) FuzzyTranspositions(value bool) simpleQueryStringType {
return q.putInTheField("fuzzy_transpositions", value)
}
// AnalyzeWildcard sets the option to analyze wildcard terms in the simpleQueryStringType object.
//
// This method determines whether wildcard terms in the query string should be analyzed.
// When set to true, wildcard terms (* and ?) will be analyzed by the analyzer defined
// for the field, allowing for more accurate searches when using wildcards.
//
// Example usage:
//
// q := es.SimpleQueryString("Fo*").AnalyzeWildcard(true)
// // q now analyzes wildcard terms in the query string.
//
// Parameters:
// - value: A boolean indicating whether wildcard terms should be analyzed (true) or not (false).
//
// Returns:
//
// The updated es.simpleQueryStringType object with the "analyze_wildcard" option set.
func (q simpleQueryStringType) AnalyzeWildcard(value bool) simpleQueryStringType {
return q.putInTheField("analyze_wildcard", value)
}
// AutoGenerateSynonymsPhraseQuery sets the option to automatically generate phrase queries for synonyms
// in the es.simpleQueryStringType object.
//
// This method enables or disables the automatic generation of phrase queries for synonyms in the query string.
// When set to true, Elasticsearch will automatically create phrase queries for terms that have synonyms,
// which can improve search accuracy when working with synonym filters.
//
// Example usage:
//
// q := es.SimpleQueryString("quick brown fox").AutoGenerateSynonymsPhraseQuery(true)
// // q now automatically generates phrase queries for synonyms in the query string.
//
// Parameters:
// - value: A boolean indicating whether to automatically generate phrase queries for synonyms (true) or not (false).
//
// Returns:
//
// The updated es.simpleQueryStringType object with the "auto_generate_synonyms_phrase_query" option set.
func (q simpleQueryStringType) AutoGenerateSynonymsPhraseQuery(value bool) simpleQueryStringType {
return q.putInTheField("auto_generate_synonyms_phrase_query", value)
}
// Flags sets the flags for the es.simpleQueryStringType object.
//
// This method specifies which features of the simple_query_string query should be enabled.
// It allows fine-grained control over the query's behavior by enabling or disabling specific features.
//
// Example usage:
//
// q := es.SimpleQueryString("Foo Bar").Flags("AND|OR|PREFIX")
// // q now enables AND, OR, and PREFIX features for the query.
//
// Parameters:
// - value: A string representing the enabled features, separated by '|'.
// Possible values include: ALL, NONE, AND, OR, NOT, PREFIX, PHRASE, PRECEDENCE, ESCAPE, WHITESPACE, FUZZY, NEAR, SLOP.
//
// Returns:
//
// The updated es.simpleQueryStringType object with the "flags" option set.
func (q simpleQueryStringType) Flags(value string) simpleQueryStringType {
return q.putInTheField("flags", value)
}
// Lenient sets the leniency option for the es.simpleQueryStringType object.
//
// This method determines whether the query should be lenient when encountering
// errors, such as analyzing incompatible fields. When set to true, the query will
// ignore such errors, allowing for more flexible and fault-tolerant searches,
// especially in cases where the data types may not match perfectly.
//
// Example usage:
//
// q := es.SimpleQueryString("Foo Bar").Lenient(true)
// // q is now lenient, allowing it to tolerate errors during the query.
//
// Parameters:
// - value: A boolean indicating whether leniency is enabled (true) or disabled (false).
//
// Returns:
//
// The updated es.simpleQueryStringType object with the "lenient" option set.
func (q simpleQueryStringType) Lenient(value bool) simpleQueryStringType {
return q.putInTheField("lenient", value)
}
// QuoteFieldSuffix sets the field suffix to be used for quoted text in the es.simpleQueryStringType object.
//
// This method specifies a suffix to be appended to the field names when analyzing quoted text in the query string.
// This is useful for applying different analyzers or field mappings to quoted phrases compared to unquoted terms.
//
// Example usage:
//
// q := es.SimpleQueryString("Foo \"Bar Baz\"").QuoteFieldSuffix("_phrase")
// // q now appends "_phrase" to the field names when processing quoted text in the query string.
//
// Parameters:
// - value: A string representing the suffix to be appended to field names for quoted text.
//
// Returns:
//
// The updated es.simpleQueryStringType object with the "quote_field_suffix" option set.
func (q simpleQueryStringType) QuoteFieldSuffix(value string) simpleQueryStringType {
return q.putInTheField("quote_field_suffix", value)
}
// Boost sets the "boost" parameter in an es.simpleQueryStringType query.
//
// This method allows you to specify a boost factor for the simple query string query,
// which influences the relevance score of matching documents. A higher boost value
// increases the importance of the query in the overall score, resulting in higher
// scores for documents that match the query string conditions.
//
// Example usage:
//
// q := es.SimpleQueryString().Boost(1.8)
// // q now includes a "boost" parameter set to 1.8.
//
// Parameters:
// - boost: A float64 value representing the boost factor for the simple
// query string query.
//
// Returns:
//
// The updated es.simpleQueryStringType object with the "boost" parameter set.
func (q simpleQueryStringType) Boost(boost float64) simpleQueryStringType {
return q.putInTheField("boost", boost)
}
func (q simpleQueryStringType) putInTheField(key string, value any) simpleQueryStringType {
for _, fieldObj := range q {
if fieldObject, ok := fieldObj.(Object); ok {
fieldObject[key] = value
break
}
}
return q
}