Query Filters

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.

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:

  1. "filter": { "type": "selector", "dimension": <dimension_string>, "value": <dimension_value_string> }

This is the equivalent of WHERE <dimension_string> = '<dimension_value_string>'.

The selector filter supports the use of extraction functions, see Filtering with Extraction Functions for details.

Column Comparison filter

The column comparison filter is similar to the selector filter, but instead compares dimensions to each other. For example:

  1. "filter": { "type": "columnComparison", "dimensions": [<dimension_a>, <dimension_b>] }

This is the equivalent of WHERE <dimension_a> = <dimension_b>.

dimensions is list of DimensionSpecs, making it possible to apply an extraction function if needed.

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.

  1. "filter": { "type": "regex", "dimension": <dimension_string>, "pattern": <pattern_string> }

The regex filter supports the use of extraction functions, see Filtering with Extraction Functions for details.

Logical expression filters

AND

The grammar for an AND filter is as follows:

  1. "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:

  1. "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:

  1. "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.

  1. {
  2. "type" : "javascript",
  3. "dimension" : <dimension_string>,
  4. "function" : "function(value) { <...> }"
  5. }

Example The following matches any dimension values for the dimension name between 'bar' and 'foo'

  1. {
  2. "type" : "javascript",
  3. "dimension" : "name",
  4. "function" : "function(x) { return(x >= 'bar' && x <= 'foo') }"
  5. }

The JavaScript filter supports the use of extraction functions, see Filtering with Extraction Functions for details.

JavaScript-based functionality is disabled by default. Please refer to the Druid JavaScript programming guide for guidelines about using Druid’s JavaScript functionality, including instructions on how to enable it.

Extraction filter

The extraction filter is now deprecated. The selector filter with an extraction function specified provides identical functionality and should be used instead.

Extraction filter matches a dimension using some specific Extraction function. The following filter matches the values for which the extraction function has transformation entry input_key=output_value where output_value is equal to the filter value and input_key is present as dimension.

Example The following matches dimension values in [product_1, product_3, product_5] for the column product

  1. {
  2. "filter": {
  3. "type": "extraction",
  4. "dimension": "product",
  5. "value": "bar_1",
  6. "extractionFn": {
  7. "type": "lookup",
  8. "lookup": {
  9. "type": "map",
  10. "map": {
  11. "product_1": "bar_1",
  12. "product_5": "bar_1",
  13. "product_3": "bar_1"
  14. }
  15. }
  16. }
  17. }
  18. }

Search filter

Search filters can be used to filter on partial string matches.

  1. {
  2. "filter": {
  3. "type": "search",
  4. "dimension": "product",
  5. "query": {
  6. "type": "insensitive_contains",
  7. "value": "foo"
  8. }
  9. }
  10. }
propertydescriptionrequired?
typeThis String should always be “search”.yes
dimensionThe dimension to perform the search over.yes
queryA JSON object for the type of search. See below for more information.yes
extractionFnExtraction function to apply to the dimensionno

The search filter supports the use of extraction functions, see Filtering with Extraction Functions for details.

Search query spec

Contains
propertydescriptionrequired?
typeThis String should always be “contains”.yes
valueA String value to run the search over.yes
caseSensitiveWhether two string should be compared as case sensitive or notno (default == false)
Insensitive Contains
propertydescriptionrequired?
typeThis String should always be “insensitive_contains”.yes
valueA 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
propertydescriptionrequired?
typeThis String should always be “fragment”.yes
valuesA JSON array of String values to run the search over.yes
caseSensitiveWhether strings should be compared as case sensitive or not. Default: false(insensitive)no

In filter

In filter can be used to express the following SQL query:

  1. SELECT COUNT(*) AS 'Count' FROM `table` WHERE `outlaw` IN ('Good', 'Bad', 'Ugly')

The grammar for a IN filter is as follows:

  1. {
  2. "type": "in",
  3. "dimension": "outlaw",
  4. "values": ["Good", "Bad", "Ugly"]
  5. }

The IN filter supports the use of extraction functions, see Filtering with Extraction Functions for details.

If an empty values array is passed to the IN filter, it will simply return an empty result.

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).

propertytypedescriptionrequired?
typeStringThis should always be “like”.yes
dimensionStringThe dimension to filter onyes
patternStringLIKE pattern, such as “foo%” or “___bar”.yes
escapeStringAn escape character that can be used to escape special characters.no
extractionFnExtraction functionExtraction function to apply to the dimensionno

Like filters support the use of extraction functions, see Filtering with Extraction Functions for details.

This Like filter expresses the condition last_name LIKE "D%" (i.e. last_name starts with “D”).

  1. {
  2. "type": "like",
  3. "dimension": "last_name",
  4. "pattern": "D%"
  5. }

Bound filter

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).

propertytypedescriptionrequired?
typeStringThis should always be “bound”.yes
dimensionStringThe dimension to filter onyes
lowerStringThe lower bound for the filterno
upperStringThe upper bound for the filterno
lowerStrictBooleanPerform strict comparison on the lower bound (“>” instead of “>=”)no, default: false
upperStrictBooleanPerform strict comparison on the upper bound (“<” instead of “<=”)no, default: false
orderingStringSpecifies 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 for more details.no, default: “lexicographic”
extractionFnExtraction functionExtraction function to apply to the dimensionno

Bound filters support the use of extraction functions, see Filtering with Extraction Functions for details.

