Elasticsearch Elasticsearch Match Query Example

By Opster Team

Updated: Aug 28, 2023

| 2 min read

Quick links

Introduction

Elasticsearch Match Query is a high-level, full-text search API that is designed to handle a wide range of search requirements. It is a versatile tool that can be used to perform both exact and fuzzy matching, making it an essential part of any Elasticsearch user’s toolkit. In this article, we will delve into the intricacies of the Elasticsearch Match Query, providing examples to illustrate its usage and capabilities.

Understanding the basics of Match Query

The Match Query is a standard method for full-text search in Elasticsearch. It operates by analyzing the query string and using it to perform a search against a specific field. The Match Query is capable of handling a variety of search types, including phrase matching, proximity matching, and fuzzy matching.

Let’s start with a basic example of a Match Query:

GET /_search
{
  "query": {
    "match": {
      "field": "text"
    }
  }
}

In this example, the Match Query is searching for the term “text” in the field “field”. If the term is found, the document containing it will be returned in the search results.

Controlling boolean logic with operators

The Match Query also supports operator usage. The “operator” parameter can be set to “and” or “or” to control the boolean logic for the query. For example:

GET /_search
{
  "query": {
    "match": {
      "field": {
        "query": "quick brown fox",
        "operator": "and"
      }
    }
  }
}

In this example, the Match Query will only return documents that contain all three terms: “quick”, “brown”, and “fox”.

Implementing fuzziness in Match Query

The Match Query also supports fuzziness, which allows for the matching of terms that are similar, but not exactly the same as the query term. This can be useful for handling typos or variations in spelling. Here’s an example:

GET /_search
{
  "query": {
    "match": {
      "field": {
        "query": "quikc",
        "fuzziness": "AUTO"
      }
    }
  }
}

In this example, the Match Query will return documents that contain terms similar to “quikc”, such as “quick”.

Using the “minimum_should_match” parameter

The Match Query also supports the “minimum_should_match” parameter, which allows you to specify the minimum number of terms that must match for a document to be considered a match. For example:

GET /_search
{
  "query": {
    "match": {
      "field": {
        "query": "quick brown fox jumps",
        "minimum_should_match": "75%"
      }
    }
  }
}

In this example, at least three out of the four terms (“quick”, “brown”, “fox”, “jumps”) must match for a document to be returned in the search results.