Elasticsearch – Full Text Queries

In the previous section, we went through a number of term level queries, which are used for exact matches. While we can use term level queries for searching for long text fields, that usually not something we want to do. Because term level queries are used for exact matches.
I have created an index named recipe and the data which is stored in that index looks like this

Can insert this data and check the mapping style using

Please download the test data file from this link
https://drive.google.com/file/d/1PMvJCQJlA_jxr-WdzWpIFDt9emUwUUhM/view?usp=sharing

Let us see the first query which is a match query, it’s a very powerful and simple query but it allows very flexible matching. When dealing with search queries that are coming from text input the match query is better suited.

The match query is a boolean query, what happens internally is that the terms are used for the boolean query with a default operator of or. Since the default operator is or, all terms do not need to appear in the fields that we were searching. The below query is almost like above but with a small difference, it has an operator key. By default it’s or but we changed it to “and”.

This might be useful in other cases, where we can define a query ahead of time, then this could indeed be useful. Such as when embedding queries within code, and not using user input for the query.

Change the query and we will see we got few matched documents. And if we remove “or” remove the from the query, the result will have remained the same. Because both terms appear in the title field. What I wanted to show you that we are still searching for terms, and not for an entire sentence, so that’s why the document still matches. Remember that the query we enter for the match query is analyzed by using the analyzer that is specified within the field mapping. This is, of course, different than the term level queries, which are not analyzed.

Match Phrases
The above match queries operate on terms. By default, documents were matched if any of the terms appear within the field that we were searching for. In match query, it doesn’t matter in which order the term appear in or they’re any other term between two matching terms. But what if we want to match phrases, i.e. terms in a specific order. That can easily be done by match_phrase, this query matches phrases, i.e. sequences of words or more formally terms. But if you change the order of the query it will not return.

Searching Multiple Fields
So far we have been searching for terms in a single field, let’s see how to search multiple fields with in the same query. By using a query multi_match, with this we can specify an array of fields to search through.