[questions][feat] add text search

pull/412/head
hpkoh 3 years ago
parent ff9cffa715
commit 69b02e50e2

@ -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");

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

@ -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(),

Loading…
Cancel
Save