利用Node.js调用Elasticsearch

1. 下载elasticsearch库

npm install elasticsearch --save

2.在脚本里导入模块,如下所示

const elasticsearch = require('elasticsearch');

3.调用

function getReport(md5, callback) {
    var search =
    {
        index: 'file-behavior-report-*',
        type: 'report',
        body: {
            query: {
                "bool": {"must": [{"match": {"md5": md5.toUpperCase()}}], "must_not": [], "should": []}
            },
            "from": 0,
            "size": 1,
            "sort": [],
            "aggs": {}
        }
    };
    getES(search, function (data) {
        callback(data);
    });
}

function getES(searchInfo, callback) {
    const esClient = new elasticsearch.Client({
        host: 'http://193.168.15.210:9200/',
        log: 'error'
    });
    esClient.search(searchInfo).then(function (re) {
        callback(re.hits.hits);
    }, function (err) {
        console.trace(err.message);
    });
}

  

原文地址:https://www.cnblogs.com/guozhe/p/6427490.html