fix(meilisearch): results will now properly paginate

This commit is contained in:
Danny Avila 2023-07-28 13:16:41 -04:00 committed by Danny Avila
parent 428fd5bed8
commit a2b6e9a6a8
2 changed files with 10 additions and 20 deletions

View file

@ -54,8 +54,6 @@ module.exports = {
const cache = {};
const convoMap = {};
const promises = [];
// will handle a syncing solution soon
const deletedConvoIds = [];
convoIds.forEach((convo) =>
promises.push(
@ -66,23 +64,17 @@ module.exports = {
),
);
const results = (await Promise.all(promises)).filter((convo, i) => {
if (!convo) {
deletedConvoIds.push(convoIds[i].conversationId);
return false;
} else {
const page = Math.floor(i / pageSize) + 1;
if (!cache[page]) {
cache[page] = [];
}
cache[page].push(convo);
convoMap[convo.conversationId] = convo;
return true;
const results = (await Promise.all(promises)).filter(Boolean);
results.forEach((convo, i) => {
const page = Math.floor(i / pageSize) + 1;
if (!cache[page]) {
cache[page] = [];
}
cache[page].push(convo);
convoMap[convo.conversationId] = convo;
});
// const startIndex = (pageNumber - 1) * pageSize;
// const convos = results.slice(startIndex, startIndex + pageSize);
const totalPages = Math.ceil(results.length / pageSize);
cache.pages = totalPages;
cache.pageSize = pageSize;
@ -92,8 +84,6 @@ module.exports = {
pages: totalPages || 1,
pageNumber,
pageSize,
// will handle a syncing solution soon
filter: new Set(deletedConvoIds),
convoMap,
};
} catch (error) {

View file

@ -153,7 +153,7 @@ const createMeiliMongooseModel = function ({ index, attributesToIndex }) {
},
{ _id: 1 },
),
);
).lean();
// Add additional data from mongodb into Meili search hits
const populatedHits = data.hits.map(function (hit) {
@ -162,7 +162,7 @@ const createMeiliMongooseModel = function ({ index, attributesToIndex }) {
const originalHit = _.find(hitsFromMongoose, query);
return {
...(originalHit ? originalHit.toJSON() : {}),
...(originalHit ?? {}),
...hit,
};
});