Skip to content

Commit

Permalink
Merge pull request danny-avila#122 from danny-avila/fix-search
Browse files Browse the repository at this point in the history
Fix search
  • Loading branch information
danny-avila authored Mar 24, 2023
2 parents cfc5b1b + a708bd1 commit 4d110a2
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 14 deletions.
2 changes: 1 addition & 1 deletion api/lib/utils/misc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const cleanUpPrimaryKeyValue = (value) => {
// For Bing convoId handling
return value.replace(/--/g, '-');
return value.replace(/--/g, '|');
};

function replaceSup(text) {
Expand Down
12 changes: 7 additions & 5 deletions api/lib/utils/reduceHits.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const mergeSort = require('./mergeSort');
const { cleanUpPrimaryKeyValue } = require('./misc');

function reduceMessages(hits) {
const counts = {};
Expand Down Expand Up @@ -29,15 +30,16 @@ function reduceHits(hits, titles = []) {
const convos = [...hits, ...titles];

for (const convo of convos) {
if (!counts[convo.conversationId]) {
counts[convo.conversationId] = 1;
const currentId = cleanUpPrimaryKeyValue(convo.conversationId);
if (!counts[currentId]) {
counts[currentId] = 1;
} else {
counts[convo.conversationId]++;
counts[currentId]++;
}

if (convo.title) {
// titleMap[convo.conversationId] = convo._formatted.title;
titleMap[convo.conversationId] = convo.title;
// titleMap[currentId] = convo._formatted.title;
titleMap[currentId] = convo.title;
}
}

Expand Down
3 changes: 1 addition & 2 deletions api/models/Conversation.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// const { Conversation } = require('./plugins');
const Conversation = require('./schema/convoSchema');
const { cleanUpPrimaryKeyValue } = require('../lib/utils/misc');
const { getMessages, deleteMessages } = require('./Message');

const getConvo = async (user, conversationId) => {
Expand Down Expand Up @@ -90,7 +89,7 @@ module.exports = {
promises.push(
Conversation.findOne({
user,
conversationId: cleanUpPrimaryKeyValue(convo.conversationId)
conversationId: convo.conversationId,
}).exec()
)
);
Expand Down
7 changes: 5 additions & 2 deletions api/server/routes/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ router.get('/sync', async function (req, res) {

router.get('/', async function (req, res) {
try {
const user = req?.session?.user?.username;
let user = req?.session?.user?.username;
user = user ?? null;
const { q } = req.query;
const pageNumber = req.query.pageNumber || 1;
const key = `${user || ''}${q}`;
Expand Down Expand Up @@ -50,8 +51,10 @@ router.get('/', async function (req, res) {
};
});
const titles = (await Conversation.meiliSearch(q)).hits;
console.log('message hits:', messages.length, 'convo hits:', titles.length);
const sortedHits = reduceHits(messages, titles);
// debugging:
// console.log('user:', user, 'message hits:', messages.length, 'convo hits:', titles.length);
// console.log('sorted hits:', sortedHits.length);
const result = await getConvosQueried(user, sortedHits, pageNumber);

const activeMessages = [];
Expand Down
11 changes: 7 additions & 4 deletions client/src/components/Messages/Content/Content.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import rehypeRaw from 'rehype-raw'
import CodeBlock from './CodeBlock';
import { langSubset } from '~/utils/languages';

const Content = React.memo(({ content, isCreatedByUser = false }) => {
const rehypePlugins = [
const Content = React.memo(({ content, isCreatedByUser = true }) => {
let rehypePlugins = [
[rehypeKatex, { output: 'mathml' }],
[
rehypeHighlight,
Expand All @@ -20,12 +20,15 @@ const Content = React.memo(({ content, isCreatedByUser = false }) => {
}
],
[rehypeRaw],
]
];

rehypePlugins = isCreatedByUser ? rehypePlugins.slice(0, -1) : rehypePlugins;

return (
<>
<ReactMarkdown
remarkPlugins={[remarkGfm, [remarkMath, { singleDollarTextMath: false }]]}
rehypePlugins={isCreatedByUser ? rehypePlugins.slice(-1) : rehypePlugins}
rehypePlugins={rehypePlugins}
linkTarget="_new"
components={{
code,
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ services:
- ./api:/api
- /api/node_modules
mongodb:
ports:
- 27018:27017
image: mongo
restart: always
container_name: mongodb
Expand Down

0 comments on commit 4d110a2

Please sign in to comment.