mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-18 01:10:14 +01:00
feat: search in progress
This commit is contained in:
parent
9995a159aa
commit
6d2f3361d0
6 changed files with 84 additions and 24 deletions
29
api/lib/utils/mergeSort.js
Normal file
29
api/lib/utils/mergeSort.js
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
function mergeSort(arr, compareFn) {
|
||||
if (arr.length <= 1) {
|
||||
return arr;
|
||||
}
|
||||
|
||||
const mid = Math.floor(arr.length / 2);
|
||||
const leftArr = arr.slice(0, mid);
|
||||
const rightArr = arr.slice(mid);
|
||||
|
||||
return merge(mergeSort(leftArr, compareFn), mergeSort(rightArr, compareFn), compareFn);
|
||||
}
|
||||
|
||||
function merge(leftArr, rightArr, compareFn) {
|
||||
const result = [];
|
||||
let leftIndex = 0;
|
||||
let rightIndex = 0;
|
||||
|
||||
while (leftIndex < leftArr.length && rightIndex < rightArr.length) {
|
||||
if (compareFn(leftArr[leftIndex], rightArr[rightIndex]) < 0) {
|
||||
result.push(leftArr[leftIndex++]);
|
||||
} else {
|
||||
result.push(rightArr[rightIndex++]);
|
||||
}
|
||||
}
|
||||
|
||||
return result.concat(leftArr.slice(leftIndex)).concat(rightArr.slice(rightIndex));
|
||||
}
|
||||
|
||||
module.exports = mergeSort;
|
||||
26
api/lib/utils/reduceHits.js
Normal file
26
api/lib/utils/reduceHits.js
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
const mergeSort = require('./mergeSort');
|
||||
|
||||
function reduceHits(hits) {
|
||||
const counts = {};
|
||||
|
||||
for (const hit of hits) {
|
||||
if (!counts[hit.conversationId]) {
|
||||
counts[hit.conversationId] = 1;
|
||||
} else {
|
||||
counts[hit.conversationId]++;
|
||||
}
|
||||
}
|
||||
|
||||
const result = [];
|
||||
|
||||
for (const [conversationId, count] of Object.entries(counts)) {
|
||||
result.push({
|
||||
conversationId,
|
||||
count
|
||||
});
|
||||
}
|
||||
|
||||
return mergeSort(result, (a, b) => b.count - a.count);
|
||||
}
|
||||
|
||||
module.exports = reduceHits;
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const { Message } = require('../../models/Message');
|
||||
const reduceHits = require('../../lib/utils/reduceHits');
|
||||
// const { MeiliSearch } = require('meilisearch');
|
||||
|
||||
router.get('/sync', async function (req, res) {
|
||||
|
|
@ -15,8 +16,14 @@ router.get('/sync', async function (req, res) {
|
|||
router.get('/', async function (req, res) {
|
||||
const { q } = req.query;
|
||||
const result = await Message.meiliSearch({ query: q });
|
||||
console.log(result);
|
||||
res.send(result);
|
||||
const sortedHits = reduceHits(result.hits);
|
||||
console.log(sortedHits);
|
||||
res.send(sortedHits);
|
||||
});
|
||||
|
||||
router.get('/clear', async function (req, res) {
|
||||
await Message.resetIndex();
|
||||
res.send('cleared');
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue