Skip to content

Commit 49bcad9

Browse files
committed
[DOCS] Reformats simple query string query (elastic#45343)
1 parent 5fe614e commit 49bcad9

File tree

1 file changed

+196
-112
lines changed

1 file changed

+196
-112
lines changed

docs/reference/query-dsl/simple-query-string-query.asciidoc

Lines changed: 196 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,21 @@
44
<titleabbrev>Simple query string</titleabbrev>
55
++++
66

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
1122

1223
[source,js]
1324
--------------------------------------------------
@@ -24,72 +35,108 @@ GET /_search
2435
--------------------------------------------------
2536
// CONSOLE
2637

27-
The `simple_query_string` top level parameters include:
2838

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.
3358

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+
--
3863

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:
4269

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`.
4873

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+
--
5178

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.
5482

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`.
5986

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.
6292

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.
6797

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.
72102

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`.
75106

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`.
79110

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`.
82114

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`.
85118

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.
89123

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:
93140

94141
* `+` signifies AND operation
95142
* `|` signifies OR operation
@@ -100,11 +147,11 @@ The `simple_query_string` supports the following special characters:
100147
* `~N` after a word signifies edit distance (fuzziness)
101148
* `~N` after a phrase signifies slop amount
102149

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+
(`\`).
105152

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:
108155

109156
[source,js]
110157
--------------------------------------------------
@@ -120,84 +167,121 @@ GET /_search
120167
--------------------------------------------------
121168
// CONSOLE
122169

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`.
143184

144185
[source,js]
145186
--------------------------------------------------
146187
GET /_search
147188
{
148189
"query": {
149190
"simple_query_string" : {
150-
"fields" : ["content", "name.*^5"],
151-
"query" : "foo bar baz"
191+
"query" : "foo | bar + baz*",
192+
"flags" : "OR|AND|PREFIX"
152193
}
153194
}
154195
}
155196
--------------------------------------------------
156197
// CONSOLE
157198

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:
164249

165250
[source,js]
166251
--------------------------------------------------
167252
GET /_search
168253
{
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>
174258
}
259+
}
175260
}
176261
--------------------------------------------------
177262
// CONSOLE
263+
<1> Query the `title`, `first_name` and `last_name` fields.
178264

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.
180282

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
201285

202286
The `simple_query_string` query supports multi-terms synonym expansion with the <<analysis-synonym-graph-tokenfilter,
203287
synonym_graph>> token filter. When this filter is used, the parser creates a phrase query for each multi-terms synonyms.

0 commit comments

Comments
 (0)