-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #117 from danny-avila/search-final
Search final
- Loading branch information
Showing
40 changed files
with
989 additions
and
363 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"arrowParens": "avoid", | ||
"bracketSpacing": true, | ||
"endOfLine": "lf", | ||
"htmlWhitespaceSensitivity": "css", | ||
"insertPragma": false, | ||
"singleAttributePerLine": true, | ||
"bracketSameLine": false, | ||
"jsxBracketSameLine": false, | ||
"jsxSingleQuote": false, | ||
"printWidth": 110, | ||
"proseWrap": "preserve", | ||
"quoteProps": "as-needed", | ||
"requirePragma": false, | ||
"semi": true, | ||
"singleQuote": true, | ||
"tabWidth": 2, | ||
"trailingComma": "none", | ||
"useTabs": false, | ||
"vueIndentScriptAndStyle": false, | ||
"parser": "babel" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
const mongoose = require('mongoose'); | ||
const Conversation = mongoose.models.Conversation; | ||
const Message = mongoose.models.Message; | ||
const { MeiliSearch } = require('meilisearch'); | ||
let currentTimeout = null; | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
async function indexSync(req, res, next) { | ||
try { | ||
if (!process.env.MEILI_HOST || !process.env.MEILI_MASTER_KEY || !process.env.SEARCH) { | ||
throw new Error('Meilisearch not configured, search will be disabled.'); | ||
} | ||
|
||
const client = new MeiliSearch({ | ||
host: process.env.MEILI_HOST, | ||
apiKey: process.env.MEILI_MASTER_KEY | ||
}); | ||
|
||
const { status } = await client.health(); | ||
// console.log(`Meilisearch: ${status}`); | ||
const result = status === 'available' && !!process.env.SEARCH; | ||
|
||
if (!result) { | ||
throw new Error('Meilisearch not available'); | ||
} | ||
|
||
const messageCount = await Message.countDocuments(); | ||
const convoCount = await Conversation.countDocuments(); | ||
const messages = await client.index('messages').getStats(); | ||
const convos = await client.index('convos').getStats(); | ||
const messagesIndexed = messages.numberOfDocuments; | ||
const convosIndexed = convos.numberOfDocuments; | ||
|
||
console.log(`There are ${messageCount} messages in the database, ${messagesIndexed} indexed`); | ||
console.log(`There are ${convoCount} convos in the database, ${convosIndexed} indexed`); | ||
|
||
if (messageCount !== messagesIndexed) { | ||
console.log('Messages out of sync, indexing'); | ||
await Message.syncWithMeili(); | ||
} | ||
|
||
if (convoCount !== convosIndexed) { | ||
console.log('Convos out of sync, indexing'); | ||
await Conversation.syncWithMeili(); | ||
} | ||
} catch (err) { | ||
// console.log('in index sync'); | ||
if (err.message.includes('not found')) { | ||
console.log('Creating indices...'); | ||
currentTimeout = setTimeout(async () => { | ||
try { | ||
await Message.syncWithMeili(); | ||
await Conversation.syncWithMeili(); | ||
} catch (err) { | ||
console.error('Trouble creating indices, try restarting the server.'); | ||
} | ||
}, 750); | ||
} else { | ||
console.error(err); | ||
// res.status(500).json({ error: 'Server error' }); | ||
} | ||
} | ||
} | ||
|
||
process.on('exit', () => { | ||
console.log('Clearing sync timeouts before exiting...'); | ||
clearTimeout(currentTimeout); | ||
}); | ||
|
||
module.exports = indexSync; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const cleanUpPrimaryKeyValue = (value) => { | ||
// For Bing convoId handling | ||
return value.replace(/--/g, '-'); | ||
}; | ||
|
||
function replaceSup(text) { | ||
if (!text.includes('<sup>')) return text; | ||
const replacedText = text.replace(/<sup>/g, '^').replace(/\s+<\/sup>/g, '^'); | ||
return replacedText; | ||
} | ||
|
||
module.exports = { | ||
cleanUpPrimaryKeyValue, | ||
replaceSup | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.