简体   繁体   中英

How to get total count or total items of an index in elasticsearch using nodejs

I am trying to get the total count of an index of elasticsearch using nodejs, but i don't know how to get the count. Can anyone please help me in this

According to their nodejs client documentation ; you can use count api.

First: after install package ; you should declare client object to use it to make requests:

const elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
    // depends on your configs
    host: 'http://localhost:9200',
    // your elasticsearch version
    apiVersion: '6.8'
});

Second: implement count functionality:

client.count({
    // required index
    index: 'indexname',
    body: {
        // you can count based on specific query or remove body at all
        query: { match_all: {} }
    }
})
.then(res => {
    // do whatever you want 
    console.log(res.count);
})
.catch(err => { 
    // handle error
});

That's it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM