ElasticSearch - Full Text Query - Match Query

In this blog, I will demonstrate the use of ElasticSearch Match query with examples. 

Introduction

The match query is one of the most basic and commonly used queries in Elasticsearch for performing a full-text search. The query enables you to search text, number, date, or boolean types of values in a field.

The match query is a boolean type query. It means the query analyzes the provided text before performing a search and the analysis process create boolean query from the given text. The operator flag can be set to or or and to control the boolean clauses (defaults to or). 

It is also including options for fuzzy matching.

We can set a threshold for a minimum amount of matching words that the document must contain. We can use minimum_should_match parameter for the same.

Let's understand the query result with some examples.

The mapping for our index is defined as follows,

PUT companies/_mapping
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "about_us": {
        "type": "text"
      }
    }
  }
}

Sample documents in our index.

[
  {
    "name": "Google",
    "about_us": "American multinational technology company focusing on search engine technology"
  },
  {
    "name": "Microsoft",
    "about_us": "Multinational american technology company producing computer software"
  },
  {
    "name": "Apple",
    "about_us": "Technology company in America having headquartered in Cupertino, California, United States"
  },
  {
    "name": "Noice",
    "about_us": "Indian brand for producing and distributing wearables and other such products"
  }
]

Let us search the word “technology” contained in the field “about_us” in the documents.

PUT companies/_search
{
  "query": {
    "match": {
      "about_us": {
        "query": "technology"
      }
    }
  }
}

The above query will return the first 3 documents out of 4. Because only 3 documents contain the word “technology” in “about_us” field.

If we want to search for more than one word. Using the same query we just performed, ex. “multinational technology company” as below.

PUT companies/_search
{
  "query": {
    "match": {
      "about_us": {
        "query": "multinational technology company"
      }
    }
  }
}

The above query still returns the same document as before because by default Elasticsearch use OR operator in the search query to treat each word. The query will match any document which contains “multinational” OR “technology” OR “company”. 

Query with Operator Parameter

We can change the default behavior of the OR operator in the "match" query.

We can specify the operator parameter with "OR" or "AND" values.

Let’s check what happens when we provide the operator parameter “AND” in the above query.

PUT companies/_search
{
  "query": {
    "match": {
      "about_us": {
        "query": "multinational technology company",
        "operator" : "AND"
      }
    }
  }
}

From the above query, the results will return 2 documents (documents 1 and 2) since they are the only documents containing all three search keywords in the “about_us” field.

minimum_should_match

We can set a threshold for a minimum amount of matching words that the document must contain. For example, if we set minimum_should_match parameter to 1, the query will check for any documents with a minimum of 1 matching word.

If we set the “minium_should_match” parameter to 1, then all three words must appear in the document for search.

PUT companies/_search
{
  "query": {
    "match": {
      "about_us": {
        "query": "multinational technology company",
        "minimum_should_match": 1
      }
    }
  }
}

From the above query, the results will return 3 documents (documents 1, 2, and 3) as those records contain all three words.

Summary

This blog has described Match query of ElasticSarch with different types of parameters.

I hope you will find this blog helpful. If you have any suggestions, then please feel free to ask in the comment section.

Thank you.