Skip to content

Commit

Permalink
fix(author-search): ♻️ remove flexsearch and just use mongodb search
Browse files Browse the repository at this point in the history
Since now it scores properly, this is a more reliable solution. Flexsearch was sometimes returning no results when it should have been

This may cause search to behave much differently; clients should be scoring themselves already though
  • Loading branch information
djdembeck committed Oct 14, 2021
1 parent c67670b commit 26435e1
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 60 deletions.
25 changes: 1 addition & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"fastify": "^3.22.0",
"fastify-plugin": "^3.0.0",
"fastify-redis": "^4.3.2",
"flexsearch": "^0.7.21",
"html-to-text": "^8.0.0",
"isomorphic-fetch": "^3.0.0",
"jsrsasign": "^10.4.1",
Expand All @@ -41,7 +40,6 @@
"typescript": "^4.4.4"
},
"devDependencies": {
"@types/flexsearch": "^0.7.1",
"@types/html-to-text": "^8.0.1",
"@types/isomorphic-fetch": "^0.0.35",
"@types/jest": "^27.0.2",
Expand Down
36 changes: 2 additions & 34 deletions src/config/routes/authors/search/show.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,4 @@
import Author from '../../../models/Author'
// Search
import { Document } from 'flexsearch'

const document = new Document({
document: {
id: 'asin',
index: [{
charset: 'latin:simple',
field: 'name',
tokenize: 'full'
}]
},
preset: 'score'
})

async function routes (fastify, options) {
fastify.get('/authors', async (request, reply) => {
Expand All @@ -29,28 +15,10 @@ async function routes (fastify, options) {
const searchDbByName = await Promise.resolve(
Author.find(
{ $text: { $search: name } },
{ projection: { _id: false, asin: true, name: true }, limit: 25, sort: { score: { $meta: 'textScore' } } },
{ projection: { _id: false, asin: true, name: true }, limit: 25, sort: { score: { $meta: 'textScore' } } }
)
)
// Add results to FlexSearch index
searchDbByName.forEach(result => {
document.add(result)
})
// Resulting search
const runFlexSearch = document.search(name)
if (runFlexSearch.length) {
const matchedResults = runFlexSearch[0].result as string[]
// Search documents matched by FlexSearch
const found = await Promise.resolve(
Author.find(
{ asin: { $in: matchedResults } }
)
)
if (found) {
return found
}
}
return []
return searchDbByName
}
})
}
Expand Down

0 comments on commit 26435e1

Please sign in to comment.