Elasticsearch | Differences between Queries and Filters

09 Aug, 2021
Elasticsearch is a full-text search and analytics engine based on Apache Lucene. Elasticsearch makes it easier to perform data aggregation operations on data from multiple sources and to perform unstructured queries such as fuzzy searches on the stored data. 
Elasticsearch document would look like:  
{
  "first_name": " Kumar",
  "last_name":"Gaurav",
  "email":"kumar@gmail.com",
  "dob":"04-11-1995",
  "country":"India"
}
Before the version of 2, the Elasticsearch perform Queries and Filters separately, after that they combined both and produce more effective search results. So first we discuss both the topics and after that, we will compare them to each other. 
Queries: A query calculates how relevant each document is to the query, and assigns it a relevance score, which is later used to sort matching documents by relevance. This concept of relevance is well suited to full-text search, where there is seldom a completely “correct” answer. The query also asks the question like below: 
 
How well does this document match?
What is the created adate of Elasticsearch?
What is the distance of lat_lon field from the specified point?
A typical use for a query is to find documents best matching the words full-text search, containing the word run, but maybe also matching runs, running, jog, or sprint and containing the words quick, brown, etc. 
Filters: The output from most filter clauses is a simple list of the documents that match the filter. It is quick to calculate and easy to cache in memory, using only 1 bit per document. These cached filters can be reused efficiently for subsequent requests. A filter asks a yes or no question of every document and is used for fields that contain exact values:  
Is the created date in the range 2013 – 2014?
Does the status field contain the term published?
Is the lat_lon field within 10km of a specified point?
Note: Query clauses and filter clauses are similar in nature but have slightly different purposes. 
Important points: Queries are not only find matching documents but also calculate how relevant each document is, which typically makes queries heavier than filters. Also, query results are not cachable. Thanks to the inverted index, a simple query that matches just a few documents may perform as well or better than a cached filter that spans millions of documents. In general, however, a cached filter will outperform a query and will do so consistently. The goal of filters is to reduce the number of documents that have to be examined by the query 
Differences between Queries and Filters:  
Note: After version 2 Query and Filter are combined to perform better results, run a query then set a filter will help you to get the actual result that you want. 
abhishek0719kadiyan
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
How to fetch data from an API in ReactJS ?
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
REST API (Introduction)
Remove elements from a JavaScript Array
How to set the default value for an HTML <select> element ?
How to set input type date in dd-mm-yyyy format using HTML ?
How to Open URL in New Tab using JavaScript ?
File uploading in React.js