Skip to content

Commit f28ea61

Browse files
Merge pull request #239339 from TheovanKraay/patch-46
Update index-metrics.md add java sample
2 parents 81fd8e4 + b1768e4 commit f28ea61

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

articles/cosmos-db/nosql/index-metrics.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Azure Cosmos DB provides indexing metrics to show both utilized indexed paths an
2121

2222
You can enable indexing metrics for a query by setting the `PopulateIndexMetrics` property to `true`. When not specified, `PopulateIndexMetrics` defaults to `false`. We only recommend enabling the index metrics for troubleshooting query performance. As long as your queries and indexing policy stay the same, the index metrics are unlikely to change. Instead, we recommend identifying expensive queries by monitoring query RU charge and latency using diagnostic logs.
2323

24-
### .NET SDK example
24+
## [.NET SDK](#tab/dotnet)
2525

2626
```csharp
2727
string sqlQueryText = "SELECT TOP 10 c.id FROM c WHERE c.Item = 'value1234' AND c.Price > 2";
@@ -43,6 +43,45 @@ You can enable indexing metrics for a query by setting the `PopulateIndexMetrics
4343
}
4444
```
4545

46+
## [Java SDK Sync](#tab/java-sync)
47+
48+
```java
49+
SqlQuerySpec querySpec = new SqlQuerySpec("SELECT TOP 10 c.id FROM c WHERE c.Item = 'value1234' AND c.Price > 2");
50+
CosmosQueryRequestOptions options = new CosmosQueryRequestOptions();
51+
options.setIndexMetricsEnabled(true);
52+
53+
CosmosPagedIterable<JsonNode> items = container.queryItems(querySpec, options, JsonNode.class);
54+
55+
// Print
56+
items.iterableByPage().forEach(itemResponse -> {
57+
logger.info("diagnostics: {}", itemResponse.getCosmosDiagnostics());
58+
for (JsonNode item : itemResponse.getResults()) {
59+
logger.info("Item: {}", item.toString());
60+
}
61+
});
62+
```
63+
64+
## [Java SDK Async](#tab/java-async)
65+
66+
```java
67+
SqlQuerySpec querySpec = new SqlQuerySpec("SELECT TOP 10 c.id FROM c WHERE c.Item = 'value1234' AND c.Price > 2");
68+
CosmosQueryRequestOptions options = new CosmosQueryRequestOptions();
69+
options.setIndexMetricsEnabled(true);
70+
71+
CosmosPagedFlux<JsonNode> items = container.queryItems(querySpec, options, JsonNode.class);
72+
73+
// Print
74+
items.byPage(100).flatMap(itemsResponse -> {
75+
logger.info("diagnostics: {}",itemsResponse.getCosmosDiagnostics());
76+
for (JsonNode item : itemsResponse.getResults()) {
77+
logger.info("Item: {}", item.toString());
78+
}
79+
executeCountQueryPrintSingleResultNumber.incrementAndGet();
80+
return Flux.just(itemsResponse);
81+
}).blockLast();
82+
```
83+
---
84+
4685
### Example output
4786

4887
In this example query, we observe the utilized paths `/Item/?` and `/Price/?` and the potential composite indexes `(/Item ASC, /Price ASC)`.

0 commit comments

Comments
 (0)