62 lines
1.6 KiB
TypeScript
62 lines
1.6 KiB
TypeScript
import { redis } from '@/lib/redis'
|
|
|
|
export async function indexPoll(pollId: string, question: string, description: string, options: string[]) {
|
|
const searchText = [question, description, ...options]
|
|
.filter(Boolean)
|
|
.join(' ')
|
|
.toLowerCase()
|
|
|
|
const words = searchText.split(/\s+/).filter(w => w.length > 2)
|
|
|
|
for (const word of words) {
|
|
await redis.sadd(`search:word:${word}`, pollId)
|
|
}
|
|
}
|
|
|
|
export async function searchPolls(query: string): Promise<string[]> {
|
|
const words = query.toLowerCase().split(/\s+/).filter(w => w.length > 2)
|
|
|
|
if (words.length === 0) return []
|
|
|
|
const pollSets = await Promise.all(
|
|
words.map(word => redis.smembers(`search:word:${word}`))
|
|
)
|
|
|
|
const pollIds = pollSets[0] || []
|
|
const results = pollIds.filter(id =>
|
|
pollSets.every(set => set?.includes(id))
|
|
)
|
|
|
|
return results
|
|
}
|
|
|
|
export async function indexUser(fid: string, username: string, displayName?: string) {
|
|
const searchText = [username, displayName]
|
|
.filter(Boolean)
|
|
.join(' ')
|
|
.toLowerCase()
|
|
|
|
const words = searchText.split(/\s+/).filter(w => w.length > 2)
|
|
|
|
for (const word of words) {
|
|
await redis.sadd(`search:user:${word}`, fid)
|
|
}
|
|
}
|
|
|
|
export async function searchUsers(query: string): Promise<string[]> {
|
|
const words = query.toLowerCase().split(/\s+/).filter(w => w.length > 2)
|
|
|
|
if (words.length === 0) return []
|
|
|
|
const userSets = await Promise.all(
|
|
words.map(word => redis.smembers(`search:user:${word}`))
|
|
)
|
|
|
|
const fids = userSets[0] || []
|
|
const results = fids.filter(fid =>
|
|
userSets.every(set => set?.includes(fid))
|
|
)
|
|
|
|
return results
|
|
}
|