-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't send index metadata, revamp Elastic querybuilder
- Loading branch information
Showing
9 changed files
with
92 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,67 @@ | ||
import { | ||
BucketAggregation, | ||
ElasticsearchQuery, | ||
MetricAggregation, | ||
TermsQuery, | ||
} from '../types'; | ||
|
||
export class ElasticQueryBuilder { | ||
timeField: string; | ||
|
||
constructor(options: { timeField: string }) { | ||
this.timeField = options.timeField; | ||
} | ||
|
||
getRangeFilter() { | ||
const filter: any = {}; | ||
filter[this.timeField] = { | ||
gte: '$timeFrom', | ||
lte: '$timeTo', | ||
// FIXME when Quickwit supports format. | ||
// format: 'epoch_millis', | ||
}; | ||
|
||
return filter; | ||
} | ||
|
||
getTermsQuery(queryDef: TermsQuery) { | ||
const query: any = { | ||
size: 0, | ||
query: { | ||
bool: { | ||
filter: [{ range: this.getRangeFilter() }], | ||
}, | ||
}, | ||
}; | ||
|
||
if (queryDef.query) { | ||
query.query.bool.filter.push({ | ||
query_string: { | ||
// FIXME when Quickwit supports analyze_wildcard. | ||
// analyze_wildcard: true, | ||
query: queryDef.query, | ||
}, | ||
}); | ||
} | ||
type OrderByType = '_key' | '_term' | '_count' | ||
|
||
let size = 500; | ||
if (queryDef.size) { | ||
size = queryDef.size; | ||
function getTermsAgg( | ||
fieldName: string, | ||
size: number, | ||
orderBy: OrderByType = "_key", | ||
order: 'asc'|'desc' = 'asc' | ||
): BucketAggregation { | ||
return { | ||
type: 'terms', | ||
id: "", | ||
field: fieldName, | ||
settings:{ | ||
size: size.toString(), | ||
order: order, | ||
orderBy: orderBy, | ||
} | ||
} | ||
} | ||
|
||
query.aggs = { | ||
'1': { | ||
terms: { | ||
field: queryDef.field, | ||
size: size, | ||
order: {}, | ||
}, | ||
}, | ||
}; | ||
export function getDataQuery(queryDef: TermsQuery, refId: string): ElasticsearchQuery { | ||
const metrics: MetricAggregation[] = [ | ||
{id:"count1", type:'count'} | ||
]; | ||
|
||
// 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) | ||
// 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; | ||
let orderBy: OrderByType; | ||
switch (queryDef.orderBy || 'key') { | ||
case 'key': | ||
case 'term': | ||
orderBy = '_key' | ||
break; | ||
case 'doc_count': | ||
orderBy = '_count' | ||
break; | ||
default: | ||
throw { message: `Invalid query sort type ${queryDef.orderBy}` }; | ||
} | ||
|
||
if (['asc', 'desc'].indexOf(order) < 0) { | ||
throw { message: `Invalid query sort order ${order}` }; | ||
} | ||
const {order = orderBy === '_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 = '_key'; | ||
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}` }; | ||
} | ||
const bucketAggs: BucketAggregation[] = []; | ||
if (queryDef.field) { | ||
bucketAggs.push(getTermsAgg(queryDef.field, 500, orderBy, order)) | ||
} | ||
|
||
return query; | ||
return { | ||
refId, | ||
metrics, | ||
bucketAggs, | ||
query: queryDef.query, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.