Skip to content

Commit d5e168e

Browse files
authored
Merge pull request #102426 from ThomasWeiss/thweiss-cosmosdb-indexing-python-v4
Added example for Python V4
2 parents 6411dda + bc2fa15 commit d5e168e

File tree

1 file changed

+68
-2
lines changed

1 file changed

+68
-2
lines changed

articles/cosmos-db/how-to-manage-indexing-policy.md

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -602,9 +602,9 @@ const containerResponse = await client.database('database').container('container
602602
const indexTransformationProgress = replaceResponse.headers['x-ms-documentdb-collection-index-transformation-progress'];
603603
```
604604

605-
## Use the Python SDK
605+
## Use the Python SDK V3
606606

607-
When using the [Python SDK](https://pypi.org/project/azure-cosmos/) (see [this Quickstart](create-sql-api-python.md) regarding its usage), the container configuration is managed as a dictionary. From this dictionary, it is possible to access the indexing policy and all its attributes.
607+
When using the [Python SDK V3](https://pypi.org/project/azure-cosmos/) (see [this Quickstart](create-sql-api-python.md) regarding its usage), the container configuration is managed as a dictionary. From this dictionary, it is possible to access the indexing policy and all its attributes.
608608

609609
Retrieve the container's details
610610

@@ -666,6 +666,72 @@ Update the container with changes
666666
response = client.ReplaceContainer(containerPath, container)
667667
```
668668

669+
## Use the Python SDK V4
670+
671+
When using the [Python SDK V4](https://pypi.org/project/azure-cosmos/), the container configuration is managed as a dictionary. From this dictionary, it is possible to access the indexing policy and all its attributes.
672+
673+
Retrieve the container's details
674+
675+
```python
676+
database_client = cosmos_client.get_database_client('database')
677+
container_client = database_client.get_container_client('container')
678+
container = container_client.read()
679+
```
680+
681+
Set the indexing mode to consistent
682+
683+
```python
684+
indexingPolicy = {
685+
'indexingMode': 'consistent'
686+
}
687+
```
688+
689+
Define an indexing policy with an included path and a spatial index
690+
691+
```python
692+
indexingPolicy = {
693+
"indexingMode":"consistent",
694+
"spatialIndexes":[
695+
{"path":"/location/*","types":["Point"]}
696+
],
697+
"includedPaths":[{"path":"/age/*","indexes":[]}],
698+
"excludedPaths":[{"path":"/*"}]
699+
}
700+
```
701+
702+
Define an indexing policy with an excluded path
703+
704+
```python
705+
indexingPolicy = {
706+
"indexingMode":"consistent",
707+
"includedPaths":[{"path":"/*","indexes":[]}],
708+
"excludedPaths":[{"path":"/name/*"}]
709+
}
710+
```
711+
712+
Add a composite index
713+
714+
```python
715+
indexingPolicy['compositeIndexes'] = [
716+
[
717+
{
718+
"path": "/name",
719+
"order": "ascending"
720+
},
721+
{
722+
"path": "/age",
723+
"order": "descending"
724+
}
725+
]
726+
]
727+
```
728+
729+
Update the container with changes
730+
731+
```python
732+
response = database_client.replace_container(container_client, container['partitionKey'], indexingPolicy)
733+
```
734+
669735
## Next steps
670736

671737
Read more about the indexing in the following articles:

0 commit comments

Comments
 (0)