4
4
<titleabbrev>Simple query string</titleabbrev>
5
5
++++
6
6
7
- A query that uses the SimpleQueryParser to parse its context. Unlike the
8
- regular `query_string` query, the `simple_query_string` query will never
9
- throw an exception, and discards invalid parts of the query. Here is
10
- an example:
7
+ Returns documents based on a provided query string, using a parser with a
8
+ limited but fault-tolerant syntax.
9
+
10
+ This query uses a <<simple-query-string-syntax,simple syntax>> to parse and
11
+ split the provided query string into terms based on special operators. The query
12
+ then <<analysis,analyzes>> each term independently before returning matching
13
+ documents.
14
+
15
+ While its syntax is more limited than the
16
+ <<query-dsl-query-string-query,`query_string` query>>, the `simple_query_string`
17
+ query does not return errors for invalid syntax. Instead, it ignores any invalid
18
+ parts of the query string.
19
+
20
+ [[simple-query-string-query-ex-request]]
21
+ ==== Example request
11
22
12
23
[source,js]
13
24
--------------------------------------------------
@@ -24,72 +35,108 @@ GET /_search
24
35
--------------------------------------------------
25
36
// CONSOLE
26
37
27
- The `simple_query_string` top level parameters include:
28
38
29
- [cols="<,<",options="header",]
30
- |=======================================================================
31
- |Parameter |Description
32
- |`query` |The actual query to be parsed. See below for syntax.
39
+ [[simple-query-string-top-level-params]]
40
+ ==== Top-level parameters for `simple_query_string`
41
+
42
+ `query`::
43
+ (Required, string) Query string you wish to parse and use for search. See <<simple-query-string-syntax>>.
44
+
45
+ `fields`::
46
+ +
47
+ --
48
+ (Optional, array of strings) Array of fields you wish to search.
49
+
50
+ This field accepts wildcard expressions. You also can boost relevance scores for
51
+ matches to particular fields using a caret (`^`) notation. See
52
+ <<simple-query-string-boost>> for examples.
53
+
54
+ Defaults to the `index.query.default_field` index setting, which has a default
55
+ value of `*`. The `*` value extracts all fields that are eligible to term
56
+ queries and filters the metadata fields. All extracted fields are then combined
57
+ to build a query if no `prefix` is specified.
33
58
34
- |`fields` |The fields to perform the parsed query against. Defaults to the
35
- `index.query.default_field` index settings, which in turn defaults to `*`. `* `
36
- extracts all fields in the mapping that are eligible to term queries and filters
37
- the metadata fields.
59
+ WARNING: There is a limit on the number of fields that can be queried at once.
60
+ It is defined by the `indices.query.bool.max_clause_count `
61
+ <<search-settings,search setting>>, which defaults to `1024`.
62
+ --
38
63
39
- WARNING: There is a limit on the number of fields that can be queried
40
- at once. It is defined by the `indices.query.bool.max_clause_count` <<search-settings>>
41
- which defaults to 1024.
64
+ `default_operator`::
65
+ +
66
+ --
67
+ (Optional, string) Default boolean logic used to interpret text in the query
68
+ string if no operators are specified. Valid values are:
42
69
43
- |`default_operator` |The default operator used if no explicit operator
44
- is specified. For example, with a default operator of `OR`, the query
45
- `capital of Hungary` is translated to `capital OR of OR Hungary`, and
46
- with default operator of `AND`, the same query is translated to
47
- `capital AND of AND Hungary`. The default value is `OR`.
70
+ `OR` (Default)::
71
+ For example, a query string of `capital of Hungary` is interpreted as `capital
72
+ OR of OR Hungary`.
48
73
49
- |`analyzer` |Force the analyzer to use to analyze each term of the query when
50
- creating composite queries.
74
+ `AND`::
75
+ For example, a query string of `capital of Hungary` is interpreted as `capital
76
+ AND of AND Hungary`.
77
+ --
51
78
52
- |`flags` |A set of <<supported-flags,flags>> specifying which features of the
53
- `simple_query_string` to enable. Defaults to `ALL`.
79
+ `all_fields`::
80
+ deprecated:[6.0.0, set `fields` to `*` instead](Optional, boolean) If `true`,
81
+ search all searchable fields in the index's field mapping.
54
82
55
- |`analyze_wildcard` | Whether terms of prefix queries should be automatically
56
- analyzed or not. If `true` a best effort will be made to analyze the prefix. However,
57
- some analyzers will be not able to provide a meaningful results
58
- based just on the prefix of a term. Defaults to `false`.
83
+ `analyze_wildcard`::
84
+ (Optional, boolean) If `true`, the query attempts to analyze wildcard terms in
85
+ the query string. Defaults to `false`.
59
86
60
- |`lenient` | If set to `true` will cause format based failures
61
- (like providing text to a numeric field) to be ignored.
87
+ `analyzer`::
88
+ (Optional, string) <<analysis,Analyzer>> used to convert text in the
89
+ query string into tokens. Defaults to the
90
+ <<specify-index-time-analyzer,index-time analyzer>> mapped for the
91
+ `default_field`. If no analyzer is mapped, the index's default analyzer is used.
62
92
63
- |`minimum_should_match` | The minimum number of clauses that must match for a
64
- document to be returned. See the
65
- <<query-dsl-minimum-should-match,`minimum_should_match`>> documentation for the
66
- full list of options .
93
+ `auto_generate_synonyms_phrase_query`::
94
+ (Optional, boolean) If `true`, <<query-dsl-match-query-phrase,match phrase>>
95
+ queries are automatically created for multi-term synonyms. Defaults to `true`.
96
+ See <<simple-query-string-synonyms>> for an example .
67
97
68
- |`quote_field_suffix` | A suffix to append to fields for quoted parts of
69
- the query string. This allows to use a field that has a different analysis chain
70
- for exact matching. Look <<mixing-exact-search-with-stemming,here>> for a
71
- comprehensive example .
98
+ `flags`::
99
+ (Optional, string) List of enabled operators for the
100
+ <<simple-query-string-syntax,simple query string syntax>>. Defaults to `ALL`
101
+ (all operators). See <<supported-flags>> for valid values .
72
102
73
- |`auto_generate_synonyms_phrase_query` |Whether phrase queries should be automatically generated for multi terms synonyms.
74
- Defaults to `true`.
103
+ `fuzzy_max_expansions`::
104
+ (Optional, integer) Maximum number of terms to which the query expands for fuzzy
105
+ matching. Defaults to `50`.
75
106
76
- |`all_fields` | deprecated[6.0.0, set `fields` to `*` instead]
77
- Perform the query on all fields detected in the mapping that can
78
- be queried .
107
+ `fuzzy_prefix_length`::
108
+ (Optional, integer) Number of beginning characters left unchanged for fuzzy
109
+ matching. Defaults to `0` .
79
110
80
- |`fuzzy_prefix_length` |Set the prefix length for fuzzy queries. Default
81
- is `0`.
111
+ `fuzzy_transpositions`::
112
+ (Optional, boolean) If `true`, edits for fuzzy matching include
113
+ transpositions of two adjacent characters (ab → ba). Defaults to `true`.
82
114
83
- |`fuzzy_max_expansions` |Controls the number of terms fuzzy queries will
84
- expand to. Defaults to `50`
115
+ `lenient`::
116
+ (Optional, boolean) If `true`, format-based errors, such as providing a text
117
+ value for a <<number,numeric>> field, are ignored. Defaults to `false`.
85
118
86
- |`fuzzy_transpositions` |Set to `false` to disable fuzzy transpositions (`ab` -> `ba`).
87
- Default is `true`.
88
- |=======================================================================
119
+ `minimum_should_match`::
120
+ (Optional, string) Minimum number of clauses that must match for a document to
121
+ be returned. See the <<query-dsl-minimum-should-match, `minimum_should_match`
122
+ parameter>> for valid values and more information.
89
123
90
- [float]
91
- ===== Simple Query String Syntax
92
- The `simple_query_string` supports the following special characters:
124
+ `quote_field_suffix`::
125
+ +
126
+ --
127
+ (Optional, string) Suffix appended to quoted text in the query string.
128
+
129
+ You can use this suffix to use a different analysis method for exact matches.
130
+ See <<mixing-exact-search-with-stemming>>.
131
+ --
132
+
133
+
134
+ [[simple-query-string-query-notes]]
135
+ ==== Notes
136
+
137
+ [[simple-query-string-syntax]]
138
+ ===== Simple query string syntax
139
+ The `simple_query_string` query supports the following operators:
93
140
94
141
* `+` signifies AND operation
95
142
* `|` signifies OR operation
@@ -100,11 +147,11 @@ The `simple_query_string` supports the following special characters:
100
147
* `~N` after a word signifies edit distance (fuzziness)
101
148
* `~N` after a phrase signifies slop amount
102
149
103
- In order to search for any of these special characters, they will need to
104
- be escaped with `\`.
150
+ To use one of these characters literally, escape it with a preceding backslash
151
+ ( `\`) .
105
152
106
- Be aware that this syntax may have a different behavior depending on the
107
- `default_operator` value. For example, consider the following query :
153
+ The behavior of these operators may differ depending on the `default_operator`
154
+ value. For example:
108
155
109
156
[source,js]
110
157
--------------------------------------------------
@@ -120,84 +167,121 @@ GET /_search
120
167
--------------------------------------------------
121
168
// CONSOLE
122
169
123
- You may expect that documents containing only "foo" or "bar" will be returned,
124
- as long as they do not contain "baz", however, due to the `default_operator`
125
- being OR, this really means "match documents that contain "foo" or documents
126
- that contain "bar", or documents that don't contain "baz". If this is unintended
127
- then the query can be switched to `"foo bar +-baz"` which will not return
128
- documents that contain "baz".
129
-
130
- [float]
131
- ==== Default Field
132
- When not explicitly specifying the field to search on in the query
133
- string syntax, the `index.query.default_field` will be used to derive
134
- which fields to search on. It defaults to `*` and the query will automatically
135
- attempt to determine the existing fields in the index's mapping that are queryable,
136
- and perform the search on those fields.
137
-
138
- [float]
139
- ==== Multi Field
140
- The fields parameter can also include pattern based field names,
141
- allowing to automatically expand to the relevant fields (dynamically
142
- introduced fields included). For example:
170
+ This search is intended to only return documents containing `foo` or `bar` that
171
+ also do **not** contain `baz`. However because of a `default_operator` of `OR`,
172
+ this search actually returns documents that contain `foo` or `bar` and any
173
+ documents that don't contain `baz`. To return documents as intended, change the
174
+ query string to `foo bar +-baz`.
175
+
176
+ [[supported-flags]]
177
+ ===== Limit operators
178
+ You can use the `flags` parameter to limit the supported operators for the
179
+ simple query string syntax.
180
+
181
+ To explicitly enable only specific operators, use a `|` separator. For example,
182
+ a `flags` value of `OR|AND|PREFIX` disables all operators except `OR`, `AND`,
183
+ and `PREFIX`.
143
184
144
185
[source,js]
145
186
--------------------------------------------------
146
187
GET /_search
147
188
{
148
189
"query": {
149
190
"simple_query_string" : {
150
- "fields " : ["content", "name.*^5"] ,
151
- "query " : "foo bar baz "
191
+ "query " : "foo | bar + baz*" ,
192
+ "flags " : "OR|AND|PREFIX "
152
193
}
153
194
}
154
195
}
155
196
--------------------------------------------------
156
197
// CONSOLE
157
198
158
- [float]
159
- [[supported-flags]]
160
- ==== Flags
161
- `simple_query_string` support multiple flags to specify which parsing features
162
- should be enabled. It is specified as a `|`-delimited string with the
163
- `flags` parameter:
199
+ [[supported-flags-values]]
200
+ ====== Valid values
201
+ The available flags are:
202
+
203
+ `ALL` (Default)::
204
+ Enables all optional operators.
205
+
206
+ `AND`::
207
+ Enables the `+` AND operator.
208
+
209
+ `ESCAPE`::
210
+ Enables `\` as an escape character.
211
+
212
+ `FUZZY`::
213
+ Enables the `~N` operator after a word, where `N` is an integer denoting the
214
+ allowed edit distance for matching. See <<fuzziness>>.
215
+
216
+ `NEAR`::
217
+ Enables the `~N` operator, after a phrase where `N` is the maximum number of
218
+ positions allowed between matching tokens. Synonymous to `SLOP`.
219
+
220
+ `NONE`::
221
+ Disables all operators.
222
+
223
+ `NOT`::
224
+ Enables the `-` NOT operator.
225
+
226
+ `OR`::
227
+ Enables the `\|` OR operator.
228
+
229
+ `PHRASE`::
230
+ Enables the `"` quotes operator used to search for phrases.
231
+
232
+ `PRECEDENCE`::
233
+ Enables the `(` and `)` operators to control operator precedence.
234
+
235
+ `PREFIX`::
236
+ Enables the `*` prefix operator.
237
+
238
+ `SLOP`::
239
+ Enables the `~N` operator, after a phrase where `N` is maximum number of
240
+ positions allowed between matching tokens. Synonymous to `NEAR`.
241
+
242
+ `WHITESPACE`::
243
+ Enables whitespace as split characters.
244
+
245
+ [[simple-query-string-boost]]
246
+ ===== Wildcards and per-field boosts in the `fields` parameter
247
+
248
+ Fields can be specified with wildcards, eg:
164
249
165
250
[source,js]
166
251
--------------------------------------------------
167
252
GET /_search
168
253
{
169
- "query": {
170
- "simple_query_string" : {
171
- "query" : "foo | bar + baz*",
172
- "flags" : "OR|AND|PREFIX"
173
- }
254
+ "query": {
255
+ "simple_query_string" : {
256
+ "query": "Will Smith",
257
+ "fields": [ "title", "*_name" ] <1>
174
258
}
259
+ }
175
260
}
176
261
--------------------------------------------------
177
262
// CONSOLE
263
+ <1> Query the `title`, `first_name` and `last_name` fields.
178
264
179
- The available flags are:
265
+ Individual fields can be boosted with the caret (`^`) notation:
266
+
267
+ [source,js]
268
+ --------------------------------------------------
269
+ GET /_search
270
+ {
271
+ "query": {
272
+ "simple_query_string" : {
273
+ "query" : "this is a test",
274
+ "fields" : [ "subject^3", "message" ] <1>
275
+ }
276
+ }
277
+ }
278
+ --------------------------------------------------
279
+ // CONSOLE
280
+
281
+ <1> The `subject` field is three times as important as the `message` field.
180
282
181
- [cols="<,<",options="header",]
182
- |=======================================================================
183
- |Flag |Description
184
- |`ALL` |Enables all parsing features. This is the default.
185
- |`NONE` |Switches off all parsing features.
186
- |`AND` |Enables the `+` AND operator.
187
- |`OR` |Enables the `\|` OR operator.
188
- |`NOT` |Enables the `-` NOT operator.
189
- |`PREFIX` |Enables the `*` Prefix operator.
190
- |`PHRASE` |Enables the `"` quotes operator used to search for phrases.
191
- |`PRECEDENCE` |Enables the `(` and `)` operators to control operator precedence.
192
- |`ESCAPE` |Enables `\` as the escape character.
193
- |`WHITESPACE` |Enables whitespaces as split characters.
194
- |`FUZZY` |Enables the `~N` operator after a word where N is an integer denoting the allowed edit distance for matching (see <<fuzziness>>).
195
- |`SLOP` |Enables the `~N` operator after a phrase where N is an integer denoting the slop amount.
196
- |`NEAR` |Synonymous to `SLOP`.
197
- |=======================================================================
198
-
199
- [float]
200
- ==== Synonyms
283
+ [[simple-query-string-synonyms]]
284
+ ===== Synonyms
201
285
202
286
The `simple_query_string` query supports multi-terms synonym expansion with the <<analysis-synonym-graph-tokenfilter,
203
287
synonym_graph>> token filter. When this filter is used, the parser creates a phrase query for each multi-terms synonyms.
0 commit comments