Skip to content

Commit 0b4adeb

Browse files
authored
DOCSP-48164: Async examples for Indexes page (#235)
1 parent b1be525 commit 0b4adeb

File tree

2 files changed

+571
-92
lines changed

2 files changed

+571
-92
lines changed
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
# start-index-single
2+
await movies.create_index("title")
3+
# end-index-single
4+
5+
# start-index-single-collation
6+
from pymongo.collation import Collation
7+
8+
await movies.create_index("title", collation=Collation(locale='fr_CA'))
9+
# end-index-single-collation
10+
11+
# start-compound-index
12+
await movies.create_index([("type", pymongo.ASCENDING), ("genre", pymongo.ASCENDING)])
13+
# end-compound-index
14+
15+
# start-compound-index-collation
16+
from pymongo.collation import Collation
17+
18+
await movies.create_index([("type", pymongo.ASCENDING), ("genre", pymongo.ASCENDING)],
19+
collation=Collation(locale='fr_CA'))
20+
# end-compound-index-collation
21+
22+
# start-index-multikey
23+
result = await movies.create_index("cast")
24+
# end-index-multikey
25+
26+
# start-index-multikey-collation
27+
from pymongo.collation import Collation
28+
29+
result = await movies.create_index("cast", collation=Collation(locale='fr_CA'))
30+
# end-index-multikey-collation
31+
32+
# start-index-text-single
33+
await movies.create_index(
34+
[( "plot", "text" )]
35+
)
36+
# end-index-text-single
37+
38+
# start-index-text-single-collation
39+
from pymongo.collation import Collation
40+
41+
await movies.create_index(
42+
[( "plot", "text" )],
43+
collation=Collation(locale='fr_CA')
44+
)
45+
# end-index-text-single-collation
46+
47+
# start-index-text-multi
48+
from pymongo.collation import Collation
49+
50+
result = await myColl.create_index(
51+
[("title", "text"), ("genre", "text")],
52+
default_language="english",
53+
weights={ "title": 10, "genre": 3 },
54+
collation=Collation(locale='fr_CA')
55+
)
56+
# end-index-text-multi
57+
58+
# start-index-geo
59+
await theaters.create_index(
60+
[( "location.geo", "2dsphere" )]
61+
)
62+
# end-index-geo
63+
64+
# start-index-geo-collation
65+
from pymongo.collation import Collation
66+
67+
await theaters.create_index(
68+
[( "location.geo", "2dsphere" )],
69+
collation=Collation(locale='fr_CA'))
70+
# end-index-geo-collation
71+
72+
# start-index-wildcard
73+
await movies.create_index({ "location.$**": pymongo.ASCENDING })
74+
# end-index-wildcard
75+
76+
# start-index-wildcard-collation
77+
await movies.create_index({ "location.$**": pymongo.ASCENDING },
78+
collation=Collation(locale='fr_CA'))
79+
# end-index-wildcard-collation
80+
81+
# start-index-unique
82+
await theaters.create_index("theaterId", unique=True)
83+
# end-index-unique
84+
85+
# start-index-unique-collation
86+
await theaters.create_index("theaterId", unique=True, collation=Collation(locale='fr_CA'))
87+
# end-index-unique-collation
88+
89+
# start-index-clustered
90+
await sample_mflix.create_collection("movies", clusteredIndex={
91+
"key": { "_id": 1 },
92+
"unique": True
93+
})
94+
# end-index-clustered
95+
96+
# start-remove-index
97+
await movies.drop_index("_title_")
98+
# end-remove-index
99+
100+
# start-create-search-index
101+
index = {
102+
"definition": {
103+
"mappings": {
104+
"dynamic": True
105+
}
106+
},
107+
"name": "<index name>",
108+
}
109+
110+
await collection.create_search_index(index)
111+
# end-create-search-index
112+
113+
# start-create-vector-search-index
114+
from pymongo.operations import SearchIndexModel
115+
116+
search_index_model = SearchIndexModel(
117+
definition={
118+
"fields": [
119+
{
120+
"type": "vector",
121+
"numDimensions": <number of dimensions>,
122+
"path": "<field to index>",
123+
"similarity": "<select from euclidean, cosine, dotProduct>"
124+
}
125+
]
126+
},
127+
name="<index name>",
128+
type="vectorSearch",
129+
)
130+
131+
await collection.create_search_index(model=search_index_model)
132+
# end-create-vector-search-index
133+
134+
# start-create-search-indexes
135+
search_idx = SearchIndexModel(
136+
definition ={
137+
"mappings": {
138+
"dynamic": True
139+
}
140+
},
141+
name="my_index",
142+
)
143+
144+
vector_idx = SearchIndexModel(
145+
definition={
146+
"fields": [
147+
{
148+
"type": "vector",
149+
"numDimensions": <number of dimensions>,
150+
"path": "<field to index>",
151+
"similarity": "<select from euclidean, cosine, dotProduct>"
152+
}
153+
]
154+
},
155+
name="my_vector_index",
156+
type="vectorSearch",
157+
)
158+
159+
indexes = [search_idx, vector_idx]
160+
161+
await collection.create_search_indexes(models=indexes)
162+
# end-create-search-indexes
163+
164+
# start-list-search-indexes
165+
results = await (await collection.list_search_indexes()).to_list()
166+
167+
async for index in results:
168+
print(index)
169+
# end-list-search-indexes
170+
171+
# start-update-search-indexes
172+
new_index_definition = {
173+
"mappings": {
174+
"dynamic": False
175+
}
176+
}
177+
178+
await collection.update_search_index("my_index", new_index)
179+
# end-update-search-indexes
180+
181+
# start-update-vector-search-indexes
182+
new_index_definition = {
183+
"fields": [
184+
{
185+
"type": "vector",
186+
"numDimensions": 1536,
187+
"path": "<field to index>",
188+
"similarity": "euclidean"
189+
},
190+
]
191+
}
192+
193+
await collection.update_search_index("my_vector_index", new_index_definition)
194+
# end-update-vector-search-indexes
195+
196+
# start-delete-search-indexes
197+
await collection.drop_search_index("my_index")
198+
# end-delete-search-indexes

0 commit comments

Comments
 (0)