分页不适用于数据表和 nodejs/elasticsearch 实现

Pagination doesn't work with datatables and nodejs/elasticsearch implementation

我正在使用 nodejs 作为 back/front-end 实现以快速 UI 概览 nodejs 中的索引,并且我想将 datatables 与服务器端处理一起使用以格式正确的 table.

显示数据

它很好地抓取了所有数据,但是它只是将所有记录转储到 table 上的一页。

型号:

const { Client } = require('@elastic/elasticsearch')

const client = new Client({
    node: '',
    auth: {
        username: '',
        password: ''
    }
})

module.exports = function getElasticData(callback/*, size*/) {
    //parseInt(inputSize) = size;
    client.search({
        index: 'anomalies02_bhc-lstm2',
        size: 10000,
        body: {
            query: {
                match_all: {}
            }
        }
    }, function (error, response, status) {
        if (error) {
            console.log("search error: " + error)
        }
        if (response) {
            var elasticList = [];
            //console.log("EL:" + JSON.stringify(response))
            response.body.hits.hits.forEach(function (hit) {
                elasticList.push(hit);
            })
            //callback(elasticList.sort());
            //console.log("SORTED LIST: " + JSON.stringify(elasticList))
            callback(elasticList);
        }
        else {
            console.log("<p>No results</p>");
        }
    });
}

控制器:

var elasticDataModel = require('../model/getDataElastic');

exports.getData = function (req, res) {
    elasticDataModel(function (elasticList) {

        var searchStr = req.body.search.value;
        var recordsTotal = 0;
        var recordsFiltered = 0;

        var size = parseInt(req.body.length);
        var recordsFiltered = elasticList.slice(0, size)
        console.log("LENGTH: " + JSON.stringify(req.body.length))
        console.log("FILTERED: " + JSON.stringify(recordsFiltered))

        //console.log(elasticList[0]._source);
        console.log(typeof parseInt(req.body.draw))
        console.log(elasticList.length)
        recordsTotal = elasticList.length;
        //console.log("DRAW " + req.body.draw);
        var data = JSON.stringify({
            "data": elasticList,
            "draw": parseInt(req.body.draw),
            "recordsTotal": recordsTotal,
            "recordsFiltered": recordsFiltered
        });

        res.send(data);

    });

}

HTML:

        $(document).ready(function () {
            var t = $('#example2').DataTable({
                "paging": true,
                "processing": true,
                "serverSide": true,
                'ajax': {
                    'type': 'POST',
                    'url': '/populateData'
                },
                'pageLength': 20,
                'lengthMenu': [5, 10, 20, 50, 100, 200, 500],
                'columns':
                    [
                        { 'data': '_id', "defaultContent": "", 'name': 'ID' },
                        { "defaultContent": "", 'name': 'Kibana Link' },
                        { 'data': '_source.Environment', "defaultContent": "", 'name': 'Environment' },
                        { 'data': '_source.Cause', "defaultContent": "", 'name': 'Downtime cause' },
                        { 'data': '_source.Start', "defaultContent": "", 'name': 'Detected start' },
                        { 'data': '_source.End', "defaultContent": "", 'name': 'Detected end' },
                        { "defaultContent": "", 'name': 'Actual start' },
                        { "defaultContent": "", 'name': 'Actual end' },
                        { "defaultContent": "", 'name': 'Reason category' },
                        { "defaultContent": "", 'name': 'Reason details' },
                        { "defaultContent": "", 'name': 'Submit' },
                    ],
                "columnDefs": [
                    {
                        "searchable": true,
                        "orderable": true,
                        "targets": 0
                    }
                ]

            });

        });

我在数据表论坛上的 Kevin 的帮助下相应地修改了我的代码,现在它可以完美地工作了。

更改代码:

控制器

var elasticDataModel = require('../model/getFilteredDataElastic');
var pageLength;
var pageStart;

exports.getData = function (req, res) {
    pageLength = req.body.length;
    pageStart = req.body.start
    console.log("MODEL Length: " + pageLength)
    elasticDataModel(pageLength, pageStart, function (elasticFilteredList) {

        var searchStr = req.body.search.value;
        var recordsTotal = 0;
        var recordsFiltered = 0;
        console.log("PAGE LENGTH: " + pageLength)
        if (req.body.search.value) {
            var regex = new RegExp(req.body.search.value, "i")
            searchStr = { $or: [{ '_id': regex }, { 'Environment': regex }, { 'Downtime cause': regex }, { 'Detected start': regex }, { 'Detected end': regex }, { 'Reason details': regex }] };
        }
        else {
            searchStr = {};
        }
        var size = parseInt(req.body.length);
        var recordsFiltered = elasticFilteredList

        var data = JSON.stringify({
            "data": elasticFilteredList,
            "draw": parseInt(req.body.draw),
            "recordsTotal": elasticFilteredList[0],
            "recordsFiltered": recordsFiltered
        });

        res.send(data);
    });

}

型号:

const { Client } = require('@elastic/elasticsearch')

const client = new Client({
    node: '',
    auth: {
        username: '',
        password: ''
    }
})

module.exports = function getElasticData(arg, arg2, callback) {
    console.log("CONTROLLER SIZE: " + arg)
    console.log("CONTROLLER FROM: " + arg2)

    client.search({
        index: 'anomalies02_bhc-lstm2',
        from: arg2,
        size: arg,
        body: {
            query: {
                match_all: {}
            }
        }
    }, function (error, response, status) {
        if (error) {
            console.log("search error: " + error)
        }
        if (response) {
            var elasticFilteredList = [];

            console.log("VALUE: " + response.body.hits.total.value);
            elasticFilteredList.push(response.body.hits.total.value);
            response.body.hits.hits.forEach(function (hit) {
                elasticFilteredList.push(hit);
            })
            callback(elasticFilteredList);
        }
        else {
            console.log("<p>No results</p>");
        }
    });
}