2018-12-13 14:47:20 -05:00
---
2019-08-21 00:48:59 -04:00
id: searchquery
title: "Search queries"
sidebar_label: "Search"
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 a query
> type that is only available in the native language.
2018-12-12 23:42:12 -05:00
2015-05-05 17:07:32 -04:00
A search query returns dimension values that match the search specification.
```json
{
"queryType": "search",
"dataSource": "sample_datasource",
"granularity": "day",
"searchDimensions": [
"dim1",
"dim2"
],
"query": {
"type": "insensitive_contains",
"value": "Ke"
},
"sort" : {
"type": "lexicographic"
},
"intervals": [
"2013-01-01T00:00:00.000/2013-01-03T00:00:00.000"
]
}
```
There are several main parts to a search query:
|property|description|required?|
|--------|-----------|---------|
2020-01-03 12:33:19 -05:00
|queryType|This String should always be "search"; this is the first thing Apache Druid looks at to figure out how to interpret the query.|yes|
2019-08-21 00:48:59 -04:00
|dataSource|A String or Object defining the data source to query, very similar to a table in a relational database. See [DataSource ](../querying/datasource.md ) for more information.|yes|
2020-12-01 21:29:51 -05:00
|granularity|Defines the granularity of the query. See [Granularities ](../querying/granularities.md ).|no (default to `all` )|
2019-08-21 00:48:59 -04:00
|filter|See [Filters ](../querying/filters.md ).|no|
2019-02-28 21:10:39 -05:00
|limit| Defines the maximum number per Historical process (parsed as int) of search results to return. |no (default to 1000)|
2015-05-05 17:07:32 -04:00
|intervals|A JSON Object representing ISO-8601 Intervals. This defines the time ranges to run the query over.|yes|
|searchDimensions|The dimensions to run the search over. Excluding this means the search is run over all dimensions.|no|
2022-08-18 23:49:23 -04:00
|virtualColumns|A JSON list of [virtual columns ](./virtual-columns.md ) available to use in `searchDimensions` .| no (default none)|
2020-04-15 19:12:20 -04:00
|query|See [SearchQuerySpec ](#searchqueryspec ).|yes|
2019-08-21 00:48:59 -04:00
|sort|An object specifying how the results of the search should be sorted.< br /> Possible types are "lexicographic" (the default sort), "alphanumeric", "strlen", and "numeric".< br /> See [Sorting Orders ](./sorting-orders.md ) for more details.|no|
|context|See [Context ](../querying/query-context.md )|no|
2015-05-05 17:07:32 -04:00
The format of the result is:
```json
[
{
2019-10-22 17:39:47 -04:00
"timestamp": "2013-01-01T00:00:00.000Z",
2015-05-05 17:07:32 -04:00
"result": [
{
"dimension": "dim1",
2016-03-15 20:22:18 -04:00
"value": "Ke$ha",
"count": 3
2015-05-05 17:07:32 -04:00
},
{
"dimension": "dim2",
2016-03-15 20:22:18 -04:00
"value": "Ke$haForPresident",
"count": 1
2015-05-05 17:07:32 -04:00
}
]
},
{
2019-10-22 17:39:47 -04:00
"timestamp": "2013-01-02T00:00:00.000Z",
2015-05-05 17:07:32 -04:00
"result": [
{
"dimension": "dim1",
2016-03-15 20:22:18 -04:00
"value": "SomethingThatContainsKe",
"count": 1
2015-05-05 17:07:32 -04:00
},
{
"dimension": "dim2",
2016-03-15 20:22:18 -04:00
"value": "SomethingElseThatContainsKe",
"count": 2
2015-05-05 17:07:32 -04:00
}
]
}
]
```
2017-01-10 21:04:20 -05:00
### Implementation details
#### Strategies
Search queries can be executed using two different strategies. The default strategy is determined by the
2019-01-30 22:41:07 -05:00
"druid.query.search.searchStrategy" runtime property on the Broker. This can be overridden using "searchStrategy" in the
2017-01-10 21:04:20 -05:00
query context. If neither the context field nor the property is set, the "useIndexes" strategy will be used.
- "useIndexes" strategy, the default, first categorizes search dimensions into two groups according to their support for
bitmap indexes. And then, it applies index-only and cursor-based execution plans to the group of dimensions supporting
bitmaps and others, respectively. The index-only plan uses only indexes for search query processing. For each dimension,
it reads the bitmap index for each dimension value, evaluates the search predicate, and finally checks the time interval
and filter predicates. For the cursor-based execution plan, please refer to the "cursorOnly" strategy. The index-only
plan shows low performance for the search dimensions of large cardinality which means most values of search dimensions
are unique.
- "cursorOnly" strategy generates a cursor-based execution plan. This plan creates a cursor which reads a row from a
queryableIndexSegment, and then evaluates search predicates. If some filters support bitmap indexes, the cursor can read
only the rows which satisfy those filters, thereby saving I/O cost. However, it might be slow with filters of low selectivity.
- "auto" strategy uses a cost-based planner for choosing an optimal search strategy. It estimates the cost of index-only
and cursor-based execution plans, and chooses the optimal one. Currently, it is not enabled by default due to the overhead
of cost estimation.
2019-10-24 14:17:39 -04:00
## Server configuration
2017-01-10 21:04:20 -05:00
The following runtime properties apply:
|Property|Description|Default|
|--------|-----------|-------|
|`druid.query.search.searchStrategy`|Default search query strategy.|useIndexes|
2019-10-24 14:17:39 -04:00
## Query context
2017-01-10 21:04:20 -05:00
The following query context parameters apply:
|Property|Description|
|--------|-----------|
|`searchStrategy`|Overrides the value of `druid.query.search.searchStrategy` for this query.|
2020-04-15 19:12:20 -04:00
## SearchQuerySpec
### `insensitive_contains`
If any part of a dimension value contains the value specified in this search query spec, regardless of case, a "match" occurs. The grammar is:
```json
{
"type" : "insensitive_contains",
"value" : "some_value"
}
```
### `fragment`
If any part of a dimension value contains all of the values specified in this search query spec, regardless of case by default, a "match" occurs. The grammar is:
```json
{
"type" : "fragment",
"case_sensitive" : false,
"values" : ["fragment1", "fragment2"]
}
```
### `contains`
If any part of a dimension value contains the value specified in this search query spec, a "match" occurs. The grammar is:
```json
{
"type" : "contains",
"case_sensitive" : true,
"value" : "some_value"
}
```
### `regex`
If any part of a dimension value contains the pattern specified in this search query spec, a "match" occurs. The grammar is:
```json
{
"type" : "regex",
"pattern" : "some_pattern"
}
```