concept query_string in category elasticsearch

appears as: query_string
Elasticsearch in Action

This is an excerpt from Manning's book Elasticsearch in Action.

In plain English, this translates to “run a query of type query_string, where the string is elasticsearch.” It might seem like too much boilerplate to type in elasticsearch, but this is because JSON provides many more options than a URI request. As you’ll see in chapter 4, using a JSON query makes sense when you start to combine different types of queries: squeezing all those options in a URI would be more difficult to handle. Let’s explore each field.

In the previous sections of this chapter, a number of queries and filters were already introduced. You started with the match_all query to return all documents, moved on to the match query to limit results of an occurring word in a field, and used the term filter to limit the results using a term in a field. One query that we didn’t discuss but that you did use is the query_string query. This query was used in the URL-based search. More on this later in this section.

In chapter 2, you used the query_string query to see how easy it is to get an Elasticsearch server up and running, but we’ll cover it again in more detail so you can see how it compares to the other queries.

As shown in the following listing, a query_string search can be performed either from the URL of the request or sent in a request body. In this example, you search for documents that contain “nosql.” The query should return one document.

Listing 4.15. Example query_string search

By default, a query_string query searches the _all field, which, if you recall from chapter 3, is made up of all the fields combined. You can change this by either specifying a field with the query, such as description:nosql, or by specifying a default_field with the request, as shown in the next listing.

Listing 4.16. Specifying a default_field for a query_string search