Skip to content

Instantly share code, notes, and snippets.

@24601
Last active July 28, 2017 02:49
Show Gist options
  • Save 24601/276694c7903a77b6391b34d2296fed52 to your computer and use it in GitHub Desktop.
Save 24601/276694c7903a77b6391b34d2296fed52 to your computer and use it in GitHub Desktop.
/**
* 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};
@24601
Copy link
Author

24601 commented Jul 27, 2017

To use:

  1. copy this file to your proj
  2. import it and call getHelper()
  3. call the setCxn and SetQuery to the appropriate set values (your query and your es connection from the es node sdk)
  4. call the shit out of next as req'd and you'll keep getting hits (preferably using a for await loop)

Best used when adding a method like .scrollStreaming (see below) to the node.js elastic sdk

                var asyncHelper = require("./vt-es-async-helper.js").getHelper()
                asyncHelper.setCxn(client)
                asyncHelper.setQuery(query)
                return asyncHelper.next;
            }

            client.scrollStream = streamFunction;```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment