Thursday, 8 June 2017

search multiple keywords in each line of file using Nodejs file system?

I have a search criteria where user will search multiple keywords in a file , so below i have each line data from file ,now my searchStr that is array of keyrwords from client. So i want to check these search keyowrds in each line in the file if it matches push that line to results, In below code if you search only one keyword it will give you results but if you have mutilple keywords its not sending any response ? Any idea how can i achieve this task ?

search.js

/*Example of search String  
var searchStr = [{"text":"457383",{"text":"requestPdr"}]
*/

     function asyncFiles(filesData) {
         async.eachSeries(filesData.logFiles, function(logfile, done) {
             // read file
             readStream = fs.createReadStream('./logs/' + filesData.searchEnv + '/' + logfile.filename, 'utf8')
             readStream.pipe(split())
                 .on('data', function(line) {
                     for (var i = 0; i < searchStr.length; i++) {
                         if (line.toLowerCase().indexOf(searchStr[i].toLowerCase()) != -1)
                             //            parseLog(line, prevLine);
                             //            else prevLine = line;
                             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) {
                             // console.log("message date is within this time range");
                             results.push({
                                 filename: logfile.filename,
                                 value: line
                             });
                         }
                     }
                 });
             done();
         }, function(err) {
             if (err) {
                 console.log('error', err);
             }
             readStream.on('end', function() {
                 callback(results);
             });
             results = [];
         });
     }



via hussain

No comments:

Post a Comment