@@ -4,7 +4,7 @@ description: Learn how to manage indexing policies in Azure Cosmos DB
4
4
author : ThomasWeiss
5
5
ms.service : cosmos-db
6
6
ms.topic : conceptual
7
- ms.date : 06/27 /2019
7
+ ms.date : 08/29 /2019
8
8
ms.author : thweiss
9
9
---
10
10
@@ -56,14 +56,45 @@ az cosmosdb collection update \
56
56
57
57
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 ` .
58
58
59
+ Retrieve the container's details
60
+
59
61
``` csharp
60
- // retrieve the container's details
61
62
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
63
68
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
67
98
await client .ReplaceDocumentCollectionAsync (containerResponse .Resource );
68
99
```
69
100
@@ -80,23 +111,82 @@ long indexTransformationProgress = container.IndexTransformationProgress;
80
111
81
112
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.
82
113
114
+ Retrieve the container's details
115
+
83
116
``` java
84
- // retrieve the container's details
85
117
Observable<ResourceResponse<DocumentCollection > > containerResponse = client. readCollection(String . format(" /dbs/%s/colls/%s" , " database" , " container" ), null );
86
118
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 );
100
190
```
101
191
102
192
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 -> {
117
207
118
208
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`.
119
209
210
+ Retrieve the container's details
211
+
120
212
```javascript
121
- // retrieve the container's details
122
213
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
124
219
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
128
262
const replaceResponse = await client. database(' database' ). container(' container' ). replace(containerResponse. body);
129
263
```
130
264
@@ -143,16 +277,63 @@ const indexTransformationProgress = replaceResponse.headers['x-ms-documentdb-col
143
277
144
278
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.
145
279
280
+ Retrieve the container' s details
281
+
146
282
```python
147
283
containerPath = ' dbs/ database/ colls/ collection'
148
- # retrieve the container's details
149
284
container = client.ReadContainer(containerPath)
150
- # set the indexing mode to Consistent
285
+ ```
286
+
287
+ Set the indexing mode to consistent
288
+
289
+ ```python
151
290
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
156
337
response = client.ReplaceContainer(containerPath, container)
157
338
```
158
339
0 commit comments