-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
atlas-api.ts
240 lines (213 loc) · 5.88 KB
/
atlas-api.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import express from 'express'
import {request} from 'urllib'
import cors from 'cors'
import {mongoClient, MONGODB_COLLECTION, MONGODB_DATABASE, User} from './util'
const ATLAS_API_BASE_URL = 'https://cloud.mongodb.com/api/atlas/v1.0'
const ATLAS_PROJECT_ID = process.env.MONGODB_ATLAS_PROJECT_ID
const ATLAS_CLUSTER_NAME = process.env.MONGODB_ATLAS_CLUSTER
const ATLAS_CLUSTER_API_URL = `${ATLAS_API_BASE_URL}/groups/${ATLAS_PROJECT_ID}/clusters/${ATLAS_CLUSTER_NAME}`
const ATLAS_SEARCH_INDEX_API_URL = `${ATLAS_CLUSTER_API_URL}/fts/indexes`
const ATLAS_API_PUBLIC_KEY = process.env.MONGODB_ATLAS_PUBLIC_KEY
const ATLAS_API_PRIVATE_KEY = process.env.MONGODB_ATLAS_PRIVATE_KEY
const DIGEST_AUTH = `${ATLAS_API_PUBLIC_KEY}:${ATLAS_API_PRIVATE_KEY}`
const USER_SEARCH_INDEX_NAME = 'user_search'
const USER_AUTOCOMPLETE_INDEX_NAME = 'user_autocomplete'
const app = express()
app.use(cors({credentials: true, origin: 'http://localhost:4000'}))
app.get('/search', async (req, res) => {
const searchQuery = req.query.query as string
const country = req.query.country as string
if (!searchQuery || searchQuery.length < 2) {
res.json([])
return
}
const db = mongoClient.db('tutorial')
const collection = db.collection<User>(MONGODB_COLLECTION)
const pipeline = []
if (country) {
pipeline.push({
$search: {
index: USER_SEARCH_INDEX_NAME,
compound: {
must: [
{
text: {
query: searchQuery,
path: ['fullName', 'email'],
fuzzy: {},
},
},
{
text: {
query: country,
path: 'country',
},
},
],
},
},
})
} else {
pipeline.push({
$search: {
index: USER_SEARCH_INDEX_NAME,
text: {
query: searchQuery,
path: ['fullName', 'email'],
fuzzy: {},
},
},
})
}
pipeline.push({
$project: {
_id: 0,
score: {$meta: 'searchScore'},
userId: 1,
fullName: 1,
email: 1,
avatar: 1,
registeredAt: 1,
country: 1,
},
})
const result = await collection.aggregate(pipeline).sort({score: -1}).limit(10)
const array = await result.toArray()
res.json(array)
})
app.get('/autocomplete', async (req, res) => {
const searchQuery = req.query.query as string
const country = req.query.country as string
const db = mongoClient.db('tutorial')
const collection = db.collection<User>(MONGODB_COLLECTION)
const pipeline = []
if (country) {
pipeline.push({
$search: {
index: USER_SEARCH_INDEX_NAME,
compound: {
must: [
{
autocomplete: {
query: searchQuery,
path: 'fullName',
fuzzy: {},
},
},
{
text: {
query: country,
path: 'country',
},
},
],
},
},
})
} else {
pipeline.push({
$search: {
index: USER_AUTOCOMPLETE_INDEX_NAME,
// https://www.mongodb.com/docs/atlas/atlas-search/autocomplete/#options
autocomplete: {
query: searchQuery,
path: 'fullName',
tokenOrder: 'sequential',
},
},
})
}
pipeline.push({
$project: {
_id: 0,
score: {$meta: 'searchScore'},
userId: 1,
fullName: 1,
email: 1,
avatar: 1,
registeredAt: 1,
country: 1,
},
})
const result = await collection.aggregate(pipeline).sort({score: -1}).limit(10)
const array = await result.toArray()
res.json(array)
})
async function findIndexByName(indexName: string) {
const allIndexesResponse = await request(
`${ATLAS_SEARCH_INDEX_API_URL}/${MONGODB_DATABASE}/${MONGODB_COLLECTION}`,
{
dataType: 'json',
contentType: 'application/json',
method: 'GET',
digestAuth: DIGEST_AUTH,
}
)
return (allIndexesResponse.data as any[]).find((i) => i.name === indexName)
}
async function upsertSearchIndex() {
const userSearchIndex = await findIndexByName(USER_SEARCH_INDEX_NAME)
if (!userSearchIndex) {
await request(ATLAS_SEARCH_INDEX_API_URL, {
data: {
name: USER_SEARCH_INDEX_NAME,
database: MONGODB_DATABASE,
collectionName: MONGODB_COLLECTION,
// https://www.mongodb.com/docs/atlas/atlas-search/index-definitions/#syntax
mappings: {
dynamic: true,
},
},
dataType: 'json',
contentType: 'application/json',
method: 'POST',
digestAuth: DIGEST_AUTH,
})
}
}
async function upsertAutocompleteIndex() {
const userAutocompleteIndex = await findIndexByName(USER_AUTOCOMPLETE_INDEX_NAME)
if (!userAutocompleteIndex) {
await request(ATLAS_SEARCH_INDEX_API_URL, {
data: {
name: USER_AUTOCOMPLETE_INDEX_NAME,
database: MONGODB_DATABASE,
collectionName: MONGODB_COLLECTION,
// https://www.mongodb.com/docs/atlas/atlas-search/autocomplete/#index-definition
mappings: {
dynamic: false,
fields: {
fullName: [
{
foldDiacritics: false,
maxGrams: 7,
minGrams: 3,
tokenization: 'edgeGram',
type: 'autocomplete',
},
],
},
},
},
dataType: 'json',
contentType: 'application/json',
method: 'POST',
digestAuth: DIGEST_AUTH,
})
}
}
async function main() {
try {
await mongoClient.connect()
await upsertSearchIndex()
await upsertAutocompleteIndex()
app.listen(3001, () => console.log('http://localhost:3001/search?query=gilbert'))
} catch (err) {
console.log(err)
}
process.on('SIGTERM', async () => {
await mongoClient.close()
process.exit(0)
})
}
main()