elasticsearch-loop
Advanced tools
Comparing version 0.2.0 to 0.3.0
155
index.js
var elasticsearch = require('elasticsearch') | ||
class elasticsearchLoop { | ||
class ElasticsearchLoop { | ||
constructor () { | ||
console.warn('This version is will deprecate soon please migrate to Promise based version. See read me for more information') | ||
this.initConstant() | ||
} | ||
constructor() { | ||
initConstant () { | ||
this.connection = null | ||
this.total = 0 | ||
this.dataAmount = 0 | ||
this.lastDataAmount = 0 | ||
this.queryDsl = 0 | ||
this.duplicateCount = 0 | ||
this.firstLoop = true | ||
this.debugMode = false | ||
this.initConstant() | ||
} | ||
this.loopCallback = null | ||
this.successCallback = null | ||
this.errorCallback = null | ||
} | ||
initConstant() { | ||
this.connection | ||
this.total = 0 | ||
this.dataAmount = 0 | ||
this.lastDataAmount = 0 | ||
this.queryDsl = 0 | ||
this.duplicateCount = 0 | ||
this.firstLoop = true | ||
this.debugMode = false | ||
connect (connectionDetail) { | ||
this.initConstant() | ||
this.connection = new elasticsearch.Client(connectionDetail) | ||
} | ||
this.loopCallback = null | ||
this.successCallback = null | ||
this.errorCallback = null | ||
} | ||
enableDebugMode () { | ||
this.debugMode = true | ||
} | ||
connect(connectionDetail) { | ||
this.initConstant() | ||
this.connection = new elasticsearch.Client(connectionDetail) | ||
printText (text, debugLog) { | ||
if (debugLog === true && this.debugMode === true) { | ||
console.log(text) | ||
} | ||
enableDebugMode() { | ||
this.debugMode = true | ||
if (!debugLog) { | ||
console.log(text) | ||
} | ||
} | ||
printText(text, debugLog) { | ||
if (debugLog == true && this.debugMode == true) { | ||
console.log(text) | ||
} | ||
nullFunction () { | ||
if (!debugLog) { | ||
console.log(text) | ||
} | ||
} | ||
query (queryDsl, loopCallback, successCallback, errorCallback) { | ||
if (!this.connection) { | ||
throw Error('Elasticsearch Connection is not defind.') | ||
} | ||
nullFunction() { | ||
if (!queryDsl) { | ||
throw Error('Elasticsearch Query is not defind.') | ||
} | ||
query(queryDsl, loopCallback, successCallback, errorCallback) { | ||
if (!this.connection) { | ||
throw Error('Elasticsearch Connection is not defind.') | ||
} | ||
this.loopCallback = loopCallback || this.printText | ||
if (!queryDsl) { | ||
throw Error('Elasticsearch Query is not defind.') | ||
} | ||
this.successCallback = successCallback || this.nullFunction | ||
this.loopCallback = loopCallback || this.printText | ||
this.errorCallback = errorCallback || this.printText | ||
this.queryDsl = queryDsl | ||
this.getMessageLoop() | ||
} | ||
this.successCallback = successCallback || this.nullFunction | ||
getMessageLoop () { | ||
this.queryDsl.from = 0 | ||
this.queryDsl.size = 300 | ||
this.errorCallback = errorCallback || this.printText | ||
this.queryDsl = queryDsl | ||
this.getMessageLoop() | ||
} | ||
this.printText('[Elasticsearch] Loop Start.', true) | ||
this.connection.search(this.queryDsl, this.getMoreUntilDone.bind(this)) | ||
} | ||
getMessageLoop() { | ||
this.queryDsl.from = 0 | ||
this.queryDsl.size = 300 | ||
getMoreUntilDone (error, response) { | ||
if (!error) { | ||
this.total = response.hits.total | ||
this.printText("[Elasticsearch] Loop Start.", true) | ||
this.connection.search(this.queryDsl, this.getMoreUntilDone.bind(this)) | ||
if (!this.firstLoop) { | ||
this.printText('[Elasticsearch] Fetched ' + this.dataAmount.toLocaleString() + ' From ' + this.total.toLocaleString(), true) | ||
} else { | ||
this.printText('[Elasticsearch] Total ' + this.total.toLocaleString() + ' Records', true) | ||
this.firstLoop = false | ||
} | ||
response.hits.hits.forEach((hit) => { | ||
this.loopCallback(hit) | ||
this.dataAmount++ | ||
}) | ||
} else { | ||
this.errorCallback(error) | ||
} | ||
getMoreUntilDone(error, response) { | ||
if (!error) { | ||
this.lastDataAmount = this.dataAmount | ||
this.total = response.hits.total | ||
if (!this.firstLoop) { | ||
this.printText("[Elasticsearch] Fetched " + this.dataAmount.toLocaleString() + " From " + this.total.toLocaleString(), true) | ||
} else { | ||
this.printText("[Elasticsearch] Total " + this.total.toLocaleString() + " Records", true) | ||
this.firstLoop = false | ||
} | ||
response.hits.hits.forEach((hit) => { | ||
this.loopCallback(hit) | ||
this.dataAmount++ | ||
}) | ||
} else { | ||
this.errorCallback(error) | ||
} | ||
this.lastDataAmount = this.dataAmount | ||
if (this.total !== this.dataAmount) { | ||
this.queryDsl.from = this.queryDsl.from + this.queryDsl.size | ||
this.connection.search(this.queryDsl, this.getMoreUntilDone.bind(this)) | ||
} else { | ||
this.printText("[Elasticsearch] Loop Ended.", true) | ||
this.successCallback() | ||
} | ||
if (this.total !== this.dataAmount) { | ||
this.queryDsl.from = this.queryDsl.from + this.queryDsl.size | ||
this.connection.search(this.queryDsl, this.getMoreUntilDone.bind(this)) | ||
} else { | ||
this.printText('[Elasticsearch] Loop Ended.', true) | ||
this.successCallback() | ||
} | ||
} | ||
} | ||
module.exports = new elasticsearchLoop() | ||
module.exports = new ElasticsearchLoop() |
{ | ||
"name": "elasticsearch-loop", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "", | ||
@@ -21,3 +21,11 @@ "main": "index.js", | ||
"elasticsearch": "^11.0.1" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^7.2.0", | ||
"eslint-config-standard": "^14.1.1", | ||
"eslint-plugin-import": "^2.21.2", | ||
"eslint-plugin-node": "^11.1.0", | ||
"eslint-plugin-promise": "^4.2.1", | ||
"eslint-plugin-standard": "^4.0.1" | ||
} | ||
} |
@@ -36,5 +36,22 @@ # Elasticsearch loop | ||
``` | ||
### Example (Promise Based) | ||
```javascript | ||
var elasticLoop = require("elasticsearch-loop/es"); | ||
// Connect Elasticsearch | ||
(async () => { | ||
const connect = elasticLoop.connect({ | ||
host: 'localhost:9200' | ||
}) | ||
const res = await connect.query({ | ||
index: 'main', | ||
q: 'time:[2016-01-01 TO 2016-12-31]', | ||
}) | ||
console.log(res) | ||
})() | ||
``` | ||
### Remark | ||
* This library is using scan & score method | ||
* Query and connection parameter reference from https://github.com/elastic/elasticsearch-js |
7596
5
187
57
6