[questions][feat] add text search

This commit is contained in:
hpkoh
2022-10-15 12:08:38 +08:00
parent ff9cffa715
commit 69b02e50e2
3 changed files with 37 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
-- AlterTable
ALTER TABLE "QuestionsQuestion" ADD COLUMN "contentSearch" TSVECTOR
GENERATED ALWAYS AS
to_tsvector('english', coalesce(content, ''))
STORED;
-- CreateIndex
CREATE INDEX "QuestionsQuestion_contentSearch_idx" ON "QuestionsQuestion" USING GIN("textSearch");

View File

@@ -374,6 +374,10 @@ model QuestionsQuestion {
votes QuestionsQuestionVote[]
comments QuestionsQuestionComment[]
answers QuestionsAnswer[]
contentSearch Unsupported("TSVECTOR")?
@@index([contentSearch])
}
model QuestionsQuestionEncounter {

View File

@@ -182,6 +182,31 @@ export const questionsQuestionRouter = createProtectedRouter()
return question;
},
})
.query('getRelatedQuestionsByContent', {
input: z.object({
content: z.string(),
pageNum: z.number(),
pageSize: z.number(),
}),
async resolve({ ctx, input }) {
const escapeChars = /[()|&:*!]/g;
const query =
input.content
.replace(escapeChars, " ")
.trim()
.split(/\s+/)
.join(" | ");
const res = await ctx.prisma.$queryRaw`
SELECT content FROM "Post"
WHERE
"contentSearch" @@ to_tsquery('english', ${query})
ORDER BY ts_rank("textSearch", to_tsquery('english', ${query})) DESC
LIMIT 10;
`;
}
})
.mutation('create', {
input: z.object({
company: z.string(),