ElasticSearch Full Text Queries – Basic

There are many ways to query for things in Elasticsearch, depending on how the data is stored. In this tutorial, we’re gonna look at some different types of queries that Elasticsearch supports and try out some examples of how to use them.

Related Posts:
Elasticsearch Overview
ElasticSearch – Structure of a Search Request/Response
ElasticSearch Filter vs Query
Elasticsearch Multi Match Query – Basic
Elasticsearch Multi Match Query – More Practice

1. Match All Query

This is the most simple query that matches all documents. All _scores will be 1.0.

Boost the _score

If we want to change the value of _scores, just add boost parameter:

2. Match Query

This is a hash map containing the field we want to search as well as the text/numerics/dates value of the field to be search for.


If we want to search any field, use _all as the field:

Now, we can see the _score has changed in the Response:

The _score increases, that means it find the value to search in other fields (tags field).

Boolean Query behavior

By default, match query uses Boolean behavior as OR operator.
For example, if we search for “angular firebase”, Elasticsearch will search for “angular” OR “firebase”
–> match both “Angular 4 ElasticSearch” and “Introduction to Firebase”.

To search for results that contain both “angular” AND “firebase”, we just need to add operator like this:

So, the Response will not include documents which “title” do not contain both “angular” and “firebase”:

3. Match Phase Query

match_phrase query is useful when searching for a specific phrase within a document:
For example, with request:

We receive the response in which each document’s “title” must contain phase “angular 4 elasticsearch”:

What if we send a request match_phrase query for “angular elasticsearch”?
We will receive total 0 document in response.
So, what should we do to configure Elasticsearch for accepting string between “angular” and “elasticsearch” in the response?

Set distance between tokens

With some amount of leeway between the positions of each word, we add a parameter called slop, which is a number representing the distance between tokens in a phrase:

Now, the response is:

4. Match Phase Prefix Query

Similar to match_phrase query, the match_phrase_prefix query allows us to search for a phrase, but it also allows prefix matching on the last term in the phrase.

This behavior is useful for providing a autocomplete running for a search box, where the user gets search suggestions while typing a search term.

For example, if we send the request:

We will get the response:

Control execution time

Now look at query string “angular 4 e”. How it works:
– At first, it create a phrase query out of “angular” and “4” (term “angular” must exist and must be followed by the term “4”).
– Then it looks at the sorted term dictionary to find the first [max_expansions] terms that begin with “e”, and adds these terms to the phrase query.
– If the first [max_expansions] terms may not include the term “elasticsearch”, so the phrase “angular 4 elasticsearch” will not be found (this usually isn’t a problem as the user will continue to type more letters until the word they are looking for appears).

The default value of max_expansions is 50. We can configure to control the execution time of the query:

By grokonez | October 26, 2017.

Last updated on August 16, 2018.

Related Posts

Got Something To Say:

Your email address will not be published. Required fields are marked *