前端日志监控 - Nodejs 通过 ElasticSearch 查询数据流程

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

你可能感兴趣的:(软件研发,搜索引擎,elasticsearch,数据建模,javac,mooc)