The following bound filter expresses the condition 21 <= age <= 31:

  1. {
  2. "type": "bound",
  3. "dimension": "age",
  4. "lower": "21",
  5. "upper": "31" ,
  6. "ordering": "numeric"
  7. }

This filter expresses the condition foo <= name <= hoo, using the default lexicographic sorting order.

  1. {
  2. "type": "bound",
  3. "dimension": "name",
  4. "lower": "foo",
  5. "upper": "hoo"
  6. }

Using strict bounds, this filter expresses the condition 21 < age < 31

  1. {
  2. "type": "bound",
  3. "dimension": "age",
  4. "lower": "21",
  5. "lowerStrict": true,
  6. "upper": "31" ,
  7. "upperStrict": true,
  8. "ordering": "numeric"
  9. }

The user can also specify a one-sided bound by omitting “upper” or “lower”. This filter expresses age < 31.

  1. {
  2. "type": "bound",
  3. "dimension": "age",
  4. "upper": "31" ,
  5. "upperStrict": true,
  6. "ordering": "numeric"
  7. }

Likewise, this filter expresses age >= 18

  1. {
  2. "type": "bound",
  3. "dimension": "age",
  4. "lower": "18" ,
  5. "ordering": "numeric"
  6. }

Interval Filter

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.

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).

propertytypedescriptionrequired?
typeStringThis should always be “interval”.yes
dimensionStringThe dimension to filter onyes
intervalsArrayA JSON array containing ISO-8601 interval strings. This defines the time ranges to filter on.yes
extractionFnExtraction functionExtraction function to apply to the dimensionno

The interval filter supports the use of extraction functions, see Filtering with Extraction Functions for details.

If an extraction function is used with this filter, the extraction function should output values that are parseable as long milliseconds.

The following example filters on the time ranges of October 1-7, 2014 and November 15-16, 2014.

  1. {
  2. "type" : "interval",
  3. "dimension" : "__time",
  4. "intervals" : [
  5. "2014-10-01T00:00:00.000Z/2014-10-07T00:00:00.000Z",
  6. "2014-11-15T00:00:00.000Z/2014-11-16T00:00:00.000Z"
  7. ]
  8. }

The filter above is equivalent to the following OR of Bound filters:

  1. {
  2. "type": "or",
  3. "fields": [
  4. {
  5. "type": "bound",
  6. "dimension": "__time",
  7. "lower": "1412121600000",
  8. "lowerStrict": false,
  9. "upper": "1412640000000" ,
  10. "upperStrict": true,
  11. "ordering": "numeric"
  12. },
  13. {
  14. "type": "bound",
  15. "dimension": "__time",
  16. "lower": "1416009600000",
  17. "lowerStrict": false,
  18. "upper": "1416096000000" ,
  19. "upperStrict": true,
  20. "ordering": "numeric"
  21. }
  22. ]
  23. }

Filtering with Extraction Functions

All filters except the “spatial” filter support extraction functions. An extraction function is defined by setting the “extractionFn” field on a filter. See Extraction function for more details on extraction functions.

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

  1. {
  2. "filter": {
  3. "type": "selector",
  4. "dimension": "product",
  5. "value": "bar_1",
  6. "extractionFn": {
  7. "type": "lookup",
  8. "lookup": {
  9. "type": "map",
  10. "map": {
  11. "product_1": "bar_1",
  12. "product_5": "bar_1",
  13. "product_3": "bar_1"
  14. }
  15. }
  16. }
  17. }
  18. }

Column types

Druid supports filtering on timestamp, string, long, and float columns.

Note that only string columns have bitmap indexes. Therefore, queries that filter on other column types will need to scan those columns.

Filtering on numeric columns

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.

For example, filtering on a specific value, myFloatColumn = 10.1:

  1. "filter": {
  2. "type": "selector",
  3. "dimension": "myFloatColumn",
  4. "value": "10.1"
  5. }

Filtering on a range of values, 10 <= myFloatColumn < 20:

  1. "filter": {
  2. "type": "bound",
  3. "dimension": "myFloatColumn",
  4. "ordering": "numeric",
  5. "lower": "10",
  6. "lowerStrict": false,
  7. "upper": "20",
  8. "upperStrict": true
  9. }

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.

If the user wishes to interpret the timestamp with a specific format, timezone, or locale, the Time Format Extraction Function is useful.

For example, filtering on a long timestamp value:

  1. "filter": {
  2. "type": "selector",
  3. "dimension": "__time",
  4. "value": "124457387532"
  5. }

Filtering on day of week:

  1. "filter": {
  2. "type": "selector",
  3. "dimension": "__time",
  4. "value": "Friday",
  5. "extractionFn": {
  6. "type": "timeFormat",
  7. "format": "EEEE",
  8. "timeZone": "America/New_York",
  9. "locale": "en"
  10. }
  11. }

Filtering on a set of ISO 8601 intervals:

  1. {
  2. "type" : "interval",
  3. "dimension" : "__time",
  4. "intervals" : [
  5. "2014-10-01T00:00:00.000Z/2014-10-07T00:00:00.000Z",
  6. "2014-11-15T00:00:00.000Z/2014-11-16T00:00:00.000Z"
  7. ]
  8. }

True Filter

The true filter is a filter which matches all values. It can be used to temporarily disable other filters without removing the filter.

  1. { "type" : "true" }