mirror of
https://github.com/yangshun/tech-interview-handbook.git
synced 2025-07-28 04:33:42 +08:00
[questions][feat] add text search (#412)
This commit is contained in:
@ -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,
|
||||
|
Reference in New Issue
Block a user