summaryrefslogtreecommitdiff
path: root/database.js
blob: 32365e7710b3d61a8a7a88b89ee772a8a04db47a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const elasticsearch = require('elasticsearch');
const esClient = new elasticsearch.Client({
    host: '127.0.0.1:9200',
    log: 'error'
});

module.exports = {
    index: (index, type, data) => {
        let bulkBody = [];

        data.forEach(item => {
            bulkBody.push({
                index: {
                    _index: index,
                    _type: type,
                    _id: item.id
                }
            });

            bulkBody.push(item);
        });

        esClient.bulk({body: bulkBody})
            .then(response => {
                let errorCount = 0;
                response.items.forEach(item => {
                    if (item.index && item.index.error) {
                        console.log(++errorCount, item.index.error);
                    }
                });
                console.log(`Successfully indexed item`);
            })
            .catch(console.err);
    },
    exists: (index, type, id, callback) => {
        return esClient.exists({
            index: index,
            type: type,
            id: id
        })
    },
    search: (index, body) => {
        return esClient.search({index: index, body: body});
    }
};