scrollgenerator
Advanced tools
Comparing version 1.0.4 to 1.1.0
105
index.js
@@ -1,62 +0,71 @@ | ||
let index = null | ||
let query = null | ||
let size = 500 | ||
let cpt = 0 | ||
let client = null | ||
let index = null; | ||
let query = null; | ||
let sort = []; | ||
let size = 500; | ||
let cpt = 0; | ||
let client = null; | ||
function init(options) { | ||
client = options.client | ||
index = options.index | ||
query = options.query | ||
size = options.size | ||
client = options.client; | ||
index = options.index; | ||
query = options.query; | ||
sort = options.sort; | ||
size = options.size; | ||
} | ||
async function elasticSearcher(scroll_id) { | ||
if (scroll_id) { | ||
const { body } = await client.scroll({ | ||
scroll_id, | ||
scroll: '30s', | ||
}) | ||
return body | ||
} | ||
if (scroll_id) { | ||
const { body } = await client.scroll({ | ||
scroll_id, | ||
scroll: "30s", | ||
}); | ||
return body; | ||
} | ||
const { body } = await client.search({ | ||
index, | ||
size: size, | ||
scroll: '30s', | ||
body: { | ||
query: query, | ||
}, | ||
}) | ||
return body | ||
const { body } = await client.search({ | ||
index, | ||
size: size, | ||
scroll: "30s", | ||
body: { | ||
sort: sort, | ||
query: query, | ||
}, | ||
}); | ||
return body; | ||
} | ||
function* resultGenerator(condition = null, scroll_id = null) { | ||
if (scroll_id && condition) { | ||
while (condition) { | ||
yield elasticSearcher(scroll_id) | ||
} | ||
} else { | ||
if (!scroll_id) yield elasticSearcher() | ||
if (scroll_id && condition) { | ||
while (condition) { | ||
yield elasticSearcher(scroll_id); | ||
} | ||
} else { | ||
if (!scroll_id) yield elasticSearcher(); | ||
} | ||
} | ||
async function scrollOverIndex(options = { client: null, index: '', query: {}, size: 500 }, cb) { | ||
init(options) | ||
const generator = resultGenerator() | ||
let result = await generator.next().value | ||
cpt += result.hits.hits.length | ||
await cb(result) | ||
if (result.hits.total.value > cpt && result._scroll_id) { | ||
const resultIterator = resultGenerator(result.hits.total.value > cpt, result._scroll_id) | ||
let iteratorResult = resultIterator.next() | ||
while (!iteratorResult.done && cpt < result.hits.total.value) { | ||
result = await iteratorResult.value | ||
cpt += result.hits.hits.length | ||
await cb(result) | ||
iteratorResult = resultIterator.next() | ||
} | ||
} else return | ||
async function scrollOverIndex( | ||
options = { client: null, index: "", query: {}, sort: [], size: 500 }, | ||
cb | ||
) { | ||
init(options); | ||
const generator = resultGenerator(); | ||
let result = await generator.next().value; | ||
cpt += result.hits.hits.length; | ||
await cb(result); | ||
if (result.hits.total.value > cpt && result._scroll_id) { | ||
const resultIterator = resultGenerator( | ||
result.hits.total.value > cpt, | ||
result._scroll_id | ||
); | ||
let iteratorResult = resultIterator.next(); | ||
while (!iteratorResult.done && cpt < result.hits.total.value) { | ||
result = await iteratorResult.value; | ||
cpt += result.hits.hits.length; | ||
await cb(result); | ||
iteratorResult = resultIterator.next(); | ||
} | ||
} else return; | ||
} | ||
module.exports = scrollOverIndex | ||
module.exports = scrollOverIndex; |
{ | ||
"name": "scrollgenerator", | ||
"version": "1.0.4", | ||
"description": "is lite and quick set up package to scroll over elastic search index built using built in node js generator", | ||
"version": "1.1.0", | ||
"description": "is lite and quick set up package to scroll over elastic search index using built in node js generator", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
# Scroll Generator | ||
is lite and quick set up package to scroll over elastic search index built using built in node js generator | ||
## How it work | ||
all you have to do to scroll over an index is to give two params and done | ||
- first import your elastic search client | ||
- first import your elastic search client | ||
example : | ||
```js | ||
import client from '../libs/elasticsearch' | ||
``` | ||
```js | ||
import client from "../libs/elasticsearch"; | ||
``` | ||
- import scrollOverIndex : | ||
```js | ||
import scrollOverIndex from 'scrollgenerator' | ||
``` | ||
```js | ||
import scrollOverIndex from "scrollgenerator"; | ||
``` | ||
- write your optional options parameter | ||
```js | ||
```js | ||
let options = { | ||
client: client, | ||
index: 'my_index', | ||
query: { | ||
"match_all": {} | ||
client: client, | ||
index: "my_index", | ||
sort: [ | ||
{ | ||
"my_field": { | ||
order: "asc", | ||
}, | ||
}, | ||
size: 500, | ||
], | ||
query: { | ||
match_all: {}, | ||
}, | ||
size: 500, | ||
}; | ||
``` | ||
- note : All of these options are required exepct size (default = 500) | ||
* finally write executable code with the imported scrollOverIndex function and scrollRun callback | ||
```js | ||
async function main() { | ||
await scrollOverIndex(options, scrollRun); | ||
console.log("scrolling over index done :)"); | ||
} | ||
``` | ||
+ note : All of these options are required exepct size (default = 500) | ||
- finally write executable code with the imported scrollOverIndex function and scrollRun callback | ||
```js | ||
async function main() { | ||
await scrollOverIndex(options, scrollRun) | ||
console.log('scrolling over index done :)') | ||
main(); | ||
``` | ||
- the scrollRun will run in evey batch of hits | ||
* example of scrollRun callback : | ||
```js | ||
async function scrollRun(result) { | ||
for (let item of result.hits.hits) { | ||
console.log(`data length :|| ${result.hits.total.value}`); | ||
console.log(item); | ||
} | ||
main() | ||
``` | ||
- the scrollRun will run in evey batch of hits | ||
+ example of scrollRun callback : | ||
```js | ||
async function scrollRun(result) { | ||
for (let item of result.hits.hits) { | ||
console.log(`data length :|| ${result.hits.total.value}`) | ||
console.log(item); | ||
} | ||
return | ||
} | ||
``` | ||
return; | ||
} | ||
``` |
4947
65
67