Skip to content

Commit 2f56aef

Browse files
authored
DOCSP-48594 AVS Indexes Missing Backport (mongodb#478)
1 parent e77be7b commit 2f56aef

File tree

1 file changed

+167
-0
lines changed

1 file changed

+167
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"log"
8+
"os"
9+
10+
"go.mongodb.org/mongo-driver/v2/bson"
11+
"go.mongodb.org/mongo-driver/v2/mongo"
12+
"go.mongodb.org/mongo-driver/v2/mongo/options"
13+
)
14+
15+
func main() {
16+
ctx := context.Background()
17+
18+
// Retrieves your Atlas connection string
19+
uri := os.Getenv("MONGODB_ATLAS_URI")
20+
if uri == "" {
21+
log.Fatal("MONGODB_ATLAS_URI environment variable is not set")
22+
}
23+
24+
// Connect to your Atlas cluster
25+
clientOptions := options.Client().ApplyURI(uri)
26+
client, err := mongo.Connect(ctx, clientOptions)
27+
if err != nil {
28+
log.Fatalf("Failed to connect to the server: %v", err)
29+
}
30+
defer func() {
31+
if err := client.Disconnect(ctx); err != nil {
32+
log.Fatalf("Failed to disconnect: %v", err)
33+
}
34+
}()
35+
36+
// Set the namespace
37+
coll := client.Database("sample_mflix").Collection("embedded_movies")
38+
39+
// start-create-vector-search
40+
// Defines the structs used for the index definition
41+
type vectorDefinitionField struct {
42+
Type string `bson:"type"`
43+
Path string `bson:"path"`
44+
NumDimensions int `bson:"numDimensions"`
45+
Similarity string `bson:"similarity"`
46+
Quantization string `bson:"quantization"`
47+
}
48+
49+
type vectorDefinition struct {
50+
Fields []vectorDefinitionField `bson:"fields"`
51+
}
52+
53+
// Sets the index name and type to "vectorSearch"
54+
const indexName = "vector_search_index"
55+
opts := options.SearchIndexes().SetName(indexName).SetType("vectorSearch")
56+
57+
// Defines the index definition
58+
vectorSearchIndexModel := mongo.SearchIndexModel{
59+
Definition: vectorDefinition{
60+
Fields: []vectorDefinitionField{{
61+
Type: "vector",
62+
Path: "plot_embedding",
63+
NumDimensions: 1536,
64+
Similarity: "dotProduct",
65+
Quantization: "scalar"}},
66+
},
67+
Options: opts,
68+
}
69+
70+
// Creates the index
71+
searchIndexName, err := coll.SearchIndexes().CreateOne(ctx, vectorSearchIndexModel)
72+
if err != nil {
73+
log.Fatalf("Failed to create the Atlas Vector Search index: %v", err)
74+
}
75+
// end-create-vector-search
76+
77+
// Creates an Atlas Search index
78+
// start-create-atlas-search
79+
// Sets the index name and type to "search"
80+
const indexName = "search_index"
81+
opts := options.SearchIndexes().SetName(indexName).SetType("search")
82+
83+
// Defines the index definition
84+
searchIndexModel := mongo.SearchIndexModel{
85+
Definition: bson.D{
86+
{Key: "mappings", Value: bson.D{
87+
{Key: "dynamic", Value: false},
88+
{Key: "fields", Value: bson.D{
89+
{Key: "plot", Value: bson.D{
90+
{Key: "type", Value: "string"},
91+
}},
92+
}},
93+
}},
94+
},
95+
Options: opts,
96+
}
97+
98+
// Creates the index
99+
searchIndexName, err := coll.SearchIndexes().CreateOne(ctx, searchIndexModel)
100+
if err != nil {
101+
log.Fatalf("Failed to create the Atlas Search index: %v", err)
102+
}
103+
// end-create-atlas-search
104+
105+
// start-list-index
106+
// Specifies the index to retrieve
107+
const indexName = "myIndex"
108+
opts := options.SearchIndexes().SetName(indexName)
109+
110+
// Retrieves the details of the specified index
111+
cursor, err := coll.SearchIndexes().List(ctx, opts)
112+
113+
// Prints the index details to the console as JSON
114+
var results []bson.D
115+
if err := cursor.All(ctx, &results); err != nil {
116+
log.Fatalf("Failed to unmarshal results to bson: %v", err)
117+
}
118+
res, err := json.Marshal(results)
119+
if err != nil {
120+
log.Fatalf("Failed to marshal results to json: %v", err)
121+
}
122+
fmt.Println(res)
123+
// end-list-index
124+
125+
// start-update-index
126+
// Specifies the index name and the new index definition
127+
const indexName = "vector_search_index"
128+
129+
type vectorDefinitionField struct {
130+
Type string `bson:"type"`
131+
Path string `bson:"path"`
132+
NumDimensions int `bson:"numDimensions"`
133+
Similarity string `bson:"similarity"`
134+
}
135+
136+
type vectorDefinition struct {
137+
Fields []vectorDefinitionField `bson:"fields"`
138+
}
139+
140+
definition := vectorDefinition{
141+
Fields: []vectorDefinitionField{
142+
{
143+
Type: "vector",
144+
Path: "plot_embedding",
145+
NumDimensions: 1536,
146+
Similarity: "cosine",
147+
Quantization: "scalar",
148+
},
149+
},
150+
}
151+
152+
// Updates the specified index
153+
err := coll.SearchIndexes().UpdateOne(ctx, indexName, definition)
154+
if err != nil {
155+
log.Fatalf("Failed to update the index: %v", err)
156+
}
157+
// end-update-index
158+
159+
// start-delete-index
160+
// Deletes the specified index
161+
err := coll.SearchIndexes().DropOne(ctx, "myIndex")
162+
if err != nil {
163+
log.Fatalf("Failed to delete the index: %v", err)
164+
}
165+
// end-delete-index
166+
167+
}

0 commit comments

Comments
 (0)