Repository navigation
Expand file tree
/
Copy pathvector-query.js
More file actions
57 lines (49 loc) · 1.72 KB
/
Copy pathvector-query.js
File metadata and controls
57 lines (49 loc) · 1.72 KB
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
import { MongoClient } from 'mongodb';
import { getEmbedding } from './get-embeddings.js';
// MongoDB connection URI and options
// connect to your Atlas deployment
const ATLAS_CONNECTION_STRING = "mongodb+srv://cklkslee:0400@lab2cluster.elg1k.mongodb.net/?retryWrites=true&w=majority&appName=Lab2Cluster"
// Connect to your Atlas cluster
const client = new MongoClient(ATLAS_CONNECTION_STRING);
async function run() {
try {
// Connect to the MongoDB client
await client.connect();
// Specify the database and collection
const db = client.db("sample_mflix");
const collection = db.collection("movies");
// Generate embedding for the search query
const queryEmbedding = await getEmbedding("alien transformers optimus prime");
// Define the sample vector search pipeline
const pipeline = [
{
$vectorSearch: {
index: "vector_index",
queryVector: queryEmbedding,
path: "embedding",
exact: true,
limit: 5
}
},
{
$project: {
_id: 0,
title: 1,
plot: 1,
score: {
$meta: "vectorSearchScore"
}
}
}
];
// run pipeline
const result = collection.aggregate(pipeline);
// print results
for await (const doc of result) {
console.dir(JSON.stringify(doc));
}
} finally {
await client.close();
}
}
run().catch(console.dir);