diff --git a/source/includes/indexes/indexes.py b/source/includes/indexes/indexes.py index 11731636..7b547017 100644 --- a/source/includes/indexes/indexes.py +++ b/source/includes/indexes/indexes.py @@ -88,26 +88,56 @@ collection.create_search_index(index) # end-create-search-index +# start-create-vector-search-index + +from pymongo.operations import SearchIndexModel + +search_index_model = SearchIndexModel( + definition={ + "fields": [ + { + "type": "vector", + "numDimensions": , + "path": "", + "similarity": "" + } + ] + }, + name="my_vector_index", + type="vectorSearch", +) -indexes = [index_one, index_two] +indexes = [search_idx, vector_idx] collection.create_search_indexes(models=indexes) # end-create-search-indexes @@ -120,18 +150,30 @@ # end-list-search-indexes # start-update-search-indexes -new_index = { - "definition": { - "mappings": { - "dynamic": True - } - }, - "name": "my_new_index", +new_index_definition = { + "mappings": { + "dynamic": False + } } collection.update_search_index("my_index", new_index) # end-update-search-indexes +# start-update-vector-search-indexes +new_index_definition = { + "fields": [ + { + "type": "vector", + "numDimensions": 1536, + "path": "", + "similarity": "euclidean" + }, + ] +} + +collection.update_search_index("my_vector_index", new_index_definition) +# end-update-vector-search-indexes + # start-delete-search-indexes collection.drop_index("my_index") # end-delete-search-indexes \ No newline at end of file diff --git a/source/indexes/atlas-search-index.txt b/source/indexes/atlas-search-index.txt index 147aef68..d0ad5091 100644 --- a/source/indexes/atlas-search-index.txt +++ b/source/indexes/atlas-search-index.txt @@ -1,8 +1,8 @@ .. _pymongo-atlas-search-index: -==================== -Atlas Search Indexes -==================== +====================================== +Atlas Search and Vector Search Indexes +====================================== .. contents:: On this page :local: @@ -20,16 +20,23 @@ Atlas Search Indexes Overview -------- -The Atlas Search feature enables you to perform full-text searches on -collections hosted on MongoDB Atlas. The indexes specify the behavior of +You can manage your :atlas:`Atlas Search ` and +:atlas:`Atlas Vector Search ` +indexes by using {+driver-short+}. The indexes specify the behavior of the search and which fields to index. -To learn more about MongoDB Atlas Search, see the -:atlas:`Atlas Search Indexes ` -documentation. +Atlas Search enables you to perform full-text searches on +collections hosted on MongoDB Atlas. Atlas Search indexes specify the behavior of +the search and which fields to index. + +Atlas Vector Search enables you to perform semantic searches on vector +embeddings stored in MongoDB Atlas. Vector Search indexes define the +indexes for the vector embeddings that you want to query and the boolean, +date, objectId, numeric, string, or UUID values that you want to use to +pre-filter your data. You can call the following methods on a collection to manage your Atlas Search -indexes: +and Vector Search indexes: - ``create_search_index()`` - ``create_search_indexes()`` @@ -55,16 +62,31 @@ Create a Search Index You can use the `create_search_index() <{+api-root+}pymongo/collection.html#pymongo.collection.Collection.create_search_index>`__ and the `create_search_indexes() <{+api-root+}pymongo/collection.html#pymongo.collection.Collection.create_search_indexes>`__ -methods to create Atlas Search indexes. +methods to create Atlas Search indexes or Atlas Vector Search indexes. -The following code example shows how to create a single index: +The following code example shows how to create a single Atlas Search index: .. literalinclude:: /includes/indexes/indexes.py :language: python :start-after: start-create-search-index :end-before: end-create-search-index -The following code example shows how to create multiple indexes: +The following code example shows how to create a single Atlas Vector Search index +by using the `SearchIndexModel <{+api-root+}pymongo/operations.html#pymongo.operations.SearchIndexModel>`__ +object: + +.. literalinclude:: /includes/indexes/indexes.py + :language: python + :start-after: start-create-vector-search-index + :end-before: end-create-vector-search-index + +You can use the `create_search_indexes() <{+api-root+}pymongo/collection.html#pymongo.collection.Collection.create_search_indexes>`__ +method to create multiple indexes. These indexes can be Atlas Search or +Vector Search indexes. The ``create_search_indexes()`` method takes a list of +``SearchIndexModel`` objects that correspond to each index you want to create. + +The following code example shows how to create an Atlas Search index and an Atlas +Vector Search index: .. literalinclude:: /includes/indexes/indexes.py :language: python @@ -78,7 +100,8 @@ List Search Indexes You can use the `list_search_indexes() <{+api-root+}pymongo/collection.html#pymongo.collection.Collection.list_search_indexes>`__ -method to return the Atlas Search indexes of a collection. +method to get information about the Atlas Search and Vector Search indexes +of a collection. The following code example shows how to print a list of the search indexes of a collection: @@ -96,9 +119,9 @@ Update a Search Index You can use the `update_search_index() <{+api-root+}pymongo/collection.html#pymongo.collection.Collection.update_search_index>`__ -method to update an Atlas Search index. +method to update an Atlas Search or Vector Search index. -The following code shows how to update a search index: +The following code example shows how to update an Atlas Search index: .. literalinclude:: /includes/indexes/indexes.py :language: python @@ -106,6 +129,14 @@ The following code shows how to update a search index: :start-after: start-update-search-indexes :end-before: end-update-search-indexes +The following code example shows how to update an Atlas Vector Search index: + +.. literalinclude:: /includes/indexes/indexes.py + :language: python + :dedent: + :start-after: start-update-vector-search-indexes + :end-before: end-update-vector-search-indexes + .. _pymongo-atlas-search-index-drop: Delete a Search Index @@ -113,7 +144,7 @@ Delete a Search Index You can use the `drop_search_index() <{+api-root+}pymongo/collection.html#pymongo.collection.Collection.drop_search_index>`__ -method to remove an Atlas Search index. +method to remove an Atlas Search or Vector Search index. The following code shows how to delete a search index from a collection: