mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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:
committed by
Sofia Papagiannaki
parent
1a71501440
commit
ba11958a52
@@ -353,17 +353,32 @@ export class ElasticQueryBuilder {
|
||||
terms: {
|
||||
field: queryDef.field,
|
||||
size: size,
|
||||
order: {
|
||||
_term: 'asc',
|
||||
},
|
||||
order: {},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
if (this.esVersion >= 60) {
|
||||
query.aggs['1'].terms.order = {
|
||||
_key: 'asc',
|
||||
};
|
||||
// Default behaviour is to order results by { _key: asc }
|
||||
// queryDef.order allows selection of asc/desc
|
||||
// queryDef.orderBy allows selection of doc_count ordering (defaults desc)
|
||||
|
||||
const { orderBy = 'key', order = orderBy === 'doc_count' ? 'desc' : 'asc' } = queryDef;
|
||||
|
||||
if (['asc', 'desc'].indexOf(order) < 0) {
|
||||
throw { message: `Invalid query sort order ${order}` };
|
||||
}
|
||||
|
||||
switch (orderBy) {
|
||||
case 'key':
|
||||
case 'term':
|
||||
const keyname = this.esVersion >= 60 ? '_key' : '_term';
|
||||
query.aggs['1'].terms.order[keyname] = order;
|
||||
break;
|
||||
case 'doc_count':
|
||||
query.aggs['1'].terms.order['_count'] = order;
|
||||
break;
|
||||
default:
|
||||
throw { message: `Invalid query sort type ${orderBy}` };
|
||||
}
|
||||
|
||||
return query;
|
||||
|
||||
Reference in New Issue
Block a user