Elastic Search

What is Fuzzy Query in Elasticsearch?

Elasticsearch is a NoSQL distributed database and analytical engine that is utilized to store and manage different kinds of data in JSON format. Data includes unstructured, semi-structured, structured and bulky, and raw data. Elasticsearch stores and manages these types of data in documents in JSON format.

To perform different operations on data, it uses its own Query DSL and rest APIs. Elasticsearch permits us to specify the DSL query based on data and its JSON format. Query DSL contains different types of queries and a “fuzzy” query is one of them.

This article elaborates on:

What is a “fuzzy” Query?

A fuzzy query is one of the DSL queries that search and perform other operations on data based on “Levenshtein edit distance”. It measures the difference between terms and searches for the result that matches or is closer to a specified or searched term.

Suppose a string “Hello World”, if a user is searching for the word “Hello” in a string. He/she will apply a fuzzy query and pass the value “ello” to search. The fuzzy query will search for a word that is similar to the searched word and returns the results as “true”. For the practical demonstration, follow the next section.

How to Use a “fuzzy” Query for Search in Elasticsearch?

To use the fuzzy query for searching a word that is similar or closer to the searched word, follow the provided steps.

Step 1: Start Elasticsearch

First, start the Elasticsearch database. For doing so, first, navigate to its bin directory:

cd C:\Users\Dell\Documents\Elk stack\elasticsearch-8.9.0\bin

Next, execute the “elasticsearch.bat” file to run the Elasticsearch:

elasticsearch.bat

Elasticsearch will be executed when its cluster health turns to “YELLOW”:

Step 2: Start Kibana

After that, run the Kibana. Similarly, navigate to its bin directory. Then, execute the “kibana.bat” file:

cd C:\Users\Dell\Documents\Elk stack\kibana-8.9.0\bin

kibana.bat

This will start the Kibana on the system:

Step 3: Sign in to Kibana

Now, open the Kibana default URL “localhost:5601” add the Elasticsearch login information and press the “Log in” button to access Kibana UI:

Step 4: Open Kibana Console

In the next step, open the Kibana menu and hit the “Dev Tools” option to launch its console:

Step 5: Execute “fuzzy” Query

To make a search, utilize the “GET <indexname>/_search>” API request. Also, specify the “fuzzy” query to get results that are similar or closer to the searched query. For illustration, let’s make a search for the document that has “Designation” equal to “Author” as shown below:

GET linuxhint/_search

{

"query": {

"fuzzy": {

"Designation": "Author"

}

}

}

The below output shows that a document having id “1” matches our searched query:

Let’s take another example for better understanding and make a search that finds a closer match. For this purpose, we have applied 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"

}

}

}

Here, you can see fuzzy query matches to document having id “1” in which the Designation value is “Author” and the fuzzy query returns the closer match:

That is all about the Elasticsearch “fuzzy” query.

Conclusion

A fuzzy query is a DSL query that performs a search on data based on “Levenshtein edit distance”. It measures the difference between terms and searches for the result that matches or is closer to a searched term. This article has elaborated on what is a “fuzzy” query and how to use it for searching.

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.