-
Notifications
You must be signed in to change notification settings - Fork 554
/
search.ts
179 lines (169 loc) · 5.87 KB
/
search.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { sql } from 'kysely'
import { InvalidRequestError } from '@atproto/xrpc-server'
import Database from '../../db'
import { notSoftDeletedClause, DbRef, AnyQb } from '../../db/util'
import { GenericKeyset, paginate } from '../../db/pagination'
export const getUserSearchQuery = (
db: Database,
opts: {
term: string
limit: number
cursor?: string
includeSoftDeleted?: boolean
},
) => {
const { ref } = db.db.dynamic
const { term, limit, cursor, includeSoftDeleted } = opts
// Matching user accounts based on handle
const distanceAccount = distance(term, ref('handle'))
let accountsQb = getMatchingAccountsQb(db, { term, includeSoftDeleted })
accountsQb = paginate(accountsQb, {
limit,
cursor,
direction: 'asc',
keyset: new SearchKeyset(distanceAccount, ref('actor.did')),
})
// Matching profiles based on display name
const distanceProfile = distance(term, ref('displayName'))
let profilesQb = getMatchingProfilesQb(db, { term, includeSoftDeleted })
profilesQb = paginate(profilesQb, {
limit,
cursor,
direction: 'asc',
keyset: new SearchKeyset(distanceProfile, ref('actor.did')),
})
// Combine and paginate result set
return paginate(combineAccountsAndProfilesQb(db, accountsQb, profilesQb), {
limit,
cursor,
direction: 'asc',
keyset: new SearchKeyset(ref('distance'), ref('actor.did')),
})
}
// Takes maximal advantage of trigram index at the expense of ability to paginate.
export const getUserSearchQuerySimple = (
db: Database,
opts: {
term: string
limit: number
},
) => {
const { ref } = db.db.dynamic
const { term, limit } = opts
// Matching user accounts based on handle
const accountsQb = getMatchingAccountsQb(db, { term })
.orderBy('distance', 'asc')
.limit(limit)
// Matching profiles based on display name
const profilesQb = getMatchingProfilesQb(db, { term })
.orderBy('distance', 'asc')
.limit(limit)
// Combine and paginate result set
return paginate(combineAccountsAndProfilesQb(db, accountsQb, profilesQb), {
limit,
direction: 'asc',
keyset: new SearchKeyset(ref('distance'), ref('actor.did')),
})
}
// Matching user accounts based on handle
const getMatchingAccountsQb = (
db: Database,
opts: { term: string; includeSoftDeleted?: boolean },
) => {
const { ref } = db.db.dynamic
const { term, includeSoftDeleted } = opts
const distanceAccount = distance(term, ref('handle'))
return db.db
.selectFrom('actor')
.if(!includeSoftDeleted, (qb) =>
qb.where(notSoftDeletedClause(ref('actor'))),
)
.where('actor.handle', 'is not', null)
.where(similar(term, ref('handle'))) // Coarse filter engaging trigram index
.where(distanceAccount, '<', getMatchThreshold(term)) // Refines results from trigram index
.select(['actor.did as did', distanceAccount.as('distance')])
}
// Matching profiles based on display name
const getMatchingProfilesQb = (
db: Database,
opts: { term: string; includeSoftDeleted?: boolean },
) => {
const { ref } = db.db.dynamic
const { term, includeSoftDeleted } = opts
const distanceProfile = distance(term, ref('displayName'))
return db.db
.selectFrom('profile')
.innerJoin('actor', 'actor.did', 'profile.creator')
.if(!includeSoftDeleted, (qb) =>
qb.where(notSoftDeletedClause(ref('actor'))),
)
.where('actor.handle', 'is not', null)
.where(similar(term, ref('displayName'))) // Coarse filter engaging trigram index
.where(distanceProfile, '<', getMatchThreshold(term)) // Refines results from trigram index
.select(['profile.creator as did', distanceProfile.as('distance')])
}
// Combine profile and account result sets
const combineAccountsAndProfilesQb = (
db: Database,
accountsQb: AnyQb,
profilesQb: AnyQb,
) => {
// Combine user account and profile results, taking best matches from each
const emptyQb = db.db
.selectFrom('actor')
.where(sql`1 = 0`)
.select([sql.literal('').as('did'), sql<number>`0`.as('distance')])
const resultsQb = db.db
.selectFrom(
emptyQb
.unionAll(sql`${accountsQb}`) // The sql`` is adding parens
.unionAll(sql`${profilesQb}`)
.as('accounts_and_profiles'),
)
.selectAll()
.distinctOn('did') // Per did, take whichever of account and profile distance is best
.orderBy('did')
.orderBy('distance')
return db.db
.selectFrom(resultsQb.as('results'))
.innerJoin('actor', 'actor.did', 'results.did')
}
// Remove leading @ in case a handle is input that way
export const cleanTerm = (term: string) => term.trim().replace(/^@/g, '')
// Uses pg_trgm strict word similarity to check similarity between a search term and a stored value
const distance = (term: string, ref: DbRef) =>
sql<number>`(${term} <<<-> ${ref})`
// Can utilize trigram index to match on strict word similarity
const similar = (term: string, ref: DbRef) => sql<boolean>`(${term} <<% ${ref})`
const getMatchThreshold = (term: string) => {
// Performing matching by word using "strict word similarity" operator.
// The more characters the user gives us, the more we can ratchet down
// the distance threshold for matching.
return term.length < 3 ? 0.9 : 0.8
}
type Result = { distance: number; did: string }
type LabeledResult = { primary: number; secondary: string }
export class SearchKeyset extends GenericKeyset<Result, LabeledResult> {
labelResult(result: Result) {
return {
primary: result.distance,
secondary: result.did,
}
}
labeledResultToCursor(labeled: LabeledResult) {
return {
primary: labeled.primary.toString().replace('0.', '.'),
secondary: labeled.secondary,
}
}
cursorToLabeledResult(cursor: { primary: string; secondary: string }) {
const distance = parseFloat(cursor.primary)
if (isNaN(distance)) {
throw new InvalidRequestError('Malformed cursor')
}
return {
primary: distance,
secondary: cursor.secondary,
}
}
}