1. 建立es客戶端連接
var elasticsearch = require('elasticsearch');
const Service = require('egg').Service;
let esClient;
class EsBaseService extends Service {
getEsHost() {
let esHost = '172.21.101.22:9200';
if (this.ctx.helper.isPro()) {
esHost = '172.2.101.2:93200';
}
return esHost
}
getEsClient(argument) {
if (!esClient) {
this.ctx.logger.info('esClient init' + this.getEsHost())
esClient = new elasticsearch.Client({
host: this.getEsHost(),
log: 'info'
});
}
return esClient;
}
searchBucketsAndCountFromFsBugInfo(opts) {
return new Promise((resolve, reject) => {
this.searchFromFsBugInfo({
body: opts.body
}).then(function (body) {
let data = {
name: opts.name,
count: body.hits.total.value,
buckets: body.aggregations[2].buckets
};
resolve(data)
});
});
}
searchFromFsBugInfo(opts){
return this.getEsClient().search({
index: 'fs-bug-info',
body: opts.body
});
}
}
module.exports = EsBaseService;
2. 定義查詢任務
async getAggr1(opts = {}) {
let name = '每日新增有效bug數';
let dateRange = opts.dateRange;
let body = {
"aggs": {
"2": {
"terms": {
"field": "team",
"size": 100,
"order": {
"_count": "desc"
}
}
}
},
"size": 0,
"_source": {
"excludes": []
},
"stored_fields": ["*"],
"script_fields": {},
"query": {
"bool": {
"must": [{
"bool": {
"must": [{
"terms": {
"resolution": ["", "fixed", "hold"]
}
}],
"must_not": [{
"terms": {
"status": ["rejected"]
}
}, {
"term": {
"versionReport": {
"value": "內測"
}
}
}, {
"term": {
"problemType": {
"value": "重複bug"
}
}
}]
}
}, {
"range": {
"created": {
"format": "strict_date_optional_time",
"gte": dateRange[0],
"lte": dateRange[1]
}
}
}, {
"bool": {
"must": [{
"terms": {
"resolution": ["", "fixed", "hold"]
}
}],
"must_not": [{
"terms": {
"status": ["rejected"]
}
}, {
"term": {
"versionReport": {
"value": "內測"
}
}
}, {
"term": {
"problemType": {
"value": "重複bug"
}
}
}]
}
}],
"filter": [{
"match_all": {}
}, {
"match_all": {}
}],
"should": [],
"must_not": []
}
},
"timeout": "30000ms"
};
return this.ctx.service.es.base.searchBucketsAndCountFromFsBugInfo({
body,
name
});
}
let data1 = await this.getAggr1(opts);
參考鏈接:
https://www.npmjs.com/package/elasticsearch
https://is-log-kibana.corp.kuaishou.com/app/kibana#/dev_tools/console