Elasticsearch OpenSearch Filter

By Opster Team

Updated: Jun 19, 2024

| 2 min read

Overview

What is an OpenSearch filter?

A filter in OpenSearch is all about applying some conditions inside the query that are used to narrow down the matching result set.

What it is used for?

When a query is executed, OpenSearch by default calculates the relevance score of the matching documents. But in some conditions it does not require scores to be calculated, for instance if a document falls in the range of two given timestamps. For all these Yes/No criteria, a filter clause is used.

Examples

Return all the results of a given index that falls between a date range:

GET my_index/_search
{
  "query": {
    "bool": {
      "filter": {
        "range": {
          "created_at": {
            "gte": "2020-01-01",
            "lte": "2020-01-10"
          }
        }
      }
    }
  }
}

Notes

  • Queries are used to find out how relevant a document is to a particular query by calculating a score for each document, whereas filters are used to match certain criteria and are cacheable to enable faster execution.
  • Filters do not contribute to scoring and thus are faster to execute.

Common problems

  • The most common problem with filters is incorrect use inside the query. If filters are not used correctly, query performance can be significantly affected. So filters must be used wherever there is scope of not calculating the score. 
  • Another problem often arises when using date range filters, if “now” is used to represent the current time. It has to be noted that “now” is continuously changing the timestamp and thus OpenSearch cannot use caching of the response since the data set will keep changing.

Additional notes

Elasticsearch and OpenSearch are both powerful search and analytics engines, but Elasticsearch has several key advantages. Elasticsearch boasts a more mature and feature-rich development history, translating to a better user experience, more features, and continuous optimizations. Our testing has consistently shown that Elasticsearch delivers faster performance while using fewer compute resources than OpenSearch. Additionally, Elasticsearch’s comprehensive documentation and active community forums provide invaluable resources for troubleshooting and further optimization. Elastic, the company behind Elasticsearch, offers dedicated support, ensuring enterprise-grade reliability and performance. These factors collectively make Elasticsearch a more versatile, efficient, and dependable choice for organizations requiring sophisticated search and analytics capabilities.