mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-02-02 15:51:49 +01:00
feat: search working as expected
This commit is contained in:
parent
da42d6272a
commit
4197a92609
5 changed files with 60 additions and 51 deletions
|
|
@ -59,7 +59,7 @@ convoSchema.plugin(mongoMeili, {
|
|||
host: process.env.MEILI_HOST,
|
||||
apiKey: process.env.MEILI_KEY,
|
||||
indexName: 'convos', // Will get created automatically if it doesn't exist already
|
||||
primaryKey: 'conversationId',
|
||||
primaryKey: 'conversationId'
|
||||
});
|
||||
|
||||
const Conversation =
|
||||
|
|
@ -142,15 +142,40 @@ module.exports = {
|
|||
return { conversations: [], pages: 1, pageNumber, pageSize };
|
||||
}
|
||||
|
||||
const promises = convoIds.map(convo => {
|
||||
return Conversation.findOne({ user, conversationId: convo.conversationId}).exec();
|
||||
const cache = {};
|
||||
const promises = [];
|
||||
|
||||
convoIds.forEach((convo, i) => {
|
||||
const page = Math.floor(i / pageSize) + 1;
|
||||
if (!cache[page]) {
|
||||
cache[page] = [];
|
||||
}
|
||||
|
||||
const conversation = Conversation.findOne({
|
||||
user,
|
||||
conversationId: convo.conversationId
|
||||
}).exec();
|
||||
|
||||
cache[page].push(conversation);
|
||||
promises.push(conversation);
|
||||
});
|
||||
const results = (await Promise.all(promises)).filter(convo => convo);
|
||||
const startIndex = (pageNumber - 1) * pageSize;
|
||||
const convos = results.slice(startIndex, startIndex + pageSize);
|
||||
const results = (await Promise.all(promises)).filter((convo) => convo);
|
||||
for (const key in cache) {
|
||||
const promises = cache[key];
|
||||
cache[key] = (await Promise.all(promises)).filter((convo) => convo);
|
||||
}
|
||||
// const startIndex = (pageNumber - 1) * pageSize;
|
||||
// const convos = results.slice(startIndex, startIndex + pageSize);
|
||||
const totalPages = Math.ceil(results.length / pageSize);
|
||||
console.log(results.length, totalPages, convos.length);
|
||||
return { conversations: convos, pages: totalPages, pageNumber, pageSize };
|
||||
cache.pages = totalPages;
|
||||
cache.pageSize = pageSize;
|
||||
return {
|
||||
cache,
|
||||
conversations: cache[pageNumber],
|
||||
pages: totalPages,
|
||||
pageNumber,
|
||||
pageSize
|
||||
};
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return { message: 'Error fetching conversations' };
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ const { Message } = require('../../models/Message');
|
|||
const { Conversation, getConvosQueried } = require('../../models/Conversation');
|
||||
const { reduceMessages, reduceHits } = require('../../lib/utils/reduceHits');
|
||||
// const { MeiliSearch } = require('meilisearch');
|
||||
const cache = new Map();
|
||||
|
||||
router.get('/sync', async function (req, res) {
|
||||
await Message.syncWithMeili();
|
||||
|
|
@ -13,18 +14,26 @@ router.get('/sync', async function (req, res) {
|
|||
|
||||
router.get('/', async function (req, res) {
|
||||
try {
|
||||
const user = req?.session?.user?.username;
|
||||
const { q } = req.query;
|
||||
console.log(req.query, req.params);
|
||||
const pageNumber = req.query.pageNumber || 1;
|
||||
const key = `${user || ''}${q}`;
|
||||
|
||||
if (cache.has(key)) {
|
||||
console.log('cache hit', key);
|
||||
const cached = cache.get(key);
|
||||
const { pages, pageSize } = cached;
|
||||
res.status(200).send({ conversations: cached[pageNumber], pages, pageNumber, pageSize });
|
||||
return;
|
||||
} else {
|
||||
cache.clear();
|
||||
}
|
||||
const message = await Message.meiliSearch(q);
|
||||
const title = await Conversation.meiliSearch(q, { attributesToHighlight: ['title'] });
|
||||
const sortedHits = reduceHits(message.hits, title.hits);
|
||||
const result = await getConvosQueried(
|
||||
req?.session?.user?.username,
|
||||
sortedHits,
|
||||
pageNumber
|
||||
);
|
||||
console.log('result', result.pageNumber, result.pages, result.pageSize);
|
||||
const result = await getConvosQueried(user, sortedHits, pageNumber);
|
||||
cache.set(q, result.cache);
|
||||
delete result.cache;
|
||||
res.status(200).send(result);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue