Elasticsearch: allow templating queries to order by doc_count (#18870)

* Elasticsearch Datasource: allow templating queries to order by doc_count

* Elasticsearch Datasource: add tests for doc_count templating queries
This commit is contained in:
James Beckett
2019-09-16 16:41:53 +01:00
committed by Sofia Papagiannaki
parent 1a71501440
commit ba11958a52
3 changed files with 97 additions and 9 deletions

View File

@@ -143,6 +143,15 @@ You can use other variables inside the query. Example query definition for a var
In the above example, we use another variable named `$source` inside the query definition. Whenever you change, via the dropdown, the current value of the ` $source` variable, it will trigger an update of the `$host` variable so it now only contains hostnames filtered by in this case the
`@source` document property.
These queries by default return results in term order (which can then be sorted alphabetically or numerically as for any variable).
To produce a list of terms sorted by doc count (a top-N values list), add an `orderBy` property of "doc_count".
This automatically selects a descending sort; using "asc" with doc_count (a bottom-N list) can be done by setting `order: "asc"` but [is discouraged](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation-order) as it "increases the error on document counts".
To keep terms in the doc count order, set the variable's Sort dropdown to **Disabled**; you might alternatively still want to use e.g. **Alphabetical** to re-sort them.
```
{"find": "terms", "field": "@hostname", "orderBy": "doc_count"}
```
### Using variables in queries
There are two syntaxes: