利用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);
    });
}

  

posted on 2017-02-22 09:51  水行者  阅读(9174)  评论(0编辑  收藏  举报

导航