2018-12-13 14:47:20 -05:00
---
2019-08-21 00:48:59 -04:00
id: filters
2020-04-15 19:12:20 -04:00
title: "Query filters"
sidebar_label: "Filters"
2018-12-13 14:47:20 -05:00
---
2018-11-13 12:38:37 -05:00
<!--
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you under the Apache License, Version 2.0 (the
~ "License"); you may not use this file except in compliance
~ with the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->
2020-04-15 19:12:20 -04:00
> Apache Druid supports two query languages: [Druid SQL](sql.md) and [native queries](querying.md).
> This document describes the native
> language. For information about aggregators available in SQL, refer to the
> [SQL documentation](sql.md#scalar-functions).
2018-12-12 23:42:12 -05:00
2020-01-03 12:33:19 -05:00
A filter is a JSON object indicating which rows of data should be included in the computation for a query. It’ s essentially the equivalent of the WHERE clause in SQL. Apache Druid supports the following types of filters.
2015-05-05 17:07:32 -04:00
2020-06-30 22:52:40 -04:00
**Note**
Filters are commonly applied on dimensions, but can be applied on aggregated metrics, for example, see [filtered-aggregator ](./aggregations.md#filtered-aggregator ) and [having-filters ](./having.md ).
2015-05-05 17:07:32 -04:00
### Selector filter
The simplest filter is a selector filter. The selector filter will match a specific dimension with a specific value. Selector filters can be used as the base filters for more complex Boolean expressions of filters.
The grammar for a SELECTOR filter is as follows:
``` json
"filter": { "type": "selector", "dimension": < dimension_string > , "value": < dimension_value_string > }
```
This is the equivalent of `WHERE <dimension_string> = '<dimension_value_string>'` .
2016-04-01 20:18:27 -04:00
The selector filter supports the use of extraction functions, see [Filtering with Extraction Functions ](#filtering-with-extraction-functions ) for details.
2017-03-23 21:23:46 -04:00
### Column Comparison filter
The column comparison filter is similar to the selector filter, but instead compares dimensions to each other. For example:
``` json
"filter": { "type": "columnComparison", "dimensions": [< dimension_a > , < dimension_b > ] }
```
This is the equivalent of `WHERE <dimension_a> = <dimension_b>` .
2019-08-21 00:48:59 -04:00
`dimensions` is list of [DimensionSpecs ](./dimensionspecs.md ), making it possible to apply an extraction function if needed.
2017-03-23 21:23:46 -04:00
2015-05-05 17:07:32 -04:00
### Regular expression filter
The regular expression filter is similar to the selector filter, but using regular expressions. It matches the specified dimension with the given pattern. The pattern can be any standard [Java regular expression ](http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html ).
``` json
"filter": { "type": "regex", "dimension": < dimension_string > , "pattern": < pattern_string > }
```
2016-04-01 20:18:27 -04:00
The regex filter supports the use of extraction functions, see [Filtering with Extraction Functions ](#filtering-with-extraction-functions ) for details.
2015-05-05 17:07:32 -04:00
### Logical expression filters
#### AND
The grammar for an AND filter is as follows:
``` json
"filter": { "type": "and", "fields": [< filter > , < filter > , ...] }
```
The filters in fields can be any other filter defined on this page.
#### OR
The grammar for an OR filter is as follows:
``` json
"filter": { "type": "or", "fields": [< filter > , < filter > , ...] }
```
The filters in fields can be any other filter defined on this page.
#### NOT
The grammar for a NOT filter is as follows:
```json
"filter": { "type": "not", "field": < filter > }
```
The filter specified at field can be any other filter defined on this page.
### JavaScript filter
The JavaScript filter matches a dimension against the specified JavaScript function predicate. The filter matches values for which the function returns true.
The function takes a single argument, the dimension value, and returns either true or false.
```json
{
"type" : "javascript",
"dimension" : < dimension_string > ,
"function" : "function(value) { < ... > }"
}
```
**Example**
The following matches any dimension values for the dimension `name` between `'bar'` and `'foo'`
```json
{
"type" : "javascript",
"dimension" : "name",
"function" : "function(x) { return(x >= 'bar' & & x < = 'foo') }"
}
```
2015-07-27 17:23:14 -04:00
2016-04-01 20:18:27 -04:00
The JavaScript filter supports the use of extraction functions, see [Filtering with Extraction Functions ](#filtering-with-extraction-functions ) for details.
2019-08-21 00:48:59 -04:00
> JavaScript-based functionality is disabled by default. Please refer to the Druid [JavaScript programming guide](../development/javascript.md) for guidelines about using Druid's JavaScript functionality, including instructions on how to enable it.
2016-04-01 20:18:27 -04:00
2015-07-27 17:23:14 -04:00
### Extraction filter
2019-08-21 00:48:59 -04:00
> The extraction filter is now deprecated. The selector filter with an extraction function specified
> provides identical functionality and should be used instead.
2016-04-01 20:18:27 -04:00
2020-12-17 16:37:43 -05:00
Extraction filter matches a dimension using some specific [Extraction function ](./dimensionspecs.md#extraction-functions ).
2015-07-31 12:30:47 -04:00
The following filter matches the values for which the extraction function has transformation entry `input_key=output_value` where
2016-04-01 20:18:27 -04:00
`output_value` is equal to the filter `value` and `input_key` is present as dimension.
2015-07-27 17:23:14 -04:00
**Example**
The following matches dimension values in `[product_1, product_3, product_5]` for the column `product`
```json
{
"filter": {
"type": "extraction",
"dimension": "product",
"value": "bar_1",
"extractionFn": {
"type": "lookup",
"lookup": {
"type": "map",
"map": {
"product_1": "bar_1",
"product_5": "bar_1",
"product_3": "bar_1"
}
2015-08-04 10:07:47 -04:00
}
2015-07-27 17:23:14 -04:00
}
}
}
```
2016-04-01 20:18:27 -04:00
2015-09-16 18:51:35 -04:00
### Search filter
2019-08-21 00:48:59 -04:00
Search filters can be used to filter on partial string matches.
2015-09-16 18:51:35 -04:00
```json
{
"filter": {
"type": "search",
"dimension": "product",
"query": {
"type": "insensitive_contains",
2019-08-21 00:48:59 -04:00
"value": "foo"
}
2015-09-16 18:51:35 -04:00
}
}
```
|property|description|required?|
|--------|-----------|---------|
|type|This String should always be "search".|yes|
|dimension|The dimension to perform the search over.|yes|
|query|A JSON object for the type of search. See below for more information.|yes|
2016-04-01 20:18:27 -04:00
|extractionFn|[Extraction function](#filtering-with-extraction-functions) to apply to the dimension|no|
The search filter supports the use of extraction functions, see [Filtering with Extraction Functions ](#filtering-with-extraction-functions ) for details.
2019-08-21 00:48:59 -04:00
#### Search query spec
2016-08-05 10:25:04 -04:00
##### Contains
|property|description|required?|
|--------|-----------|---------|
|type|This String should always be "contains".|yes|
|value|A String value to run the search over.|yes|
|caseSensitive|Whether two string should be compared as case sensitive or not|no (default == false)|
##### Insensitive Contains
|property|description|required?|
|--------|-----------|---------|
|type|This String should always be "insensitive_contains".|yes|
|value|A String value to run the search over.|yes|
Note that an "insensitive_contains" search is equivalent to a "contains" search with "caseSensitive": false (or not
provided).
##### Fragment
|property|description|required?|
|--------|-----------|---------|
|type|This String should always be "fragment".|yes|
|values|A JSON array of String values to run the search over.|yes|
|caseSensitive|Whether strings should be compared as case sensitive or not. Default: false(insensitive)|no|
2015-09-16 18:51:35 -04:00
2015-11-04 17:15:14 -05:00
### In filter
In filter can be used to express the following SQL query:
```sql
SELECT COUNT(*) AS 'Count' FROM `table` WHERE `outlaw` IN ('Good', 'Bad', 'Ugly')
```
The grammar for a IN filter is as follows:
```json
{
"type": "in",
"dimension": "outlaw",
"values": ["Good", "Bad", "Ugly"]
}
```
2016-04-01 20:18:27 -04:00
The IN filter supports the use of extraction functions, see [Filtering with Extraction Functions ](#filtering-with-extraction-functions ) for details.
2018-10-14 23:43:16 -04:00
If an empty `values` array is passed to the IN filter, it will simply return an empty result.
2020-03-10 01:50:38 -04:00
If the `dimension` is a multi-valued dimension, the IN filter will return true if one of the dimension values is
in the `values` array.
2018-10-14 23:43:16 -04:00
2016-11-04 13:55:03 -04:00
### Like filter
Like filters can be used for basic wildcard searches. They are equivalent to the SQL LIKE operator. Special characters
supported are "%" (matches any number of characters) and "\_" (matches any one character).
|property|type|description|required?|
|--------|-----------|---------|---------|
|type|String|This should always be "like".|yes|
|dimension|String|The dimension to filter on|yes|
|pattern|String|LIKE pattern, such as "foo%" or "___bar".|yes|
|escape|String|An escape character that can be used to escape special characters.|no|
|extractionFn|[Extraction function](#filtering-with-extraction-functions)| Extraction function to apply to the dimension|no|
Like filters support the use of extraction functions, see [Filtering with Extraction Functions ](#filtering-with-extraction-functions ) for details.
This Like filter expresses the condition `last_name LIKE "D%"` (i.e. last_name starts with "D").
```json
{
"type": "like",
"dimension": "last_name",
"pattern": "D%"
}
```
2016-04-01 20:18:27 -04:00
2015-12-10 09:42:04 -05:00
### Bound filter
2016-11-04 13:55:03 -04:00
Bound filters can be used to filter on ranges of dimension values. It can be used for comparison filtering like
greater than, less than, greater than or equal to, less than or equal to, and "between" (if both "lower" and
"upper" are set).
2016-07-29 18:44:16 -04:00
|property|type|description|required?|
|--------|-----------|---------|---------|
|type|String|This should always be "bound".|yes|
|dimension|String|The dimension to filter on|yes|
|lower|String|The lower bound for the filter|no|
|upper|String|The upper bound for the filter|no|
2018-12-06 13:14:50 -05:00
|lowerStrict|Boolean|Perform strict comparison on the lower bound (">" instead of ">=")|no, default: false|
|upperStrict|Boolean|Perform strict comparison on the upper bound ("< " instead of "< =")|no, default: false|
2019-08-21 00:48:59 -04:00
|ordering|String|Specifies the sorting order to use when comparing values against the bound. Can be one of the following values: "lexicographic", "alphanumeric", "numeric", "strlen", "version". See [Sorting Orders ](./sorting-orders.md ) for more details.|no, default: "lexicographic"|
2016-07-29 18:44:16 -04:00
|extractionFn|[Extraction function](#filtering-with-extraction-functions)| Extraction function to apply to the dimension|no|
2019-08-21 00:48:59 -04:00
2016-11-04 13:55:03 -04:00
Bound filters support the use of extraction functions, see [Filtering with Extraction Functions ](#filtering-with-extraction-functions ) for details.
2016-04-01 20:18:27 -04:00
2016-07-29 18:44:16 -04:00
The following bound filter expresses the condition `21 <= age <= 31` :
2019-07-15 12:55:18 -04:00
2015-12-10 09:42:04 -05:00
```json
{
"type": "bound",
"dimension": "age",
"lower": "21",
"upper": "31" ,
2016-07-29 18:44:16 -04:00
"ordering": "numeric"
2015-12-10 09:42:04 -05:00
}
```
2016-07-29 18:44:16 -04:00
This filter expresses the condition `foo <= name <= hoo` , using the default lexicographic sorting order.
2019-07-15 12:55:18 -04:00
2015-12-10 09:42:04 -05:00
```json
{
"type": "bound",
"dimension": "name",
"lower": "foo",
"upper": "hoo"
}
```
2016-07-29 18:44:16 -04:00
Using strict bounds, this filter expresses the condition `21 < age < 31`
2019-07-15 12:55:18 -04:00
2015-12-10 09:42:04 -05:00
```json
{
"type": "bound",
"dimension": "age",
"lower": "21",
"lowerStrict": true,
"upper": "31" ,
"upperStrict": true,
2016-07-29 18:44:16 -04:00
"ordering": "numeric"
2015-12-10 09:42:04 -05:00
}
```
2016-07-29 18:44:16 -04:00
The user can also specify a one-sided bound by omitting "upper" or "lower". This filter expresses `age < 31` .
2019-07-15 12:55:18 -04:00
2015-12-10 09:42:04 -05:00
```json
{
"type": "bound",
"dimension": "age",
"upper": "31" ,
"upperStrict": true,
2016-07-29 18:44:16 -04:00
"ordering": "numeric"
2015-12-10 09:42:04 -05:00
}
```
2016-07-29 18:44:16 -04:00
Likewise, this filter expresses `age >= 18`
2019-07-15 12:55:18 -04:00
2015-12-10 09:42:04 -05:00
```json
{
"type": "bound",
"dimension": "age",
"lower": "18" ,
2016-07-29 18:44:16 -04:00
"ordering": "numeric"
2015-12-10 09:42:04 -05:00
}
```
2016-04-01 20:18:27 -04:00
2016-08-05 10:25:04 -04:00
### Interval Filter
2015-09-16 18:51:35 -04:00
2016-08-05 10:25:04 -04:00
The Interval filter enables range filtering on columns that contain long millisecond values, with the boundaries specified as ISO 8601 time intervals. It is suitable for the `__time` column, long metric columns, and dimensions with values that can be parsed as long milliseconds.
2016-06-02 22:03:40 -04:00
2016-08-05 10:25:04 -04:00
This filter converts the ISO 8601 intervals to long millisecond start/end ranges and translates to an OR of Bound filters on those millisecond ranges, with numeric comparison. The Bound filters will have left-closed and right-open matching (i.e., start < = time < end ) .
2016-06-02 22:03:40 -04:00
2016-08-05 10:25:04 -04:00
|property|type|description|required?|
|--------|-----------|---------|---------|
|type|String|This should always be "interval".|yes|
|dimension|String|The dimension to filter on|yes|
|intervals|Array|A JSON array containing ISO-8601 interval strings. This defines the time ranges to filter on.|yes|
|extractionFn|[Extraction function](#filtering-with-extraction-functions)| Extraction function to apply to the dimension|no|
2019-08-21 00:48:59 -04:00
2016-08-05 10:25:04 -04:00
The interval filter supports the use of extraction functions, see [Filtering with Extraction Functions ](#filtering-with-extraction-functions ) for details.
2015-09-16 18:51:35 -04:00
2016-08-05 10:25:04 -04:00
If an extraction function is used with this filter, the extraction function should output values that are parseable as long milliseconds.
2015-09-16 18:51:35 -04:00
2016-08-05 10:25:04 -04:00
The following example filters on the time ranges of October 1-7, 2014 and November 15-16, 2014.
2019-07-15 12:55:18 -04:00
2016-08-05 10:25:04 -04:00
```json
{
"type" : "interval",
"dimension" : "__time",
"intervals" : [
"2014-10-01T00:00:00.000Z/2014-10-07T00:00:00.000Z",
"2014-11-15T00:00:00.000Z/2014-11-16T00:00:00.000Z"
]
}
```
2016-06-02 22:03:40 -04:00
2016-08-05 10:25:04 -04:00
The filter above is equivalent to the following OR of Bound filters:
2015-09-16 18:51:35 -04:00
2016-08-05 10:25:04 -04:00
```json
{
"type": "or",
"fields": [
{
"type": "bound",
"dimension": "__time",
"lower": "1412121600000",
"lowerStrict": false,
"upper": "1412640000000" ,
"upperStrict": true,
"ordering": "numeric"
},
{
"type": "bound",
"dimension": "__time",
"lower": "1416009600000",
"lowerStrict": false,
"upper": "1416096000000" ,
"upperStrict": true,
"ordering": "numeric"
}
]
}
```
2015-11-02 00:21:29 -05:00
2016-04-01 20:18:27 -04:00
### Filtering with Extraction Functions
2016-09-09 16:47:13 -04:00
All filters except the "spatial" filter support extraction functions.
2016-04-01 20:18:27 -04:00
An extraction function is defined by setting the "extractionFn" field on a filter.
2020-12-17 16:37:43 -05:00
See [Extraction function ](./dimensionspecs.md#extraction-functions ) for more details on extraction functions.
2016-04-01 20:18:27 -04:00
If specified, the extraction function will be used to transform input values before the filter is applied.
The example below shows a selector filter combined with an extraction function. This filter will transform input values
according to the values defined in the lookup map; transformed values will then be matched with the string "bar_1".
**Example**
The following matches dimension values in `[product_1, product_3, product_5]` for the column `product`
```json
{
"filter": {
"type": "selector",
"dimension": "product",
"value": "bar_1",
"extractionFn": {
"type": "lookup",
"lookup": {
"type": "map",
"map": {
"product_1": "bar_1",
"product_5": "bar_1",
"product_3": "bar_1"
}
}
}
}
}
2016-06-02 22:03:40 -04:00
```
2016-07-20 20:08:49 -04:00
2017-03-10 15:48:59 -05:00
## Column types
2016-07-20 20:08:49 -04:00
2017-03-10 15:48:59 -05:00
Druid supports filtering on timestamp, string, long, and float columns.
2016-07-20 20:08:49 -04:00
2017-03-10 15:48:59 -05:00
Note that only string columns have bitmap indexes. Therefore, queries that filter on other column types will need to
scan those columns.
2016-07-20 20:08:49 -04:00
2017-03-10 15:48:59 -05:00
### Filtering on numeric columns
2016-07-20 20:08:49 -04:00
2017-03-10 15:48:59 -05:00
When filtering on numeric columns, you can write filters as if they were strings. In most cases, your filter will be
converted into a numeric predicate and will be applied to the numeric column values directly. In some cases (such as
the "regex" filter) the numeric column values will be converted to strings during the scan.
2016-07-20 20:08:49 -04:00
2017-03-10 15:48:59 -05:00
For example, filtering on a specific value, `myFloatColumn = 10.1` :
```json
"filter": {
"type": "selector",
"dimension": "myFloatColumn",
"value": "10.1"
}
```
Filtering on a range of values, `10 <= myFloatColumn < 20` :
```json
"filter": {
"type": "bound",
"dimension": "myFloatColumn",
"ordering": "numeric",
2018-08-23 17:07:10 -04:00
"lower": "10",
2017-03-10 15:48:59 -05:00
"lowerStrict": false,
2018-08-23 17:07:10 -04:00
"upper": "20",
2017-03-10 15:48:59 -05:00
"upperStrict": true
}
```
### Filtering on the Timestamp Column
Query filters can also be applied to the timestamp column. The timestamp column has long millisecond values. To refer
to the timestamp column, use the string `__time` as the dimension name. Like numeric dimensions, timestamp filters
should be specified as if the timestamp values were strings.
2020-12-17 16:37:43 -05:00
If the user wishes to interpret the timestamp with a specific format, timezone, or locale, the [Time Format Extraction Function ](./dimensionspecs.md#time-format-extraction-function ) is useful.
2017-03-10 15:48:59 -05:00
For example, filtering on a long timestamp value:
2016-07-20 20:08:49 -04:00
```json
"filter": {
"type": "selector",
"dimension": "__time",
"value": "124457387532"
}
```
Filtering on day of week:
2017-03-10 15:48:59 -05:00
2016-07-20 20:08:49 -04:00
```json
"filter": {
"type": "selector",
"dimension": "__time",
"value": "Friday",
"extractionFn": {
"type": "timeFormat",
"format": "EEEE",
"timeZone": "America/New_York",
"locale": "en"
}
}
```
2016-08-05 10:25:04 -04:00
Filtering on a set of ISO 8601 intervals:
2017-03-10 15:48:59 -05:00
2016-08-05 10:25:04 -04:00
```json
{
"type" : "interval",
"dimension" : "__time",
"intervals" : [
"2014-10-01T00:00:00.000Z/2014-10-07T00:00:00.000Z",
"2014-11-15T00:00:00.000Z/2014-11-16T00:00:00.000Z"
]
}
2017-02-13 18:11:18 -05:00
```
2018-04-18 10:57:07 -04:00
2018-06-28 14:52:45 -04:00
### True Filter
2019-08-21 00:48:59 -04:00
The true filter is a filter which matches all values. It can be used to temporarily disable other filters without removing the filter.
2018-04-18 10:57:07 -04:00
```json
2018-08-23 17:07:10 -04:00
2018-06-28 14:52:45 -04:00
{ "type" : "true" }
2018-08-23 17:07:10 -04:00
```
2021-04-07 19:58:28 -04:00
### Expression Filter
The expression filter allows for the implementation of arbitrary conditions, leveraging the Druid expression system.
This filter allows for more flexibility, but it might be less performant than a combination of the other filters on this page due to the fact that not all filter optimizations are in place yet.
```json
{
"type" : "expression" ,
"expression" : "((product_type == 42) & & (!is_deleted))"
}
```
See the [Druid expression system ](../misc/math-expr.md ) for more details.