Elasticsearch Filtered Query

Elasticsearch is an open-source search and analysis engine using the Apache Lucene library. It is the most successful search engine, which was launched in 2010. It is developed in Java, supporting customers in a wide range of languages, like Python, PHP, C#, and Ruby. Elasticsearch makes it possible to search for all types of data. It offers a scalable search solution, and has almost real-time search and can be accessed from any location. Elasticsearch receives unstructured data from various locations, stores and indexes them based on the mapping specified by the user that may also be derived from data automatically and makes it searchable.

Elasticsearch Filtered Query - Table of Content

With its distributed architecture, huge volumes of data can be searched and analyzed in almost real-time. It enables you to begin with a single machine and slowly scale to hundreds. Elasticsearch facilitates the execution of a complete search cluster, although its large-scale execution still requires a considerable amount of expertise. In addition to the use cases of full-text search such as document search, product search, email search, etc. Elasticsearch is commonly used to store data that must be diced and sliced, grouped by different dimensions, and so on. Examples of such analytics use cases include Elasticsearch's use for logs, traces, metrics, and other time-series data. In this blog let us learn about Elasticsearch Filtered query.

Elasticsearch sorts search will result with relevance score By default, that measures the extent to which every document corresponds to a query. Relevance Score is the positive floating number which will return in the search APIs metadata field _score. The greater the _score, the greater the relevance of the document. Although every query type may compute relevance scores in various ways, the calculation of scores also depends on where the query clause is executed, whether in a filter or query context.

     Become a ElasticSearch Certified professional  by learning ElasticSearch Certification Course from hkrtrainings! 

Query Context:

In the context of the query, a query clause responds to the question "To what extent does this document comply with this query clause?". In addition to determining whether the document matches or not, the query clause also computes the relevance score in the metadata field _score. The query context comes into effect when the query clause is passed to the query parameter, like the query parameter within the search API. 

ElasticSearch Training

  • Master Your Craft
  • Lifetime LMS & Faculty Access
  • 24/7 online expert support
  • Real-world & Project Based Learning

Filter Context: 

In the context of a filter, a request clause responds to the question, "Does this document comply with the query clause?" The response will be a simple Yes or No - there are no scores calculated. The context of the filters is primarily used to filter structured data. Commonly used filters are automatically cached by Elasticsearch in order to accelerate performance.

The filter context applies each time the query clause passes the filter parameter, like the must_not or filter parameters within bool query, and constant_score query, or filter aggregation.

The following are the query clause examples that are used in the filter or query context in the search API. This query will be for documents that meet all of the following requirements:

The title field includes the word 'search'.

The content field includes the word 'elasticsearch.

The status field includes the word 'published'.

The publish_date field includes the dates from Jan 1st 2015.

GET / _search


"query": { 

    "bool": { 

      "must": [

        { "match": { "title": "Search" }},

        { "match": { "content": "Elasticsearch" }}


      "filter": [ 

        { "term": { "status": "published" }},

        { "range": { "publish_date": { "gte": "2015-01-01" }}}




  • Here The query parameter specifies the query context.
  • Bool and the two-match clauses are used within the query context, meaning they are used to evaluate the extent to which the document matches.
  • The filter parameter refers to the filter context. Their term and range clauses are used within the filter context. They will screen out documents which will not match; however, they will not have an impact on the score of the documents that are matching.

                                                                 Lets's get started with ElasticSearch Tutorial online !

Subscribe to our youtube channel to get new updates..!

Queries and filters: 

The difference between filters and queries may initially lead to confusion. Particularly when we attempt to link terminology to the query languages to other data sources like SQL. In ElasticSearch, a query is anything that identifies whether the documents meet a given criterion and, to produce a score from 0 to 1 that indicates the extent to which it achieves this. For example, if we ask ElasticSearch to index "The five boxing wizards", it will index it by default like ["the", "five", "boxing", "Wizards"]. If we search with a query which searches for the term "boxing", the indexed string will be matched; however, the score will not be one because a single word matches.

In addition, a filter ignores scoring. It will only identify whether or not a document will match the filter. With the help of filters, we may restrict the search result to documents which meet the given criterion without having to change the score. Moreover, since ElasticSearch does not have to worry about the rating of the filters, they can be cached and are faster. Hence, one general rule is to use filters always unless you need the results to be sorted by relevance based on the query.

To filter without a query:

When a request is not specified, it will default to the match_all query. It indicates that the filtered query may be used to just wrap around a filter so that it may be used anywhere a query is expected. In the previous example, we restrict the results of a query to a filter. However, what if at all we are looking for is a filter? For this kind of instance, we always use the query property within the body of the query, which waits for a query. However, we cannot simply add a filter; we have to wrap it in some kind of query. A solution to this is to change our existing query by substituting the query_string query with a match_all query in a filtered query that is a query which just answers everything.

                                                                                                                                                      [Related Article: Elasticsearch Filters]

ElasticSearch Training

Weekday / Weekend Batches


In this blog, we have learned about Elasticsearch Filtered Query. We hope you found this information useful. If you are searching for any topic related to Elastic search, do not hesitate to comment below. We would revert with that topic.

Other Blogs:

Find our upcoming ElasticSearch Training Online Classes

  • Batch starts on 2nd Jun 2023, Fast Track batch

  • Batch starts on 6th Jun 2023, Weekday batch

  • Batch starts on 10th Jun 2023, Weekend batch

Global Promotional Image


Request for more information

Research Analyst
As a senior Technical Content Writer for HKR Trainings, Gayathri has a good comprehension of the present technical innovations, which incorporates perspectives like Business Intelligence and Analytics. She conveys advanced technical ideas precisely and vividly, as conceivable to the target group, guaranteeing that the content is available to clients. She writes qualitative content in the field of Data Warehousing & ETL, Big Data Analytics, and ERP Tools. Connect me on LinkedIn.