examples/node/helloworld/search_realtime.js
function(job, done) {
                var MAX_COUNT = 5;
                var count = 0;
                
                Async.whilst(
                    // Loop for N times
                    function() { return MAX_COUNT > count; },
                    // Every second, ask for preview results
                    function(iterationDone) {
                        Async.sleep(1000, function() {
                            job.preview({}, function(err, results) {
                                if (err) {
                                    iterationDone(err);
                                    return;
                                }
                                
                                // Only do something if we have results
                                if (results && results.rows) {
                                    // Up the iteration counter
                                    count++;
                                    
                                    console.log("========== Iteration " + count + " ==========");
                                    var sourcetypeIndex = results.fields.indexOf("sourcetype");
                                    var countIndex = results.fields.indexOf("count");
                                    
                                    for(var i = 0; i < results.rows.length; i++) {
                                        var row = results.rows[i];
                                        
                                        // This is a hacky "padding" solution
                                        var stat = ("  " + row[sourcetypeIndex] + "                         ").slice(0, 30);
                                        
                                        // Print out the sourcetype and the count of the sourcetype so far
                                        console.log(stat + row[countIndex]);   
                                    }
                                    
                                    console.log("=================================");
                                }
                                    
                                // And we're done with this iteration
                                iterationDone();
                            });
                        });
                    },
                    // When we're done looping, just cancel the job
                    function(err) {
                        job.cancel(done);
                    }
                );
            }
function(job, done) {
                var MAX_COUNT = 5;
                var count = 0;
                
                Async.whilst(
                    // Loop for N times
                    function() { return MAX_COUNT > count; },
                    // Every second, ask for preview results
                    function(iterationDone) {
                        Async.sleep(1000, function() {
                            job.preview({}, function(err, results) {
                                if (err) {
                                    iterationDone(err);
                                    return;
                                }
                                
                                // Only do something if we have results
                                if (results && results.rows) {
                                    // Up the iteration counter
                                    count++;
                                    
                                    console.log("========== Iteration " + count + " ==========");
                                    var sourcetypeIndex = results.fields.indexOf("sourcetype");
                                    var countIndex = results.fields.indexOf("count");
                                    
                                    for(var i = 0; i < results.rows.length; i++) {
                                        var row = results.rows[i];
                                        
                                        // This is a hacky "padding" solution
                                        var stat = ("  " + row[sourcetypeIndex] + "                         ").slice(0, 30);
                                        
                                        // Print out the sourcetype and the count of the sourcetype so far
                                        console.log(stat + row[countIndex]);   
                                    }
                                    
                                    console.log("=================================");
                                }
                                    
                                // And we're done with this iteration
                                iterationDone();
                            });
                        });
                    },
                    // When we're done looping, just cancel the job
                    function(err) {
                        job.cancel(done);
                    }
                );
            }
function ( job , done ) { var MAX_COUNT = 5 ; var count = 0 ; Async . whilst ( // Loop for N times function ( ) { return MAX_COUNT > count ; } , // Every second, ask for preview results function ( iterationDone ) { Async . sleep ( 1000 , function ( ) { job . preview ( { } , function ( err , results ) { if ( err ) { iterationDone ( err ) ; return ; } // Only do something if we have results if ( results && results . rows ) { // Up the iteration counter count ++ ; console . log ( "========== Iteration " + count + " ==========" ) ; var sourcetypeIndex = results . fields . indexOf ( "sourcetype" ) ; var countIndex = results . fields . indexOf ( "count" ) ; for ( var i = 0 ; i < results . rows . length ; i ++ ) { var row = results . rows [ i ] ; // This is a hacky "padding" solution var stat = ( "  " + row [ sourcetypeIndex ] + "                         " ) . slice ( 0 , 30 ) ; // Print out the sourcetype and the count of the sourcetype so far console . log ( stat + row [ countIndex ] ) ; } console . log ( "=================================" ) ; } // And we're done with this iteration iterationDone ( ) ; } ) ; } ) ; } , // When we're done looping, just cancel the job function ( err ) { job . cancel ( done ) ; } ) ; }
The search is never going to be done, so we simply poll it every second to get more results
The search is never going to be done so we simply poll it every second to get more results