From c7e825446d585439b05e35af82444547ecdba494 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mercy=20Falcon=C3=AD?= Date: Fri, 21 May 2021 14:24:01 -0500 Subject: [PATCH 1/3] batch indexing with query that contains _id --- app/api/search/entitiesIndex.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/app/api/search/entitiesIndex.js b/app/api/search/entitiesIndex.js index 69a063b6c3..2b8b0c8000 100644 --- a/app/api/search/entitiesIndex.js +++ b/app/api/search/entitiesIndex.js @@ -81,9 +81,9 @@ const bulkIndex = async (docs, _action = 'index') => { return results; }; -const getEntitiesToIndex = async (query, stepIndex, limit, select) => { +const getEntitiesToIndex = async (query, stepBach, limit, select) => { const thisQuery = { ...query }; - thisQuery._id = !thisQuery._id ? { $gte: stepIndex } : thisQuery._id; + thisQuery._id = !thisQuery._id ? { $in: stepBach } : thisQuery._id; return entities.getUnrestrictedWithDocuments(thisQuery, '+permissions', { limit, documentsFullText: select && select.includes('+fullText'), @@ -98,11 +98,9 @@ const bulkIndexAndCallback = async assets => { const getSteps = async (query, limit) => { const allIds = await entities.getWithoutDocuments(query, '_id', { sort: { _id: 1 } }); - const milestoneIds = []; - for (let i = 0; i < allIds.length; i += limit) { - milestoneIds.push(allIds[i]); - } - return milestoneIds; + return [...Array(Math.ceil(allIds.length / limit))].map((_v, i) => + allIds.slice(i * limit, (i + 1) * limit) + ); }; /*eslint max-statements: ["error", 20]*/ @@ -110,12 +108,15 @@ const indexBatch = async (totalRows, options) => { const { query, select, limit, batchCallback, searchInstance } = options; const steps = await getSteps(query, limit); + if (query._id) { + delete query._id; + } const promisePool = new PromisePool(); const { errors: indexingErrors } = await promisePool .for(steps) .withConcurrency(10) - .process(async stepIndex => { - const entitiesToIndex = await getEntitiesToIndex(query, stepIndex, limit, select); + .process(async stepBatch => { + const entitiesToIndex = await getEntitiesToIndex(query, stepBatch, limit, select); if (entitiesToIndex.length > 0) { await bulkIndexAndCallback({ searchInstance, From aa287a4261447c8df75a37e0c138b6d1654c6be4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mercy=20Falcon=C3=AD?= Date: Fri, 21 May 2021 14:42:02 -0500 Subject: [PATCH 2/3] removes property with destructuring --- app/api/search/entitiesIndex.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/app/api/search/entitiesIndex.js b/app/api/search/entitiesIndex.js index 2b8b0c8000..4ba73033f0 100644 --- a/app/api/search/entitiesIndex.js +++ b/app/api/search/entitiesIndex.js @@ -108,15 +108,14 @@ const indexBatch = async (totalRows, options) => { const { query, select, limit, batchCallback, searchInstance } = options; const steps = await getSteps(query, limit); - if (query._id) { - delete query._id; - } + const { _id: remove, ...queryToIndex } = query; + const promisePool = new PromisePool(); const { errors: indexingErrors } = await promisePool .for(steps) .withConcurrency(10) .process(async stepBatch => { - const entitiesToIndex = await getEntitiesToIndex(query, stepBatch, limit, select); + const entitiesToIndex = await getEntitiesToIndex(queryToIndex, stepBatch, limit, select); if (entitiesToIndex.length > 0) { await bulkIndexAndCallback({ searchInstance, From dbbe049856819e153148a5a81089196568ca1acc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mercy=20Falcon=C3=AD?= Date: Fri, 21 May 2021 16:29:11 -0500 Subject: [PATCH 3/3] removing unneeded options/conditions --- app/api/search/entitiesIndex.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/api/search/entitiesIndex.js b/app/api/search/entitiesIndex.js index 4ba73033f0..80c20586c4 100644 --- a/app/api/search/entitiesIndex.js +++ b/app/api/search/entitiesIndex.js @@ -83,7 +83,7 @@ const bulkIndex = async (docs, _action = 'index') => { const getEntitiesToIndex = async (query, stepBach, limit, select) => { const thisQuery = { ...query }; - thisQuery._id = !thisQuery._id ? { $in: stepBach } : thisQuery._id; + thisQuery._id = { $in: stepBach }; return entities.getUnrestrictedWithDocuments(thisQuery, '+permissions', { limit, documentsFullText: select && select.includes('+fullText'), @@ -97,7 +97,7 @@ const bulkIndexAndCallback = async assets => { }; const getSteps = async (query, limit) => { - const allIds = await entities.getWithoutDocuments(query, '_id', { sort: { _id: 1 } }); + const allIds = await entities.getWithoutDocuments(query, '_id'); return [...Array(Math.ceil(allIds.length / limit))].map((_v, i) => allIds.slice(i * limit, (i + 1) * limit) );