Skip to content

Commit 31afe64

Browse files
committed
chore(generateSearch): parallel save and reduce printing
1 parent 94dc916 commit 31afe64

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

scripts/generateSearch.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,11 @@ async function run() {
139139

140140
const contents = generateContents();
141141

142-
let count = 0;
142+
const promises = [];
143+
let lastPrint = 0;
144+
145+
let doneCount = 0;
146+
console.log('Search Content to save:', contents.length);
143147
for (const content of contents) {
144148
if (version === '8.x') {
145149
let url = content.url.startsWith('/') ? content.url : `/${content.url}`;
@@ -154,18 +158,28 @@ async function run() {
154158
}
155159
content.url = `/docs/${version}${url}`;
156160
}
157-
console.log(`${++count} / ${contents.length}`);
158-
await content.save();
161+
const promise = content.save().then(() => {
162+
doneCount += 1;
163+
const nowDate = Date.now();
164+
// only print every 2 seconds, or if it is the first or last element
165+
if (nowDate - lastPrint > 2000 || doneCount === contents.length || doneCount === 1) {
166+
lastPrint = nowDate;
167+
console.log(`${doneCount} / ${contents.length}`);
168+
}
169+
});
170+
promises.push(promise);
159171
}
160172

173+
await Promise.allSettled(promises);
174+
161175
const results = await Content.
162176
find({ $text: { $search: 'validate' }, version }, { score: { $meta: 'textScore' } }).
163177
sort({ score: { $meta: 'textScore' } }).
164178
limit(10);
165179

166180
console.log(results.map(res => res.url));
167181

168-
console.log(`Added ${contents.length} Content`);
182+
console.log(`Added ${contents.length} Search Content`);
169183

170184
process.exit(0);
171185
}

0 commit comments

Comments
 (0)