Skip to content

DOCSP-44135: Mention vector search in pymongo search indexes page #116

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 5 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 62 additions & 20 deletions source/includes/indexes/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": <number of dimensions>,
"path": "<field to index>",
"similarity": "<select from euclidean, cosine, dotProduct>"
}
]
},
name="<index name>",
type="vectorSearch",
)

collection.create_search_index(model=search_index_model)

# end-create-vector-search-index

# start-create-search-indexes
index_one = {
"definition": {
"mappings": {
"dynamic": True
}
},
"name": "my_index",
}

index_two = {
"definition": {
search_idx = SearchIndexModel(
definition ={
"mappings": {
"dynamic": True
}
},
"name": "my_other_index",
}
name="my_index",
)

vector_idx = SearchIndexModel(
definition={
"fields": [
{
"type": "vector",
"numDimensions": <number of dimensions>,
"path": "<field to index>",
"similarity": "<select from euclidean, cosine, dotProduct>"
}
]
},
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
Expand All @@ -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": "<field to index>",
"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
63 changes: 47 additions & 16 deletions source/indexes/atlas-search-index.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
.. _pymongo-atlas-search-index:

====================
Atlas Search Indexes
====================
======================================
Atlas Search and Vector Search Indexes
======================================

.. contents:: On this page
:local:
Expand All @@ -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 </atlas-search>` and
:atlas:`Atlas Vector Search </atlas-vector-search/vector-search-overview/>`
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 </atlas-search/atlas-search-overview/>`
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()``
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -96,24 +119,32 @@ 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
:dedent:
: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
---------------------

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:

Expand Down
Loading