Both queries parse a search string with operators, but they differ in strictness. The Elasticsearch query_string query accepts the full Lucene syntax and returns an error if the string is malformed, while simple_query_string supports a smaller operator set and silently ignores invalid syntax. Use simple_query_string for text typed by end users, and query_string only for trusted or advanced input.
Both let a single string express things like quick AND fox, phrases and exclusions, so you can build a search box without assembling a bool query yourself.
The query_string query parses its input with a strict query parser. It splits the string into terms and operators, analyses each term with the field analyser, and builds an equivalent query.
Supported syntax includes:
AND, OR, NOT (written in uppercase), and + and - prefixes.title:guide.quick*), fuzziness (quikc~1), proximity ("quick fox"~2), ranges (price:[10 TO 50]) and regular expressions.If the string has an unbalanced quote or parenthesis, or ends with a dangling operator, the request fails with a parse exception and a 400 response.
simple_query_string uses a more limited syntax designed for user-facing search boxes. It never throws a parse error: invalid parts are discarded or treated as text.
Its operators are:
| Operator | Meaning |
|---|---|
+ | AND |
| | OR |
- | NOT (negate the term) |
"..." | Phrase |
* (suffix) | Prefix match |
( ) | Grouping |
~N after a word | Fuzziness with edit distance N |
~N after a phrase | Slop, the allowed gap between phrase words |
There are no field prefixes such as title:, no ranges and no wildcards at the start of a term. You choose fields only through the fields parameter. You can also limit which operators are enabled using the flags parameter, for example "flags": "AND|OR|PHRASE".
| Aspect | query_string | simple_query_string |
|---|---|---|
| Syntax errors | Returns an error | Ignores invalid parts |
| Boolean words | AND, OR, NOT | +, |, - |
| Field syntax in the string | Yes (field:term) | No |
| Ranges, regex, leading wildcards | Yes | No |
| Safe for raw user input | Poor | Good |
| Control over allowed features | Limited | flags parameter |
Both queries share parameters such as fields, default_operator, analyzer, minimum_should_match and lenient (which ignores type mismatches like text searched against a numeric field). By default default_operator is OR, so a search for quick fox matches documents containing either word, unless you set it to AND.
For a search box that anyone can type into, choose simple_query_string. A visitor who types an unclosed quote will get results rather than a 400 error, and they cannot reach into fields you did not list or run expensive constructs such as regular expressions or leading wildcards.
query_string is a reasonable choice when:
status:error AND level:[3 TO 5].Even then, be careful. A leading wildcard like *foo can scan the whole term dictionary, and regular expressions can be slow. Elasticsearch has a setting named allow_expensive_queries, and when it is set to false, some of these expensive forms are rejected.
For many applications, a plain match or multi_match query combined with a bool query is clearer and safer than either. Reach for the string queries when you want a single user-supplied string to carry operators.
If you must use query_string with user text, you have two defensive options:
+ - = && || > < ! ( ) { } [ ] ^ " ~ * ? : \ /. Escape each with a backslash.simple_query_string or a match query.You can also validate a string without running the search using the validate API:
The response has "valid": false and an explanation of the problem. Note that simple_query_string would instead interpret what it can, so the same broken text would not produce an error.
simple_query_string. It never fails on malformed input and does not expose field prefixes, ranges or regex.
The string probably has a syntax problem such as an unbalanced quote or parenthesis, or an operator with nothing after it. Fix or escape the string, or use simple_query_string.
In query_string, operators must be uppercase to be treated as operators. Lowercase and is just another search term.
Yes. Both accept a fields array and per-field boosts such as title^2.
query_string gives you the full Lucene syntax with fields, ranges, wildcards and regex, but it fails on bad syntax and can run expensive queries. simple_query_string offers a small, forgiving operator set and is the right choice for user-typed search boxes. Pick based on who writes the string, escape input when you need the stricter parser, and consider match or bool queries when you do not need operators at all.