elasticsearch
Loading

Filter context

Serverless Stack

Use a Painless script as a filter in a query to include and exclude documents.

params (Map, read-only)
User-defined parameters passed in as part of the query.
doc (Map, read-only)
Contains the fields of the current document where each field is a List of values.
boolean
Return true if the current document should be returned as a result of the query, and false otherwise.

The standard Painless API is available.

To run the example, first install the eCommerce sample data.

This example filters documents where the average price per item in an order exceeds a minimum threshold of 30.

Tip

Filters answer the question “Does this document match?” with a simple “Yes or No” response. Use filter context for all conditions that don’t need scoring. Refer to Query and filter context to learn more.

GET kibana_sample_data_ecommerce/_search
{
  "query": {
    "bool": {
      "filter": {
        "script": {
          "script": {
            "source": """
                (doc['taxful_total_price'].value / doc['total_quantity'].value) > params.min_avg_price
            """,
            "params": {
              "min_avg_price": 30
            }
          }
        }
      }
    }
  }
}