Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Elasticsearch | Differences between Queries and Filters

  • Last Updated : 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: 
 

QueriesFilters
  
Queries are slower it returns a calculated score of how well a document matches the query.Filters are faster because they check only if the document matched or not.
Queries produce non-boolean values.Filters produce boolean values.
Using filters after performing a Query is faster compare to others.But using a Query after filter is not worth it.
Queries are not cacheable.Filters are cacheable.

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.
 

My Personal Notes arrow_drop_up
Recommended Articles
Page :