-
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
Changes from 13 commits
5f1224a
5e68d0e
7c78d7c
da324ed
1d39cca
af336e3
e16f984
e8c5be1
30ca633
a39d6d0
4d67413
c11990e
250a161
ccb4dba
9823429
0718c66
33b654b
3b115a8
bc00c5f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,161 @@ | ||||||
| package main | ||||||
|
|
||||||
| import ( | ||||||
| "context" | ||||||
| "fmt" | ||||||
| "log" | ||||||
| "time" | ||||||
|
|
||||||
| "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) | ||||||
| } | ||||||
| }() | ||||||
lindseymoore marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
|
|
||||||
| // 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_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 vector search index: %v", err) | ||||||
lindseymoore marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| } | ||||||
| // end-create-vector-search | ||||||
|
|
||||||
| // Creates an Atlas Search index | ||||||
| // start-create-atlas-search | ||||||
| // Sets the index name and type to "search" | ||||||
| const indexName = "atlas_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) | ||||||
lindseymoore marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| } | ||||||
| // end-create-atlas-search | ||||||
|
|
||||||
| // start-list-index | ||||||
| // Specify the index to retrieve | ||||||
lindseymoore marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| const indexName = "<indexName>" | ||||||
| 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 | ||||||
| // Specify the index name and the new index definition | ||||||
|
||||||
| // Specify the index name and the new index definition | |
| // Specifies the index name and the new index definition |
Nit: Keeps it in the same tense as the rest of the comments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shuangela According to this guidance on code comments, comments that indicate that a user needs to take an action should be imperative. For this section, the user needs to replace the index name and definition, so the imperative tense is necessary here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was going to make the same comment as @shuangela and then saw your response @lindseymoore. We can keep the imperative if that's the guidance, but is there a way we can make it even clearer to the user that they should modify the code here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since it's an example, I propose we just use the same index name as in the previous examples. As part of this, I would change all of the examples to use the same index name consistently:
const indexName = "atlas_vector_search_index"
const indexName = "atlas_search_index"
or:
const indexName = "vector_search_index"
const indexName = "search_index"
And then we would end up with something like this:
// Specifies the index name and the new index definition
const indexName = "search_index"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On second thought, do some of the examples apply to both AS and AVS?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the delete, list, and update methods are used the same for both AS and AVS. I updated the examples to remove the placeholders and include more general names, like myIndex. The only exception is in the update section where I made the name vector_index, since the definition shown is for a vector index.
Also changed the comments back to "specifies".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, that makes sense. Thanks for making the change.
Any reason you decided to not make the names atlas_search_index and vector_index more consistent though? I would either use "atlas" for both or neither. Also, the corresponding terms are "search" vs "vector search" rather than "search" vs "vector". Let me know if there's something I'm missing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for clarifying! Updated names to vector_search_index and search_index
Uh oh!
There was an error while loading. Please reload this page.