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

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM