-
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 18 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 | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -243,6 +243,98 @@ | |||||||||||||
|
|
||||||||||||||
| Name of Index Created: cast_-1 | ||||||||||||||
|
|
||||||||||||||
| .. _golang-atlas-search-indexes: | ||||||||||||||
|
|
||||||||||||||
| Atlas Search and Atlas Vector Search Indexes | ||||||||||||||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||
|
|
||||||||||||||
| You can programmatically manage your Atlas Search and Atlas Vector | ||||||||||||||
| Search indexes by using the {+driver-short+}. | ||||||||||||||
|
|
||||||||||||||
| The Atlas Search feature enables you to perform full-text searches on | ||||||||||||||
| collections hosted on MongoDB Atlas. To learn more about Atlas | ||||||||||||||
| Search, see the :atlas:`Atlas Search | ||||||||||||||
| </atlas-search/atlas-search-overview/>` documentation. | ||||||||||||||
|
|
||||||||||||||
| Atlas Vector Search enables you to perform semantic searches on vector | ||||||||||||||
| embeddings stored in Atlas. To learn more about Atlas | ||||||||||||||
| Vector Search, see the :atlas:`Atlas Vector Search | ||||||||||||||
| </atlas-vector-search/vector-search-overview/>` documentation. | ||||||||||||||
|
|
||||||||||||||
| .. Add when Go AVS guide is ready: | ||||||||||||||
| .. To learn more about Atlas Vector Search, see the :ref:`golang-atlas-vector-search` guide. | ||||||||||||||
jtazin marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
|
|
||||||||||||||
| The following sections contain code examples that demonstrate how to manage Atlas | ||||||||||||||
| Search and Atlas Vector Search indexes. | ||||||||||||||
|
|
||||||||||||||
| Create a Search Index | ||||||||||||||
| ````````````````````` | ||||||||||||||
|
|
||||||||||||||
| You can create an Atlas Search or an Atlas Vector Search index by providing | ||||||||||||||
| an index definition to the ``SearchIndexView.CreateOne()`` method. | ||||||||||||||
|
|
||||||||||||||
| The following example creates an Atlas Search index on the ``plot`` field of the | ||||||||||||||
| ``sample_mflix.movies`` collection: | ||||||||||||||
|
|
||||||||||||||
| .. literalinclude:: /includes/fundamentals/code-snippets/indexes/atlasVectorSearch.go | ||||||||||||||
| :language: go | ||||||||||||||
| :start-after: start-create-atlas-search | ||||||||||||||
| :end-before: end-create-atlas-search | ||||||||||||||
| :dedent: | ||||||||||||||
|
|
||||||||||||||
| The following example creates an Atlas Vector Search index on the ``plot_embedding`` | ||||||||||||||
| field in the ``sample_mflix.embedded_movies`` collection: | ||||||||||||||
|
|
||||||||||||||
| .. literalinclude:: /includes/fundamentals/code-snippets/indexes/atlasVectorSearch.go | ||||||||||||||
| :language: go | ||||||||||||||
| :start-after: start-create-vector-search | ||||||||||||||
| :end-before: end-create-vector-search | ||||||||||||||
| :dedent: | ||||||||||||||
|
|
||||||||||||||
| List a Search Index | ||||||||||||||
| ``````````````````` | ||||||||||||||
|
|
||||||||||||||
| You can use the ``SearchIndexView.List()`` method to list an Atlas Search or Atlas | ||||||||||||||
| Vector Search index by specifying the name of the index. | ||||||||||||||
|
|
||||||||||||||
| The following example lists the details of the specified search index: | ||||||||||||||
lindseymoore marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||
|
|
||||||||||||||
| .. literalinclude:: /includes/fundamentals/code-snippets/indexes/atlasVectorSearch.go | ||||||||||||||
| :language: go | ||||||||||||||
| :start-after: start-list-index | ||||||||||||||
| :end-before: end-list-index | ||||||||||||||
| :dedent: | ||||||||||||||
|
|
||||||||||||||
| Update a Search Index | ||||||||||||||
| ````````````````````` | ||||||||||||||
|
|
||||||||||||||
| You can use the ``SearchIndexView.UpdateOne()`` method to update an Atlas Search | ||||||||||||||
| or Atlas Vector Search index by specifying the name of the index and the new | ||||||||||||||
| index definition. | ||||||||||||||
|
|
||||||||||||||
| The following example updates an Atlas Vector Search index by providing the name of the index and | ||||||||||||||
jtazin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||
| a new index definition: | ||||||||||||||
|
|
||||||||||||||
| .. literalinclude:: /includes/fundamentals/code-snippets/indexes/atlasVectorSearch.go | ||||||||||||||
| :language: go | ||||||||||||||
| :start-after: start-update-index | ||||||||||||||
| :end-before: end-update-index | ||||||||||||||
| :dedent: | ||||||||||||||
|
|
||||||||||||||
| Delete a Search Index | ||||||||||||||
| ````````````````````` | ||||||||||||||
|
|
||||||||||||||
| You can use the ``SearchIndexView.DropOne()`` method to delete an Atlas Search or | ||||||||||||||
| Atlas Vector Search index by specifying the name of the index. | ||||||||||||||
|
|
||||||||||||||
| The following example deletes a search index with the specified name: | ||||||||||||||
|
||||||||||||||
| The following example deletes a search index with the specified name: | |
| The following example deletes an Atlas Search index with the specified name: |
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.
"a search index" would refer to both Atlas Search and Atlas Vector Search. If we'd like to say Atlas Search, then we should mention Atlas Vector Search as well, since the example applies to both.
| The following example deletes a search index with the specified name: | |
| The following example deletes an Atlas Search index or an Atlas Vector Search index with the specified name: |
or
| The following example deletes a search index with the specified name: | |
| The following example deletes an Atlas Search or Atlas Vector Search index with the specified name: |
jtazin marked this conversation as resolved.
Show resolved
Hide resolved
Check failure on line 579 in source/fundamentals/indexes.txt
GitHub Actions / TDBX Vale rules
[vale] reported by reviewdog 🐶
[MongoDB.ConciseTerms] 'and' is preferred over 'as well as'.
Raw Output:
{"message": "[MongoDB.ConciseTerms] 'and' is preferred over 'as well as'.", "location": {"path": "source/fundamentals/indexes.txt", "range": {"start": {"line": 579, "column": 8}}}, "severity": "ERROR"}
lindseymoore marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
jtazin marked this conversation as resolved.
Show resolved
Hide resolved
| 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 | ||
|
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.