Skip to content

Commit fb0c5f0

Browse files
committed
full page
1 parent 8203326 commit fb0c5f0

File tree

2 files changed

+93
-27
lines changed

2 files changed

+93
-27
lines changed

source/includes/indexes/indexes.py

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -88,26 +88,56 @@
8888
collection.create_search_index(index)
8989
# end-create-search-index
9090

91+
# start-create-vector-search-index
92+
93+
from pymongo.operations import SearchIndexModel
94+
95+
search_index_model = SearchIndexModel(
96+
definition={
97+
"fields": [
98+
{
99+
"type": "vector",
100+
"numDimensions": <numberOfDimensions>,
101+
"path": "<fieldToIndex>",
102+
"similarity": "euclidean | cosine | dotProduct"
103+
}
104+
]
105+
},
106+
name="<nameOfIndex>",
107+
type="vectorSearch",
108+
)
109+
110+
collection.create_search_index(model=search_index_model)
111+
112+
# end-create-vector-search-index
113+
91114
# start-create-search-indexes
92-
index_one = {
93-
"definition": {
94-
"mappings": {
95-
"dynamic": True
96-
}
97-
},
98-
"name": "my_index",
99-
}
100115

101-
index_two = {
102-
"definition": {
116+
index_one = SearchIndexModel(
117+
definition ={
103118
"mappings": {
104119
"dynamic": True
105120
}
106121
},
107-
"name": "my_other_index",
108-
}
122+
name="my_index",
123+
)
124+
125+
index_two_vector = SearchIndexModel(
126+
definition={
127+
"fields": [
128+
{
129+
"type": "vector",
130+
"numDimensions": <numberOfDimensions>,
131+
"path": "<fieldToIndex>",
132+
"similarity": "euclidean | cosine | dotProduct"
133+
}
134+
]
135+
},
136+
name="my_vector_index",
137+
type="vectorSearch",
138+
)
109139

110-
indexes = [index_one, index_two]
140+
indexes = [index_one, index_two_vector]
111141

112142
collection.create_search_indexes(models=indexes)
113143
# end-create-search-indexes
@@ -120,18 +150,30 @@
120150
# end-list-search-indexes
121151

122152
# start-update-search-indexes
123-
new_index = {
124-
"definition": {
125-
"mappings": {
126-
"dynamic": True
127-
}
128-
},
129-
"name": "my_new_index",
153+
new_index_definition = {
154+
"mappings": {
155+
"dynamic": False
156+
}
130157
}
131158

132159
collection.update_search_index("my_index", new_index)
133160
# end-update-search-indexes
134161

162+
# start-update-vector-search-indexes
163+
new_index_definition = {
164+
"fields": [
165+
{
166+
"type": "vector",
167+
"numDimensions": 1536,
168+
"path": "<fieldToIndex>",
169+
"similarity": "euclidean"
170+
},
171+
]
172+
}
173+
174+
collection.update_search_index("my_vector_index", new_index_definition)
175+
# end-update-vector-search-indexes
176+
135177
# start-delete-search-indexes
136178
collection.drop_index("my_index")
137179
# end-delete-search-indexes

source/indexes/atlas-search-index.txt

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,31 @@ Create a Search Index
6262
You can use the `create_search_index() <{+api-root+}pymongo/collection.html#pymongo.collection.Collection.create_search_index>`__
6363
and the
6464
`create_search_indexes() <{+api-root+}pymongo/collection.html#pymongo.collection.Collection.create_search_indexes>`__
65-
methods to create Atlas Search indexes.
65+
methods to create Atlas Search indexes or Atlas Vector Search indexes.
6666

67-
The following code example shows how to create a single index:
67+
The following code example shows how to create a single Atlas Search index:
6868

6969
.. literalinclude:: /includes/indexes/indexes.py
7070
:language: python
7171
:start-after: start-create-search-index
7272
:end-before: end-create-search-index
7373

74-
The following code example shows how to create multiple indexes:
74+
The following code example shows how to create a single Atlas Vector Search index
75+
using the `SearchIndexModel <{+api-root+}pymongo/operations.html#pymongo.operations.SearchIndexModel>`__
76+
object:
77+
78+
.. literalinclude:: /includes/indexes/indexes.py
79+
:language: python
80+
:start-after: start-create-vector-search-index
81+
:end-before: end-create-vector-search-index
82+
83+
You can use `create_search_indexes() <{+api-root+}pymongo/collection.html#pymongo.collection.Collection.create_search_indexes>`__
84+
to create multiple indexes. These indexes can be Atlas Search indexes or Atlas
85+
Vector Search indexes. ``create_search_indexes()`` requires the ``SearchIndexModel``
86+
object.
87+
88+
The following code example shows how to create an Atlas Search index and Atlas
89+
Vector Search index:
7590

7691
.. literalinclude:: /includes/indexes/indexes.py
7792
:language: python
@@ -85,7 +100,8 @@ List Search Indexes
85100

86101
You can use the
87102
`list_search_indexes() <{+api-root+}pymongo/collection.html#pymongo.collection.Collection.list_search_indexes>`__
88-
method to return the Atlas Search indexes of a collection.
103+
method to return the Atlas Search indexes and Atlas Vector Search indexes
104+
of a collection.
89105

90106
The following code example shows how to print a list of the search indexes of
91107
a collection:
@@ -103,24 +119,32 @@ Update a Search Index
103119

104120
You can use the
105121
`update_search_index() <{+api-root+}pymongo/collection.html#pymongo.collection.Collection.update_search_index>`__
106-
method to update an Atlas Search index.
122+
method to update an Atlas Search index or Atlas Vector Search index.
107123

108-
The following code shows how to update a search index:
124+
The following code example shows how to update an Atlas Search index:
109125

110126
.. literalinclude:: /includes/indexes/indexes.py
111127
:language: python
112128
:dedent:
113129
:start-after: start-update-search-indexes
114130
:end-before: end-update-search-indexes
115131

132+
The following code example shows how to update an Atlas Vector Search index:
133+
134+
.. literalinclude:: /includes/indexes/indexes.py
135+
:language: python
136+
:dedent:
137+
:start-after: start-update-vector-search-indexes
138+
:end-before: end-update-vector-search-indexes
139+
116140
.. _pymongo-atlas-search-index-drop:
117141

118142
Delete a Search Index
119143
---------------------
120144

121145
You can use the
122146
`drop_search_index() <{+api-root+}pymongo/collection.html#pymongo.collection.Collection.drop_search_index>`__
123-
method to remove an Atlas Search index.
147+
method to remove an Atlas Search index or Atlas Vector Search index.
124148

125149
The following code shows how to delete a search index from a collection:
126150

0 commit comments

Comments
 (0)