Skip to content

Commit

Permalink
Still use omdb top result
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Sarmiento committed Dec 23, 2023
1 parent 9a9bd3f commit 143b20a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/pages/api/search/title.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ const handler: NextApiHandler = async (req, res) => {
...omdbResults,
...mdbResults.filter((mdbResult) => !omdbResults.find((r) => r.id === mdbResult.id)),
];
// pop the first result
const firstResult = results.shift();
// sort result by levenstein distance to search term
// start by computing the levenstein distance for each result
results.forEach((result) => {
Expand Down Expand Up @@ -171,6 +173,10 @@ const handler: NextApiHandler = async (req, res) => {
}
}
}
// put the first result back at the top
if (firstResult) {
results.unshift(firstResult);
}

await db.saveSearchResults(keyword.toString().trim(), results);

Expand Down
1 change: 1 addition & 0 deletions src/pages/search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ function Search() {
const data = await res.json();
if (data.error) throw new Error(data.error);
setSearchResults(data.results);
if (data.results.length === 0) fetchMiscData(q);
} catch (error: any) {
setErrorMessage(error.message);
fetchMiscData(q);
Expand Down

0 comments on commit 143b20a

Please sign in to comment.