Skip to content
This repository was archived by the owner on Jan 23, 2025. It is now read-only.

Commit 848d6b6

Browse files
[Next Release] Enable V5 Elastic Search to be able to reach more than 10000 (#91)
* fix-es-query * switch sort key * refine code * refine code v2 * enable sync queue error log * reset ci
1 parent f6918ee commit 848d6b6

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

src/services/challengeService.js

+23-5
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ async function getChallengeIDsFromV4 (filter, perPage, page = 1) {
343343
* @param {Number} page
344344
* @returns {Object} { total, ids }
345345
*/
346-
async function getChallengeIDsFromV5 (filter, perPage, page = 1) {
346+
async function getChallengeIDsFromV5 (filter, perPage, lastDate) {
347347
// logger.warn(`getChallengeIDsFromV5 ${JSON.stringify(filter)} perPage ${perPage} page ${page}`)
348348
const boolQuery = []
349349
const mustQuery = []
@@ -373,7 +373,6 @@ async function getChallengeIDsFromV5 (filter, perPage, page = 1) {
373373
type: config.get('ES.CHALLENGE_ES_TYPE'),
374374
// refresh: config.get('ES.ES_REFRESH'),
375375
size: perPage,
376-
from: perPage * (page - 1),
377376
body: {
378377
_source: ['legacyId', 'id'],
379378
version: 'true',
@@ -390,6 +389,9 @@ async function getChallengeIDsFromV5 (filter, perPage, page = 1) {
390389
]
391390
}
392391
}
392+
if (lastDate) {
393+
esQuery.body.search_after = [Number(lastDate)]
394+
}
393395
// Search with constructed query
394396
let docs
395397
// logger.warn(`V5 Challenge IDs Query ${JSON.stringify(esQuery)}`)
@@ -407,14 +409,30 @@ async function getChallengeIDsFromV5 (filter, perPage, page = 1) {
407409
}
408410
// logger.warn(JSON.stringify(docs))
409411
// Extract data from hits
410-
if (docs.hits.total > 0) {
412+
let result = _.map(docs.hits.hits, item => item._source)
413+
logger.info(`ES Search Hits Total -> ${docs.hits.total}`)
414+
let newLastDate = null
415+
if (result.length > 0) {
416+
logger.info(`ES Search Result Length -> ${result.length}`)
417+
const endSortDate = docs.hits.hits[result.length - 1].sort
418+
if (endSortDate && endSortDate.length) {
419+
logger.info(`LastSortDate: ${JSON.stringify(endSortDate)}`)
420+
newLastDate = endSortDate[0]
421+
}
422+
logger.info(`newLastDate Timestamp: ${newLastDate}`)
411423
return {
412424
total: docs.hits.total,
413425
ids: _.map(docs.hits.hits, hit => _.toNumber(hit._source.legacyId)),
414-
v5Ids: _.map(docs.hits.hits, hit => hit._source.id)
426+
v5Ids: _.map(docs.hits.hits, hit => hit._source.id),
427+
lastDate: newLastDate
415428
}
416429
}
417-
return false
430+
return {
431+
total: docs.hits.total,
432+
ids: [],
433+
v5Ids: [],
434+
lastDate: newLastDate
435+
}
418436
}
419437

420438
async function getChallengeListingFromV4ES (legacyId) {

src/services/challengeSyncStatusService.js

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ async function getSyncProgress (filter, perPage = 100, page = 1) {
7373
docs = await getESClient().search(esQuery)
7474
} catch (e) {
7575
// Catch error when the ES is fresh and has no data
76+
logger.error(`Sync Queue Challenge IDs try/catch ${JSON.stringify(e)}`)
7677
docs = {
7778
hits: {
7879
total: 0,

src/services/syncService.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -213,20 +213,21 @@ async function getV4ChallengeIds (filter) {
213213
}
214214

215215
async function getV5LegacyChallengeIds (filter) {
216-
let page = 1
216+
let lastKey = null
217217
let running = true
218218
let v5Ids = []
219219
const perPage = 1000
220220
let combinedTotal = 0
221221

222222
while (running) {
223223
// logger.debug(`V5 Challenge IDs - Getting ${page}`)
224-
const { total, ids } = await challengeService.getChallengeIDsFromV5(filter, perPage, page)
224+
const { total, ids, lastDate } = await challengeService.getChallengeIDsFromV5(filter, perPage, lastKey)
225+
// Record the last call's end timestamp
226+
lastKey = lastDate;
225227
if (ids && ids.length > 0) {
226228
// logger.warn(`IDs ${JSON.stringify(ids)}`)
227229
combinedTotal = total
228230
v5Ids = _.concat(v5Ids, ids)
229-
page += 1
230231
} else {
231232
running = false
232233
}

0 commit comments

Comments
 (0)