The query_string query lets an Elasticsearch search accept a compact expression such as field:value combined with operators. It is useful when translating URI-style searches or operator-based search input into a Query DSL request body. section 8.6.4 section 10.9
Find Bert’s Java books released after 2000
Put the expression in the query parameter. Each name-value pair targets a field, and AND joins the required conditions. section 10.9
Listing 10.20 Creating a query string with operators
GET books/_search
{
"query": {
"query_string": { #1
"query": "author:Bert AND edition:2 AND release_date>=2000-01-01" #2
}
}
}
This request combines author, edition, and date conditions in one query_string expression. section 10.9
When an expression omits field names, Elasticsearch can search broadly. Restrict the search with fields, or set default_field to direct unqualified terms such as Patterns to a chosen field like title. section 10.9.1
Listing 10.22 Specifying fields explicitly in a query_string query
GET books/_search
{
"query": {
"query_string": {
"query": "Patterns", #1
"fields": ["title","synopsis","tags"] #2
}
}
}
Use fields to limit an unqualified term to title, synopsis, and tags instead of leaving field selection implicit. section 10.9.1
Multiple bare terms use OR by default, so Design Patterns can match either term. Set default_operator to AND when both terms must be present. For an ordered phrase, enclose it in escaped quotes; phrase_slop can allow a missing word. section 10.9.2 section 10.9.3
Listing 10.26 A query_string query with a phrase and phrase_slop
GET books/_search
{
"query": {
"query_string": {
"query": "\"making code better\"", #1
"default_field": "synopsis",
"phrase_slop": 1 #2
}
}
}
The quoted phrase searches synopsis, while phrase_slop: 1 permits one missing word in the phrase. section 10.9.3
NOTE: query_string syntax is strict. An illegal quote or other malformed input raises an exception rather than being ignored. Use simple_query_string when syntax errors should be suppressed, and prefer other Query DSL queries unless a query string is specifically needed. section 10.11 section 10.13
