4.6. Choosing the best query for the job

Now that we’ve covered some of the most popular Elasticsearch queries, let’s look at how to decide which queries to use and when. Although there’s no hard-and-fast rule for which query to use for what, table 4.3 helps you determine which query to use for the general case. Table 4.3. Which type of query to use for general use cases

Use case Query type to use
You want to take input from a user, similar to a Google-style interface, and search for documents with the input. Use a match query or the simple_query_string query if you want to support +/- and search in specific fields.
You want to take input as a phrase and search for documents containing that phrase, perhaps with some amount of leniency (slop). Use a match_phrase query with an amount of slop to find phrases similar to what the user is searching for.
You want to search for a single word in a not_analyzed field, knowing exactly how the word should appear. Use a term query because query terms aren’t analyzed.
You want to combine many different searches or types of searches, creating a single search out of them. Use the bool query to combine any number of subqueries into a single query.
You want to search for certain words across many fields in a document. Use the multi_match query, which behaves similarly to the match query but on multiple fields.
You want to return every document from a search. Use the match_all query to return all documents from a search.
You want to search a field for values that are between two specified values. Use a range query to search within documents with values between a certain range.
You want to search a field for values that start with a specified string. Use a prefix query to search for terms starting with a given string.
You want to autocomplete the value of a single word based on what the user has already typed in. Use a prefix query to send what the user has typed in and get back exact matches starting with the text.
You want to search for all documents that have no value for a specified field. Use the missing filter to filter out documents that are missing fields.