OpenSearch template variables
Instead of hard-coding values like server names, applications, or sensor names in your queries, you can use template variables. Variables appear as drop-down select boxes at the top of the dashboard, making it easy to change the data displayed.
For an introduction to template variables, refer to the Grafana template variables documentation.
Create a query variable
The OpenSearch data source supports two types of queries for query variables. Queries are written as a JSON string in the Query field.
Note
The query must be valid JSON. Invalid JSON causes the variable query to fail.
To create a query variable:
- Navigate to Dashboard settings > Variables.
- Click Add variable.
- Select Query as the variable type.
- Select the OpenSearch data source.
- Enter one of the JSON queries from the following table in the Query field.
Find fields
Use "find": "fields" to return a list of field names from the index. Optionally filter by type:
{"find": "fields", "type": "keyword"}Other examples:
{"find": "fields", "type": "number"}{"find": "fields", "type": "date"}The type parameter is optional. Supported type values include:
Omitting the type parameter returns all eligible fields.
Find terms
Use "find": "terms" to return a list of values for a field using term aggregation. Terms queries are always scoped to the current dashboard time range.
The following table describes all available parameters for terms queries:
Multi-field types
If the query targets a multi-field with both a text and keyword type, use "field":"fieldname.keyword" (or sometimes fieldname.raw) to specify the keyword field in your query.
Use other variables in queries
You can reference other variables inside a query definition. For example, for a variable named $host:
{"find": "terms", "field": "@hostname", "query": "@source:$source"}In this example, the $source variable filters the $host variable. Changing the $source value triggers an update to the $host variable, returning only hostnames matching the selected @source value.
Sort by document count
By default, terms queries return results sorted by term value. To sort by document count (top-N values), add "orderBy": "doc_count":
{"find": "terms", "field": "@hostname", "orderBy": "doc_count"}This automatically selects descending order. You can explicitly set ascending order with "order": "asc", but this is discouraged because it increases the error on document counts. Refer to the OpenSearch terms aggregation documentation for details.
To return the top 10 values by document count:
{"find": "terms", "field": "@hostname", "orderBy": "doc_count", "order": "desc", "size": 10}To keep terms in document count order in the dashboard drop-down, set the variable’s Sort drop-down to Disabled. You can also use other sort options like Alphabetical to re-sort the results.
Use variables in queries
There are two syntaxes for using variables in queries:
Lucene queries
When the Multi-value or Include all value options are enabled, Grafana converts the variable values into a Lucene-compatible OR condition.
You can use variables in the Lucene query field and in the Terms group-by field to dynamically change how data is filtered and grouped. For example:
@hostname:$hostname AND status:[400 TO 499]PPL queries
Variables in PPL queries are interpolated using CSV formatting. For example, a multi-value variable produces comma-separated values. You can use variables in PPL queries like this:
source = my_index | where hostname = '$hostname' | where status >= 400Ad hoc filters
Ad hoc filter variables let you add key-value filters from the dashboard without modifying the query. The OpenSearch data source supports ad hoc filters for both Lucene and PPL queries.
When you create an ad hoc filter variable:
- Key suggestions are populated from all available fields in the index.
- Value suggestions are populated using a terms aggregation on the selected key, scoped to the dashboard time range.
Supported operators
The following operators are available for ad hoc filters:
Note
Regex operators (
=~and!~) are not supported in PPL queries and are silently ignored.


