Skip to main content

Filters

filter is an array of Elasticsearch query clauses. Documents must match all of them.

await orders.aggregate({
size: 50,
filter: [
{ term: { 'status.keyword': 'paid' } },
{ range: { total: { gte: 50 } } }
]
})

The Insights app sends exactly one element, a bool clause:

"filter": [
{ "bool": {
"must": [ { "terms": { "status.keyword": ["paid", "shipped"] } } ],
"must_not": [ { "term": { "category.ngram": "merch" } } ]
} }
]

must clauses need to match, must_not clauses must not. Because the value is passed to Elasticsearch, any Elasticsearch query works, including bool.should for OR conditions.

Filters do not affect the field list

filter only decides which documents take part. Use fields to choose what is returned, and aggs for grouping.

What the Insights app builds​

The filter bar of the app offers the operators below. This table shows the clause each one produces, so you can write the same by hand. f is the field, f.keyword its exact-match form (see field names).

Text fields (string)​

OperatorClause
contains{ "term": { "f.ngram": "value in lowercase" } }
does not containthe same clause in must_not
starts with{ "wildcard": { "f.wildcard": { "value": "abc*", "case_insensitive": true } } }
ends with{ "wildcard": { "f.wildcard": { "value": "*abc", "case_insensitive": true } } }
is (one of){ "terms": { "f.keyword": ["a", "b"] } }
is not (one of)the same clause in must_not
is empty{ "exists": { "field": "f" } } in must_not
is not empty{ "exists": { "field": "f" } } in must
How "contains" works

Contains searches a n-gram index of the value (lowercased, up to 20 characters). It finds substrings of single words up to 20 characters. Longer text or matches across spaces are not found this way: use wildcard (*text*) on f.wildcard, or a match query, for those.

Numbers (integer, float)​

OperatorClause
={ "term": { "f": 42 } }
!=the same in must_not
>, >=, <, <={ "range": { "f": { "gt": 42 } } } (gte, lt, lte)
is empty / is not emptyexists, as above

Dates (date-iso, timestamp-*)​

OperatorClause
after, on or after, before, on or before{ "range": { "f": { "gt": "2026-08-01T00:00:00Z" } } } (gt, gte, lt, lte)
equals{ "term": { "f": "2026-08-14T09:30:00Z" } }
this / last / … hour, day, week, month, quarter, yeara range with gte and lte, see below
is empty / is not emptyexists

Relative date ranges ("last month", "this week"). The Insights app computes the start and end of the interval in the browser's time zone and sends them as timestamps. In your own code, you can either do the same, or use Elasticsearch date math, which is evaluated in UTC on the server:

{ "range": { "orderedAt": { "gte": "now-1M/M", "lte": "now-1M/M" } } }

now-1M/M means "one month ago, rounded to the month". With gte it is the start of that month, with lte the end of it, so the range covers last month completely. Units: h, d, w, M, y. Quarters have no date-math unit: send explicit timestamps.

Booleans​

OperatorClause
is true{ "term": { "f": true } }
is false{ "term": { "f": true } } in must_not. This includes documents where the field is missing

Locations (geo-point)​

The app offers is empty / is not empty. From code, use Elasticsearch geo queries:

{ "geo_distance": { "distance": "10km", "location": { "lat": 53.55, "lon": 9.99 } } }
{ "geo_bounding_box": { "location": { "top_left": { "lat": 54, "lon": 9 }, "bottom_right": { "lat": 53, "lon": 10 } } } }

Search box (quick filter)​

Tables offer a search box across several fields. For each word typed, at least one of the fields must contain it:

{ "bool": { "must": [
{ "bool": { "should": [
{ "term": { "product.ngram": "espr" } },
{ "term": { "category.ngram": "espr" } }
], "minimum_should_match": 1 } }
] } }

OR, NOT and nesting​

The Insights app combines its filter items with AND only. Elasticsearch gives you more:

"filter": [
{ "bool": {
"should": [
{ "term": { "status.keyword": "cancelled" } },
{ "term": { "status.keyword": "refunded" } }
],
"minimum_should_match": 1
} }
]

Empty and invalid conditions​

The app silently drops a filter item if its field does not exist in the source, or if it has no value (except is empty / is not empty). Do the same in your code so a half-filled filter bar does not break a query. The helper in From a chart to code does this.

Which operators fit which type​

TypeOffered operators
stringcontains, does not contain, starts with, ends with, is empty, is not empty, is, is not
integer, float=, !=, >, >=, <, <=, is empty, is not empty
date typesthis/last interval, after, on or after, before, on or before, is empty, is not empty
booleanis, is not
geo-pointis empty, is not empty

Dashboard filters​

Dashboard filters (the filter widget) are added to the filter of every chart on the dashboard, combined with AND. A dashboard filter is applied to a chart if the chart's source has a field with the same name, otherwise it is skipped. Reproduce it by appending the filter items to the chart's own filter items before building the request.

Customers (customers-entities)​

Same clauses, with data.-prefixed field names. To restrict to one entity type add { "term": { "entityType.keyword": "ou" } }. The Insights app additionally applies the user's group permissions there. See Customers as a source.