Skip to content

Commit

Permalink
Update search to use Fuse.js
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiodxa committed Mar 26, 2024
1 parent f61b6c8 commit 77b7ece
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 43 deletions.
18 changes: 6 additions & 12 deletions app/models/article.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Database } from "~/services/db.server";
import type { UUID } from "~/utils/uuid";

import { and, eq } from "drizzle-orm";
import Fuse from "fuse.js";

import { Post } from "~/models/post.server";
import { Markdown } from "~/modules/md.server";
Expand Down Expand Up @@ -101,20 +102,13 @@ export class Article extends Post<ArticleMeta> {
static async search(services: Services, query: string) {
let articles = await Article.list(services);

let words = query
.trim()
.toLowerCase()
.split(/\s+/)
.filter((word) => word.length > 1);
query = query.trim().toLowerCase();

for (let word of words) {
articles = articles.filter((item) => {
let title = item.title.toLowerCase();
return title.includes(word);
});
}
let fuse = new Fuse(articles, {
keys: ["title", "content"],
});

return articles;
return fuse.search(query).map((result) => result.item);
}

static async findById(services: Services, id: UUID) {
Expand Down
19 changes: 7 additions & 12 deletions app/models/glossary.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { BaseMeta, PostAttributes } from "~/models/post.server";
import type { Database, Tables } from "~/services/db.server";
import type { UUID } from "~/utils/uuid";

import Fuse from "fuse.js";

import { Post } from "~/models/post.server";

interface GlossaryMeta extends BaseMeta {
Expand Down Expand Up @@ -86,19 +88,12 @@ export class Glossary extends Post<GlossaryMeta> {
static async search(services: Services, query: string) {
let glossary = await Glossary.list(services);

let words = query
.trim()
.toLowerCase()
.split(/\s+/)
.filter((word) => word.length > 1);
query = query.trim().toLowerCase();

for (let word of words) {
glossary = glossary.filter((item) => {
let term = item.term.toLowerCase();
return term.includes(word);
});
}
let fuse = new Fuse(glossary, {
keys: ["term", "title", "definition"],
});

return glossary;
return fuse.search(query).map((result) => result.item);
}
}
19 changes: 7 additions & 12 deletions app/models/like.server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { BaseMeta, PostAttributes } from "~/models/post.server";
import type { Database, Tables } from "~/services/db.server";

import Fuse from "fuse.js";

import { Post } from "~/models/post.server";

interface Services {
Expand Down Expand Up @@ -50,20 +52,13 @@ export class Like extends Post<LikeMeta> {
static async search({ db }: Services, query: string) {
let likes = await Like.list({ db });

let words = query
.trim()
.toLowerCase()
.split(/\s+/)
.filter((word) => word.length > 1);
query = query.trim().toLowerCase();

for (let word of words) {
likes = likes.filter((item) => {
let title = item.title.toLowerCase();
return title.includes(word);
});
}
let fuse = new Fuse(likes, {
keys: ["title"],
});

return likes;
return fuse.search(query).map((result) => result.item);
}

static override async show({ db }: Services, id: Tables.SelectPost["id"]) {
Expand Down
12 changes: 5 additions & 7 deletions app/models/tutorial.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Database } from "~/services/db.server";
import type { UUID } from "~/utils/uuid";

import { and, eq } from "drizzle-orm";
import Fuse from "fuse.js";
import * as semver from "semver";

import { Markdown } from "~/modules/md.server";
Expand Down Expand Up @@ -157,14 +158,11 @@ export class Tutorial extends Post<TutorialMeta> {
});
}

for (let word of query.trim()) {
tutorials = tutorials.filter((item) => {
let title = item.title.toLowerCase();
return title.includes(word);
});
}
let fuse = new Fuse(tutorials, {
keys: ["title", "content"],
});

return tutorials;
return fuse.search(query).map((result) => result.item);
}

static async findById(services: Services, id: UUID) {
Expand Down
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"dotenv": "^16.4.1",
"drizzle-orm": "^0.29.1",
"front-matter": "^4.0.2",
"fuse.js": "^7.0.0",
"htmlparser2": "^9.0.0",
"i18next": "^23.7.19",
"i18next-browser-languagedetector": "^7.1.0",
Expand Down

0 comments on commit 77b7ece

Please sign in to comment.