-
Notifications
You must be signed in to change notification settings - Fork 34
DOCSP-47797 Add VS and AVS to indexes page #462
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
5f1224a
DOCSP-47797 Add VS and AVS to indexes page
lindseymoore 5e68d0e
add atlas searche ex
lindseymoore 7c78d7c
edits
lindseymoore da324ed
comment edit
lindseymoore 1d39cca
code edits
lindseymoore af336e3
update with error handling
lindseymoore e16f984
link to general AVS cloud docs
lindseymoore e8c5be1
more code comment edits
lindseymoore 30ca633
copy edits
lindseymoore a39d6d0
copy review feedback
lindseymoore 4d67413
last of copy review comments
lindseymoore c11990e
push
lindseymoore 250a161
PV tech review
lindseymoore ccb4dba
JT comments
lindseymoore 9823429
include method names
lindseymoore 0718c66
edit
lindseymoore 33b654b
JT comments
lindseymoore 3b115a8
code updates
lindseymoore bc00c5f
edits
lindseymoore File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletions
167
source/includes/fundamentals/code-snippets/indexes/atlasVectorSearch.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"go.mongodb.org/mongo-driver/v2/bson" | ||
"go.mongodb.org/mongo-driver/v2/mongo" | ||
"go.mongodb.org/mongo-driver/v2/mongo/options" | ||
) | ||
|
||
func main() { | ||
ctx := context.Background() | ||
|
||
// Retrieves your Atlas connection string | ||
uri := os.Getenv("MONGODB_ATLAS_URI") | ||
lindseymoore marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if uri == "" { | ||
log.Fatal("MONGODB_ATLAS_URI environment variable is not set") | ||
} | ||
|
||
// Connect to your Atlas cluster | ||
clientOptions := options.Client().ApplyURI(uri) | ||
client, err := mongo.Connect(ctx, clientOptions) | ||
if err != nil { | ||
log.Fatalf("Failed to connect to the server: %v", err) | ||
} | ||
defer func() { | ||
if err := client.Disconnect(ctx); err != nil { | ||
log.Fatalf("Failed to disconnect: %v", err) | ||
} | ||
}() | ||
|
||
// Set the namespace | ||
coll := client.Database("sample_mflix").Collection("embedded_movies") | ||
|
||
// start-create-vector-search | ||
// Defines the structs used for the index definition | ||
type vectorDefinitionField struct { | ||
Type string `bson:"type"` | ||
Path string `bson:"path"` | ||
NumDimensions int `bson:"numDimensions"` | ||
Similarity string `bson:"similarity"` | ||
Quantization string `bson:"quantization"` | ||
} | ||
|
||
type vectorDefinition struct { | ||
Fields []vectorDefinitionField `bson:"fields"` | ||
} | ||
|
||
// Sets the index name and type to "vectorSearch" | ||
const indexName = "vector_search_index" | ||
opts := options.SearchIndexes().SetName(indexName).SetType("vectorSearch") | ||
|
||
// Defines the index definition | ||
vectorSearchIndexModel := mongo.SearchIndexModel{ | ||
Definition: vectorDefinition{ | ||
Fields: []vectorDefinitionField{{ | ||
Type: "vector", | ||
Path: "plot_embedding", | ||
NumDimensions: 1536, | ||
Similarity: "dotProduct", | ||
Quantization: "scalar"}}, | ||
}, | ||
Options: opts, | ||
} | ||
|
||
// Creates the index | ||
searchIndexName, err := coll.SearchIndexes().CreateOne(ctx, vectorSearchIndexModel) | ||
if err != nil { | ||
log.Fatalf("Failed to create the Atlas Vector Search index: %v", err) | ||
} | ||
// end-create-vector-search | ||
|
||
// Creates an Atlas Search index | ||
// start-create-atlas-search | ||
// Sets the index name and type to "search" | ||
const indexName = "search_index" | ||
opts := options.SearchIndexes().SetName(indexName).SetType("search") | ||
|
||
// Defines the index definition | ||
searchIndexModel := mongo.SearchIndexModel{ | ||
Definition: bson.D{ | ||
{Key: "mappings", Value: bson.D{ | ||
{Key: "dynamic", Value: false}, | ||
{Key: "fields", Value: bson.D{ | ||
{Key: "plot", Value: bson.D{ | ||
{Key: "type", Value: "string"}, | ||
}}, | ||
}}, | ||
}}, | ||
}, | ||
Options: opts, | ||
} | ||
|
||
// Creates the index | ||
searchIndexName, err := coll.SearchIndexes().CreateOne(ctx, searchIndexModel) | ||
if err != nil { | ||
log.Fatalf("Failed to create the Atlas Search index: %v", err) | ||
} | ||
// end-create-atlas-search | ||
|
||
// start-list-index | ||
// Specifies the index to retrieve | ||
const indexName = "myIndex" | ||
opts := options.SearchIndexes().SetName(indexName) | ||
|
||
// Retrieves the details of the specified index | ||
cursor, err := coll.SearchIndexes().List(ctx, opts) | ||
|
||
// Prints the index details to the console as JSON | ||
var results []bson.D | ||
if err := cursor.All(ctx, &results); err != nil { | ||
log.Fatalf("Failed to unmarshal results to bson: %v", err) | ||
} | ||
res, err := json.Marshal(results) | ||
if err != nil { | ||
log.Fatalf("Failed to marshal results to json: %v", err) | ||
} | ||
fmt.Println(res) | ||
lindseymoore marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// end-list-index | ||
|
||
// start-update-index | ||
// Specifies the index name and the new index definition | ||
const indexName = "vector_search_index" | ||
|
||
type vectorDefinitionField struct { | ||
Type string `bson:"type"` | ||
Path string `bson:"path"` | ||
NumDimensions int `bson:"numDimensions"` | ||
Similarity string `bson:"similarity"` | ||
} | ||
lindseymoore marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
type vectorDefinition struct { | ||
Fields []vectorDefinitionField `bson:"fields"` | ||
} | ||
lindseymoore marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
definition := vectorDefinition{ | ||
Fields: []vectorDefinitionField{ | ||
{ | ||
Type: "vector", | ||
Path: "plot_embedding", | ||
NumDimensions: 1536, | ||
Similarity: "cosine", | ||
Quantization: "scalar", | ||
}, | ||
}, | ||
} | ||
|
||
// Updates the specified index | ||
err := coll.SearchIndexes().UpdateOne(ctx, indexName, definition) | ||
if err != nil { | ||
log.Fatalf("Failed to update the index: %v", err) | ||
} | ||
// end-update-index | ||
|
||
// start-delete-index | ||
// Deletes the specified index | ||
err := coll.SearchIndexes().DropOne(ctx, "myIndex") | ||
if err != nil { | ||
log.Fatalf("Failed to delete the index: %v", err) | ||
} | ||
// end-delete-index | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.