使用nodejs搜索elasticsearch(DSL)

使用nodejs搜索elasticsearch
一、npm 安装elasticsearch模块
二、启动elasticsearch
三、elasticsearch查询语句(query DSL)
四、查询、删除内容、删除索引demo
查询
删除索引中日志的内容
删除索引
一、npm 安装elasticsearch模块
npm install elasticsearch
1
二、启动elasticsearch
启动的的elasticsearch服务,而非步骤一中安装的npm模块。对elasticsearch的介绍,我后面的博客会单独做介绍。
单击elasticsearch.bat,或者在cmd命令行输入以下命令

.\bin\elasticsearch.bat
1
三、elasticsearch查询语句(query DSL)
Elasticsearch提供了一种json风格的查询语言,称为Query DSL(Query domain-specific language)。查询语言功能很全面。
以下几篇博客,给大家参考

1.elasticsearch查询语句(query DSL)
2.ES 22 - Elasticsearch中如何进行日期(数值)范围查询
3.Elastic Search之Search API(Query DSL)、字段类查询、复合查询
4.Elasticsearch查询方法
5.Elasticsearch DSL 常用语法介绍
四、查询、删除内容、删除索引demo
查询
关于tags可以参考我的filebeat的配置filebeat重载配置文件:reload功能,添加的tags是为了搜索时方便,比如在此处,在此会只查询tags为"GUID"的日志信息。

(function () {
  'use strict';

  const elasticsearch = require('elasticsearch');
  const esClient = new elasticsearch.Client({
    host: '127.0.0.1:9200',
    log: 'error'

  });

  const search = function search(index, body) {
    return esClient.search({ index: index, body: body });
  };
  const test = function test() {
    // let body = {
    //   "query": {
    //     "constant_score": {
    //       "filter": {
    //         "term": {
    //           "tags": "c7f1771f-0c8f-4c75-9d71-3b8c4b6bd191"
    //         }
    //       }
    //     }
    //   }
    // }
    let body = {
      size: 100,
      from: 0,
      query: {
        bool: {
          must: [
            {
              match: {
                message: {
                  query: 'WARN ERROR INFO'
                }
              }
            },
          ],
          filter: [
            {
              range: {
                '@timestamp': {
                  gte: '2020-08-31T06:25:56.149Z',
                  lte: '2020-09-10T08:38:54.281Z'
                }
              }
           },
              // term tags是filebeta采集时加入的标签,在此会只查询tags为"GUID"的日志信息。
            {
              term: {
                    "tags": "GUID"
              }
            }
          ]
        }
      }
    };

    console.log(`retrieving documents with a combined bool query (displaying ${body.size} items at a time)...`);
    //search('opslog-systemlog-2020.08.10', body)
    search('ops*', body)
      .then(results => {
        console.log(`found ${results.hits.total} items in ${results.took}ms`);
        if (results.hits.total > 0) console.log(`returned article titles:`);
        results.hits.hits.forEach((hit, index) => console.log(`\t${body.from + ++index} - ${hit._source.title} (score: ${hit._score})`));
      })
      .catch(console.error);
  };

  test();

  module.exports = {
    search
  };
}());

删除索引中日志的内容
删除索引“opslog-systemlog-2020.08.10”在此时间段内message带有10的内容

(function () {
  'use strict';

  const elasticsearch = require('elasticsearch');
  const esClient = new elasticsearch.Client({
    host: '127.0.0.1:9200',
    log: 'error'
  });
  const deletecontent= function deletecontent(index, body) {
    return esClient.deleteByQuery({index: index, body: body});
  };

  // only for testing purposes
  // all calls should be initiated through the module
  const test = function test() {
    let body = {
      size: 100,
      // from: 0,
      query: {
        bool: {
          must: [
            {
              query_string: {
                query: '(message:*10)'
              }
            }
          ],
          filter: [
            {
              range: {
                '@timestamp': {
                  gte: '2020-08-10T08:34:15.960Z' ,
                  lte: '2020-08-10T08:38:54.281Z'
                }
              }
            }
          ]
        }
      }
    };

    console.log(`retrieving documents with a combined bool query (displaying ${body.size} items at a time)...`);
    deletecontent('opslog-systemlog-2020.08.10', body)
    .then(results => {
      console.log(`found ${results.hits.total} items in ${results.took}ms`);
      if (results.hits.total > 0) console.log(`returned article titles:`);
      results.hits.hits.forEach((hit, index) => console.log(`\t${body.from + ++index} - ${hit._source.title} (score: ${hit._score})`));
    })
    .catch(console.error);
  };

  test();

  module.exports = {
    deletecontent
  };
} ());

删除索引
删除索引“ops-systemlog-2020.08.05”,至于自动清理索引的功能,我会单独写一篇博客介绍。elasticsearch定时删除过期索引index

(function () {
    'use strict';
  
    const elasticsearch = require('elasticsearch');
    const esClient = new elasticsearch.Client({
      host: '127.0.0.1:9200',
      log: 'error'
    });
    const deleteindex = function deleteindex(index) {
      return esClient.indices.delete({index: index});
    };
  
    var index = ['ops-systemlog-2020.08.05']
    const test = function test() {
      deleteindex(index)
      .then(results => {
        console.log(results);
      })
      .catch(console.error);
    };
  
    test();
  
    module.exports = {
        deleteindex
    };
  } ());

参考:
【1】https://github.com/sitepoint-editors/node-elasticsearch-tutorial
【2】https://www.npmjs.com/package/elasticsearch
【3】https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/16.x/api-reference.html
【4】如何用 Node.js 和 Elasticsearch 构建搜索引擎https://www.oschina.net/translate/search-engine-node-elasticsearch?lang=chs&p=1
【5】如何用 Node.js 和 Elasticsearch 构建搜索引擎https://www.jianshu.com/p/598f941ee206
【6】nodejs之elasticsearch使用:基础篇(一)
【7】Nodejs基础使用Elasticsearch(二)
————————————————
版权声明:本文为CSDN博主「junxuezheng」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/junxuezheng/article/details/108369982

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值