I want to query similar posts but not the post itself included.
Here's my attempt:
export async function getSimilars(slug: string) {
const excludeThis = await getBySlug(slug)
const posts = await knex('posts')
.whereNot({ slug }) // Each post has its own unique slug, used nanoid(11).
.andWhere({ is_active: true })
.andWhere({ type: excludeThis.type })
.orWhere('keywords', 'ilike', `%${excludeThis.keywords}%`)
.orWhere('title', 'ilike', `%${excludeThis.title}%`)
.limit(10)
.orderBy(orderBy)
// Since posts includes excludeThis post, I have to filter it out here.
const result = posts.filter(p => p.slug !== slug)
return result
}
But my attempt query all posts that have similar keywords
and title
, which includes the post from whereNot
. I have to filter it out later.
How do I query the similar of a post without including the post itself?