Elastic Search

What is the Difference Between fuzzy Query and match Query?

Elasticsearch is a well-known, free, open-source, analytical, and distributed search engine and database that is utilized to manage various types of data. It is purely a NoSQL database and works differently from typical SQL and relational databases. Elasticsearch uses query DSL ((Domain Specific Language) and REST APIs to manage and analyze the data. Different types of queries exist in Elasticsearch such as fuzzy query, match query, wild card query, prefix query, and many more. These queries work differently from each other.

This post demonstrates on:

What is a “fuzzy” Query?

The “fuzzy” query is a Query DSL that performs search other operations such as changing the character, inserting the character, or removing the character based on “Levenshtein edit distance” distance. It usually finds the difference between terms and returns the result in a document that is nearer or almost similar to the searched term.

What is a “match” Query?

The “match” query is another type of Query DSL that is used to match or search the given data such as string, number, or text. It is a “full-text” based query and performs a full-text search and returns the results that exactly match the searched term. If the term does not match the search term it will return a null string or false.

Difference Between “fuzzy” and “match” Query

Both “fuzzy” and “match” queries are used for searching purposes. The key difference between these two queries is that the “fuzzy” query makes a fuzzy search and returns a result that is similar to or close to the searched term. In contrast, the “match” query returns the results that exactly match the searched term.

For better understanding, follow the below-provided examples:

Example 1: Fuzzy Search Using “fuzzy” Query

Suppose, the user wants to find the document that has the “Designation” value as “Author”. Let’s make a search that finds a closer match. To do so, utilize the “fuzzy” query to search a document that has a “Designation” value equal to or closer to “uthor”:

GET linuxhint/_search

{

"query": {

"fuzzy": {

"Designation": "uthor"

}

}

}

The below output shows that the document having id “1” has a “Designation” value close to the “uthor” searched term:

But if the above example is applied by the “match” query, it will send a “null string” as it returns the exact matching term.

Example 2: Fuzzy Search Using “Match” Query

Let’s take the same example and apply the “match” query to find the document having the “Designation” value as “uthor”:

GET linuxhint/_search

{

"query": {

"match": {

"Designation": "uthor"

}

}

}

The below output shows that the “match” query does not find closer results and returns a “null” string:

Let’s modify the searched value from “uthor” to “Author” and run the “match” query as shown below:

GET linuxhint/_search

{

"query": {

"match": {

"Designation": "Author"

}

}

}

Here, you can see the “match” query returns the document having id “1”. Because document 1 exactly matches the searched terms:

That is all about the difference between a “fuzzy” query and a “match” query.

Conclusion

The “fuzzy” query is used to make a fuzzy search and returns the results that closely match the searched term. However, the “match” query does not support fuzzy search and returns the results that exactly match the searched term. This blog has illustrated the difference between fuzzy query and match query.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.