ElasticSearch fuzzy query can be used in scenarios when the user searches with mistyped keywords or misspellings. Alternatively, it can also be used for performing the search for similar words based on Levenshtein Edit Distance, which can be defined as the minimum number of single-character edits (insertions, deletions or substitutions) required to change one word into the other.
In this post, Fuzzy Search using ElasticSearch Java API is demonstrated. Some of the following points are covered:
First and foremost, get set up with ElasticSearch and Kibana. For Windows environment, refer to my post on Getting Started with ElasticSearch and Kibana on Windows
Create a Java Maven project. Put the following in pom.xml file for working with ElasticSearch Java APIs:
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>6.2.2</version> </dependency> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20180130</version> </dependency>
Pay attention to some of the following which is required for using fuzzy query for search the index:
public class App { private static final String INDEX_NAME = "recruitment"; private static final String INDEX_TYPE = "interviews"; public static void main(String[] args) throws IOException { // // Create an instance of TransportClient // TransportClient client = = new PreBuiltTransportClient(Settings.EMPTY) .addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9300)); // // Create a query builder using fuzzyQuery Method // Name of the key to search: name // Value to search: "vitalflux" // QueryBuilder queryBuilder = QueryBuilders.fuzzyQuery(name, "vitalflux").boost(1.0f).prefixLength(0).fuzziness(Fuzziness.ONE).transpositions(true); // // Create an instance of SearchRequestBuilder // SearchRequestBuilder requestBuilder = client.prepareSearch(INDEX_NAME).setTypes(INDEX_TYPE) .setQuery(queryBuilder).setSize(100); // // Get the search result // SearchResponse response = requestBuilder.get(); // // Iterate through search results // SearchHit[] srchHits = response.getHits().getHits(); String[] result = new String[srchHits.length]; int i = 0; for (SearchHit srchHit : srchHits) { result[i++] = (String) srchHit.getSourceAsMap().get(KEY_NAME); } } }
The following code can be used to build the QueryBuilder instance with Match Query API which is later used to build the instance of SearchRequestBuilder. The rest of the code remains same as above code.
QueryBuilder queryBuilder = QueryBuilders.matchQuery("name", "vitalflux").fuzziness(Fuzziness.ONE).boost(1.0f).prefixLength(0).fuzzyTranspositions(true); // // Create an instance of SearchRequestBuilder // SearchRequestBuilder requestBuilder = client.prepareSearch(INDEX_NAME).setTypes(INDEX_TYPE).setQuery(queryBuilder).setSize(100); // // Get the search result // SearchResponse response = requestBuilder.get();
The following code can be used to build the QueryBuilder instance with Bool Query API which is later used to build the instance of SearchRequestBuilder. The rest of the code remains same as above code.
QueryBuilder queryBuilder = QueryBuilders.matchQuery(KEY_NAME, refNumber).fuzziness(editDistance).boost(1.0f).prefixLength(0).fuzzyTranspositions(true); // // Create Bool Query Builder // final QueryBuilder boolQueryBuilder = QueryBuilders.boolQuery().must(fuzzyQueryBuilder); // // Create an instance of SearchRequestBuilder // SearchRequestBuilder requestBuilder = client.prepareSearch(INDEX_NAME).setTypes(INDEX_TYPE).setQuery(boolQueryBuilder).setSize(100); // // Get the search result // SearchResponse response = requestBuilder.get();
In this post, you learned about using fuzzy query with ElasticSearch using Java APIs.
Did you find this article useful? Do you have any questions or suggestions about this article in relation to doing fuzzy search using ElasticSearch Java APIs? Leave a comment and ask your questions and I shall do my best to address your queries.
Artificial Intelligence (AI) agents have started becoming an integral part of our lives. Imagine asking…
In the ever-evolving landscape of agentic AI workflows and applications, understanding and leveraging design patterns…
In this blog, I aim to provide a comprehensive list of valuable resources for learning…
Have you ever wondered how systems determine whether to grant or deny access, and how…
What revolutionary technologies and industries will define the future of business in 2025? As we…
For data scientists and machine learning researchers, 2024 has been a landmark year in AI…