[questions][feat] add text search (#412)

pull/427/head
hpkoh 2 years ago committed by GitHub
parent 7589e9b078
commit f70caba3f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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

@ -414,6 +414,9 @@ model QuestionsQuestion {
answers QuestionsAnswer[]
QuestionsListQuestionEntry QuestionsListQuestionEntry[]
contentSearch Unsupported("TSVECTOR")?
@@index([contentSearch])
@@index([lastSeenAt, id])
@@index([upvotes, id])
}

@ -316,6 +316,31 @@ export const questionsQuestionRouter = createProtectedRouter()
return question;
},
})
.query('getRelatedQuestionsByContent', {
input: z.object({
content: z.string(),
}),
async resolve({ ctx, input }) {
const escapeChars = /[()|&:*!]/g;
const query =
input.content
.replace(escapeChars, " ")
.trim()
.split(/\s+/)
.join(" | ");
const relatedQuestions = await ctx.prisma.$queryRaw`
SELECT * FROM "QuestionsQuestion"
WHERE
"contentSearch" @@ to_tsquery('english', ${query})
ORDER BY ts_rank("textSearch", to_tsquery('english', ${query})) DESC
`;
return relatedQuestions;
}
})
.mutation('create', {
input: z.object({
companyId: z.string(),
@ -537,7 +562,7 @@ export const questionsQuestionRouter = createProtectedRouter()
const incrementValue = voteToDelete.vote === Vote.UPVOTE ? -1 : 1;
const [questionVote] = await ctx.prisma.$transaction([
const [ questionVote ] = await ctx.prisma.$transaction([
ctx.prisma.questionsQuestionVote.delete({
where: {
id: input.id,

Loading…
Cancel
Save