Skip to content

Commit d832a04

Browse files
authored
Merge pull request #86723 from timsander1/indexingSamples
add indexing samples
2 parents a9550a2 + 8c884f7 commit d832a04

File tree

1 file changed

+212
-31
lines changed

1 file changed

+212
-31
lines changed

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

Lines changed: 212 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: Learn how to manage indexing policies in Azure Cosmos DB
44
author: ThomasWeiss
55
ms.service: cosmos-db
66
ms.topic: conceptual
7-
ms.date: 06/27/2019
7+
ms.date: 08/29/2019
88
ms.author: thweiss
99
---
1010

@@ -56,14 +56,45 @@ az cosmosdb collection update \
5656

5757
The `DocumentCollection` object from the [.NET SDK v2](https://www.nuget.org/packages/Microsoft.Azure.DocumentDB/) (see [this Quickstart](create-sql-api-dotnet.md) regarding its usage) exposes an `IndexingPolicy` property that lets you change the `IndexingMode` and add or remove `IncludedPaths` and `ExcludedPaths`.
5858

59+
Retrieve the container's details
60+
5961
```csharp
60-
// retrieve the container's details
6162
ResourceResponse<DocumentCollection> containerResponse = await client.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri("database", "container"));
62-
// set the indexing mode to Consistent
63+
```
64+
65+
Set the indexing mode to consistent
66+
67+
```csharp
6368
containerResponse.Resource.IndexingPolicy.IndexingMode = IndexingMode.Consistent;
64-
// add an excluded path
65-
containerResponse.Resource.IndexingPolicy.ExcludedPaths.Add(new ExcludedPath { Path = "/headquarters/employees/?" });
66-
// update the container with our changes
69+
```
70+
71+
Add an included path
72+
73+
```csharp
74+
containerResponse.Resource.IndexingPolicy.IncludedPaths.Add(new IncludedPath { Path = "/age/*" });
75+
```
76+
77+
Add an excluded path
78+
79+
```csharp
80+
containerResponse.Resource.IndexingPolicy.ExcludedPaths.Add(new ExcludedPath { Path = "/name/*" });
81+
```
82+
83+
Add a spatial index
84+
85+
```csharp
86+
containerResponse.Resource.IndexingPolicy.SpatialIndexes.Add(new SpatialSpec() { Path = "/locations/*", SpatialTypes = new Collection<SpatialType>() { SpatialType.Point } } );
87+
```
88+
89+
Add a composite index
90+
91+
```csharp
92+
containerResponse.Resource.IndexingPolicy.CompositeIndexes.Add(new Collection<CompositePath> {new CompositePath() { Path = "/name", Order = CompositePathSortOrder.Ascending }, new CompositePath() { Path = "/age", Order = CompositePathSortOrder.Descending }});
93+
```
94+
95+
Update container with changes
96+
97+
```csharp
6798
await client.ReplaceDocumentCollectionAsync(containerResponse.Resource);
6899
```
69100

@@ -80,23 +111,82 @@ long indexTransformationProgress = container.IndexTransformationProgress;
80111

81112
The `DocumentCollection` object from the [Java SDK](https://mvnrepository.com/artifact/com.microsoft.azure/azure-cosmosdb) (see [this Quickstart](create-sql-api-java.md) regarding its usage) exposes `getIndexingPolicy()` and `setIndexingPolicy()` methods. The `IndexingPolicy` object they manipulate lets you change the indexing mode and add or remove included and excluded paths.
82113

114+
Retrieve the container's details
115+
83116
```java
84-
// retrieve the container's details
85117
Observable<ResourceResponse<DocumentCollection>> containerResponse = client.readCollection(String.format("/dbs/%s/colls/%s", "database", "container"), null);
86118
containerResponse.subscribe(result -> {
87-
DocumentCollection container = result.getResource();
88-
IndexingPolicy indexingPolicy = container.getIndexingPolicy();
89-
// set the indexing mode to Consistent
90-
indexingPolicy.setIndexingMode(IndexingMode.Consistent);
91-
Collection<ExcludedPath> excludedPaths = new ArrayList<>();
92-
ExcludedPath excludedPath = new ExcludedPath();
93-
excludedPath.setPath("/*");
94-
// add an excluded path
95-
excludedPaths.add(excludedPath);
96-
indexingPolicy.setExcludedPaths(excludedPaths);
97-
// update the container with our changes
98-
client.replaceCollection(container, null);
99-
});
119+
DocumentCollection container = result.getResource();
120+
IndexingPolicy indexingPolicy = container.getIndexingPolicy();
121+
```
122+
123+
Set the indexing mode to consistent
124+
125+
```java
126+
indexingPolicy.setIndexingMode(IndexingMode.Consistent);
127+
```
128+
129+
Add an included path
130+
131+
```java
132+
Collection<IncludedPath> includedPaths = new ArrayList<>();
133+
ExcludedPath includedPath = new IncludedPath();
134+
includedPath.setPath("/age/*");
135+
includedPaths.add(includedPath);
136+
indexingPolicy.setIncludedPaths(includedPaths);
137+
```
138+
139+
Add an excluded path
140+
141+
```java
142+
Collection<ExcludedPath> excludedPaths = new ArrayList<>();
143+
ExcludedPath excludedPath = new ExcludedPath();
144+
excludedPath.setPath("/name/*");
145+
excludedPaths.add(excludedPath);
146+
indexingPolicy.setExcludedPaths(excludedPaths);
147+
```
148+
149+
Add a spatial index
150+
151+
```java
152+
Collection<SpatialSpec> spatialIndexes = new ArrayList<SpatialSpec>();
153+
Collection<SpatialType> collectionOfSpatialTypes = new ArrayList<SpatialType>();
154+
155+
SpatialSpec spec = new SpatialSpec();
156+
spec.setPath("/locations/*");
157+
collectionOfSpatialTypes.add(SpatialType.Point);
158+
spec.setSpatialTypes(collectionOfSpatialTypes);
159+
spatialIndexes.add(spec);
160+
161+
indexingPolicy.setSpatialIndexes(spatialIndexes);
162+
163+
```
164+
165+
Add a composite index
166+
167+
```java
168+
Collection<ArrayList<CompositePath>> compositeIndexes = new ArrayList<>();
169+
ArrayList<CompositePath> compositePaths = new ArrayList<>();
170+
171+
CompositePath nameCompositePath = new CompositePath();
172+
nameCompositePath.setPath("/name/*");
173+
nameCompositePath.setOrder(CompositePathSortOrder.Ascending);
174+
175+
CompositePath ageCompositePath = new CompositePath();
176+
ageCompositePath.setPath("/age/*");
177+
ageCompositePath.setOrder(CompositePathSortOrder.Descending);
178+
179+
compositePaths.add(ageCompositePath);
180+
compositePaths.add(nameCompositePath);
181+
182+
compositeIndexes.add(compositePaths);
183+
indexingPolicy.setCompositeIndexes(compositeIndexes);
184+
```
185+
186+
Update the container with changes
187+
188+
```java
189+
client.replaceCollection(container, null);
100190
```
101191

102192
To track the index transformation progress on a container, pass a `RequestOptions` object that requests the quota info to be populated, then retrieve the value from the `x-ms-documentdb-collection-index-transformation-progress` response header.
@@ -117,14 +207,58 @@ containerResponse.subscribe(result -> {
117207

118208
The `ContainerDefinition` interface from [Node.js SDK](https://www.npmjs.com/package/@azure/cosmos) (see [this Quickstart](create-sql-api-nodejs.md) regarding its usage) exposes an `indexingPolicy` property that lets you change the `indexingMode` and add or remove `includedPaths` and `excludedPaths`.
119209

210+
Retrieve the container's details
211+
120212
```javascript
121-
// retrieve the container's details
122213
const containerResponse = await client.database('database').container('container').read();
123-
// set the indexing mode to Consistent
214+
```
215+
216+
Set the indexing mode to consistent
217+
218+
```javascript
124219
containerResponse.body.indexingPolicy.indexingMode = "consistent";
125-
// add an excluded path
126-
containerResponse.body.indexingPolicy.excludedPaths.push({ path: '/headquarters/employees/?' });
127-
// update the container with our changes
220+
```
221+
222+
Add included path including a spatial index
223+
224+
```javascript
225+
containerResponse.body.indexingPolicy.includedPaths.push({
226+
includedPaths: [
227+
{
228+
path: "/age/*",
229+
indexes: [
230+
{
231+
kind: cosmos.DocumentBase.IndexKind.Range,
232+
dataType: cosmos.DocumentBase.DataType.String
233+
},
234+
{
235+
kind: cosmos.DocumentBase.IndexKind.Range,
236+
dataType: cosmos.DocumentBase.DataType.Number
237+
}
238+
]
239+
},
240+
{
241+
path: "/locations/*",
242+
indexes: [
243+
{
244+
kind: cosmos.DocumentBase.IndexKind.Spatial,
245+
dataType: cosmos.DocumentBase.DataType.Point
246+
}
247+
]
248+
}
249+
]
250+
});
251+
```
252+
253+
Add excluded path
254+
255+
```javascript
256+
containerResponse.body.indexingPolicy.excludedPaths.push({ path: '/name/*' });
257+
```
258+
259+
Update the container with changes
260+
261+
```javascript
128262
const replaceResponse = await client.database('database').container('container').replace(containerResponse.body);
129263
```
130264

@@ -143,16 +277,63 @@ const indexTransformationProgress = replaceResponse.headers['x-ms-documentdb-col
143277

144278
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.
145279

280+
Retrieve the container's details
281+
146282
```python
147283
containerPath = 'dbs/database/colls/collection'
148-
# retrieve the container's details
149284
container = client.ReadContainer(containerPath)
150-
# set the indexing mode to Consistent
285+
```
286+
287+
Set the indexing mode to consistent
288+
289+
```python
151290
container['indexingPolicy']['indexingMode'] = 'consistent'
152-
# add an excluded path
153-
container['indexingPolicy']['excludedPaths'] = [
154-
{"path": "/headquarters/employees/?"}]
155-
# update the container with our changes
291+
```
292+
293+
Define an indexing policy with an included path and a spatial index
294+
295+
```python
296+
container["indexingPolicy"] = {
297+
298+
"indexingMode":"consistent",
299+
"spatialIndexes":[
300+
{"path":"/location/*","types":["Point"]}
301+
],
302+
"includedPaths":[{"path":"/age/*","indexes":[]}],
303+
"excludedPaths":[{"path":"/*"}]
304+
}
305+
```
306+
307+
Define an indexing policy with an excluded path
308+
309+
```python
310+
container["indexingPolicy"] = {
311+
"indexingMode":"consistent",
312+
"includedPaths":[{"path":"/*","indexes":[]}],
313+
"excludedPaths":[{"path":"/name/*"}]
314+
}
315+
```
316+
317+
Add a composite index
318+
319+
```python
320+
container['indexingPolicy']['compositeIndexes'] = [
321+
[
322+
{
323+
"path": "/name",
324+
"order": "ascending"
325+
},
326+
{
327+
"path": "/age",
328+
"order": "descending"
329+
}
330+
]
331+
]
332+
```
333+
334+
Update the container with changes
335+
336+
```python
156337
response = client.ReplaceContainer(containerPath, container)
157338
```
158339

0 commit comments

Comments
 (0)