Wednesday, 7 June 2017

How to search multiple keyowrds in file using nodejs?

Lets say i have multiple search keywords from client side searchStr that should match with the line in file , how i will match both text with each line in file and push that line to results.

search.js

var searchStr = [{
        "text": "3474889"
    }, {
        "text": "request"
    }];
    readStream = fs.createReadStream('./logs/' + filesData.searchEnv + '/' + logfile.filename, 'utf8')
    readStream.pipe(split())
        .on('data', function(line) {
            searchStr.forEach(function(text) {
                if (line.toLowerCase().indexOf(text.toLowerCase()) != -1)
                    var messageDateInfo = line.split('|')[0].replace(/[\[\]']+/g, '');
                messageDateInfo = new Date(messageDateInfo).getTime();
                searchStartDate = new Date(searchStartDate).getTime();
                searchEndDate = new Date(searchEndDate).getTime();
                if (messageDateInfo - searchStartDate > 0 && searchEndDate - messageDateInfo > 0) {
                    results.push({
                        filename: logfile.filename,
                        value: line
                    });
                }
            });
        });



via hussain

No comments:

Post a Comment