There are two sub-types of query string keywords, query_string and simple_query_string.
They use different parsers to process the query string. The syntax of the parser is quite different. If the query string is not suitable for the grammar, the parser of query_string will throw an exception, while the parser of simple_query_string will discard the invalid part and continue the process. Both parsers will split the query string with the operators and then, for each group, it splits the field and its value:
- query_string query: The syntax of the query string is described in the following table. If no field is specified in the query string, the setting, index.query.default_field, is used:
The reserved characters are +, -, =, &&, ||, >, <, !, (, ), {, }, [, ], ^, ", ~, *, ?, :, and . If you use them as literals, you need to use backlash characters to escape it: | ||
Item | Description | Example |
: | This is the separator between the field and the value. | "field_1: value_1" |
() | This groups the elements to a clause. | "field_1: (value_1 || value_2)" |
&&,||,! | These are for Boolean operators. | "field_1:(value_1 ||value_2) &&value_3 && !value_4)" |
+,- | The plus symbol followed by a term means that the term must be matched. The minus symbol followed by a term means that the term must not be matched. | "field_1:(+value_1 && -value_2)" |
>, <, = | These are mathematical symbols. |
"field_1:>=4" |
*, ? |
|
"field_1:value_1*" |
" | This matches an exact phrase. | "field_1:"value_1"" |
~ | This represents fuzzy matching. | "field_1:(value_1~ && value_2~)" |
/ | This is for a regular expression. | "field_1://regular expression//" |
^ | This is for boosting (increasing weight). | "field_1:(value_1 value_2^2)" |
[, ], {, } | These are for range queries. { and } mean exclusion, while [ and ] mean inclusion. | "field_1:[1 TO 3}" |
Let's look at an example of the query_string query to find a rating range from 1 to 3, but not including 3, as demonstrated in the following table:
If all the criteria are applied to a single field or multiple fields, then we may rewrite the query using the fields parameter. In the case of multiple fields, it uses the Boolean, OR, to group the matching according to each field. The search request is shown in the following screenshot:
- simple_query_string query: The syntax of the query string is described in the following table. If there is no field specified in the query string, then the index.query.default_field setting is used:
The reserved characters are +, -, |, ", ~, N, *, and (). If you use them as literals, you need to use backlash characters, , to escape them. | ||
Item | Description | Example |
+ | The Boolean AND operator. | "value_1 + value_2" |
| | The Boolean OR operator. | "value_1 | value_2 + value_3" |
- | This must not match the following token. | "-value_1" |
" | This matches an exact phrase. | ""value_1"" |
W~, P~ | If this comes after a word, W, it means fuzziness. If this comes after a phrase, Pit means slop, . |
"value_1~" |
W* | This is for a prefix match if it comes after a word, W. | "value_1*" |
() | This is for precedence. | "(value_1 || value_2) + value_3" |
If you want to only enable some of the reserved characters, you can use the flags
field with the options to enable those items. The options are listed in the following table:
Flag | For reserved character | Description |
ALL | This is the default for all reserved characters. | |
NONE | Disable all reserved characters. | |
AND | + | Enable it. |
OR | | | Enable it. |
NOT | - | Enable it. |
PREFIX | W* | Enable it. |
PHRASE | " | Enable it. |
PRECEDENCE | () | Enable it. |
FUZZY | W~ | Enable ~ with the fuzziness feature. |
SLOP/NEAR | P~ | Enable ~ with the slop feature. |
ESCAPE | Enable the backslash character as an escape character. | |
WHITESPACE | Enable the whitespace character as separators. |