Last active
July 28, 2017 02:49
-
-
Save 24601/276694c7903a77b6391b34d2296fed52 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by basitmustafa on 7/27/17. | |
* | |
* Do what you want with this. No warranties. This code sucks. If you improve it, I would love a better copy sent back and I'll update. | |
* | |
* [email protected] | |
* 24601 @github | |
*/ | |
function getHelper() { | |
var retVal = {} | |
var query; | |
var response = null; | |
var index = 0; | |
var cxn; | |
var done = false; | |
async function* next() { | |
if (response == null) { | |
response = await cxn.search(query); | |
index = 0; | |
} | |
while (!done) { | |
if (response && response.hits && response.hits.hits && response.hits.hits.length > 0 && index < response.hits.hits.length) { | |
++index; | |
yield response.hits.hits[index - 1]; | |
} | |
else if (response && response.hits && response.hits.hits && response.hits.hits.length > 0 && index >= response.hits.hits.length) { | |
response = await cxn.scroll({scroll: query.scroll, scrollId: response._scroll_id}); | |
index = 0; | |
} | |
else { | |
done = true; | |
} | |
} | |
} | |
function setCxn(connection) { | |
cxn = connection; | |
} | |
function setQuery(theQuery) { | |
query = theQuery; | |
} | |
retVal.setCxn = setCxn; | |
retVal.setQuery = setQuery; | |
retVal.next = next; | |
return retVal; | |
} | |
export {getHelper}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use:
Best used when adding a method like .scrollStreaming (see below) to the node.js elastic sdk