Skip to content

Commit 3232358

Browse files
authored
Merge pull request #281837 from AvijitkGupta/indexing_vcore
indexing_scenarios
2 parents 425d405 + 2cd0b17 commit 3232358

File tree

3 files changed

+572
-11
lines changed

3 files changed

+572
-11
lines changed

articles/cosmos-db/mongodb/vcore/TOC.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@
9696
href: background-indexing.md
9797
- name: Indexing best practices
9898
href: how-to-create-indexes.md
99+
- name: Indexing scenarios
100+
href: how-to-index.md
99101
- name: Working with wildcard
100102
href: how-to-create-wildcard-indexes.md
101103
- name: Search and query text

articles/cosmos-db/mongodb/vcore/how-to-create-wildcard-indexes.md

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,65 @@ ms.reviewer: sidandrews
88
ms.service: azure-cosmos-db
99
ms.subservice: mongodb-vcore
1010
ms.topic: conceptual
11-
ms.date: 6/25/2024
11+
ms.date: 7/30/2024
1212
---
1313

1414

1515
# Create wildcard indexes in Azure Cosmos DB for MongoDB vCore
1616

1717
[!INCLUDE[MongoDB vCore](~/reusable-content/ce-skilling/azure/includes/cosmos-db/includes/appliesto-mongodb-vcore.md)]
1818

19-
While most workloads have a predictable set of fields used in query filters and predicates, adhoc query patterns may use filters on any field in the json document structure.
19+
Workloads which have an unpredictable set of fields in the schema can use wildcard indexes to support queries against arbitrary or unknown fields, for optimized performance.
2020

2121
Wildcard indexing can be helpful in the following scenarios:
2222
- Queries filtering on any field in the document making indexing all fields through a single command easier than indexing each field individually.
2323
- Queries filtering on most fields in the document making indexing all but a few fields through a single easier than indexing most fields individually.
2424

25+
## Indexing all fields
26+
27+
Set up a wildcard index to facilitate queries on all possible document fields, including those with unknown or dynamic names.
28+
29+
```javascript
30+
db.collection.createIndex( { "$**": 1 } )
31+
```
32+
33+
> [!IMPORTANT]
34+
> For large collections, we recommend using alternate approach defined later in this doc.
35+
36+
## Include or exclude specific fields
37+
38+
Wildcard indexes can also be restricted to specific fields while excluding certain fields from being targeted for indexing. Let's review a sample for the following Json.
39+
40+
```json
41+
{
42+
"firstName": "Steve",
43+
"lastName": "Smith",
44+
"companyName": "Microsoft",
45+
"division": "Azure",
46+
"timeInOrgInYears": 7
47+
}
48+
```
49+
50+
We can control the indexing behavior, the example restricts creating indexes on `firstName`,`lastName` & `timeInOrgInYears` field.
51+
52+
```javascript
53+
db.collection.createIndex( { "$**": 1 },
54+
{"wildcardProjection" : { "firstName": 0
55+
, "lastName": 0
56+
, "companyName": 1
57+
, "division": 1
58+
, "timeInOrgInYears": 0
59+
}
60+
}
61+
)
62+
```
63+
64+
In the wildcardProjection document, the value 0 or 1 indicates whether the field is included (1) or excluded (0) from indexing.
65+
66+
## Alternative for indexing all fields
67+
2568
This sample describes a simple workaround to minimize the effort needed to create individual indexes until wildcard indexing is generally available in Azure Cosmos DB for MongoDB vCore.
2669

27-
## Solution
2870
Consider the json document below:
2971
```json
3072
{
@@ -70,9 +112,9 @@ While this sample document only requires a combination of 10 fields to be explic
70112

71113
The jar file detailed in the rest of this document makes indexing fields in larger documents simpler. The jar takes a sample JSON document as input, parses the document and executes createIndex commands for each field without the need for user intervention.
72114

73-
## Prerequisites
115+
### Prerequisites
74116

75-
### Java 21
117+
#### Java 21
76118
After the virtual machine is deployed, use SSH to connect to the machine, and install CQLSH using the below commands:
77119

78120
```bash
@@ -81,7 +123,7 @@ sudo apt update
81123
sudo apt install openjdk-21-jdk
82124
```
83125

84-
## Sample jar to create individual indexes for all fields
126+
### Sample jar to create individual indexes for all fields
85127

86128
Clone the repository containing the Java sample to iterate through each field in the JSON document's structure and issue createIndex operations for each field in the document.
87129

@@ -99,7 +141,7 @@ The cloned repository does not need to be built if there are no changes to be ma
99141
java -jar azure-cosmosdb-mongo-data-indexer-1.0-SNAPSHOT.jar mongodb+srv://<user>:<password>@abinav-test-benchmarking.global.mongocluster.cosmos.azure.com/?tls=true&authMechanism=SCRAM-SHA-256&retrywrites=false&maxIdleTimeMS=120000 cosmicworks employee sampleEmployee.json
100142
```
101143

102-
## Track the status of a createIndex operation
144+
### Track the status of a createIndex operation
103145
The jar file is designed to not wait on a response from each createIndex operation. The indexes are created asynchronously on the server and the progress of the index build operation on the cluster can be tracked.
104146

105147
Consider this sample to track indexing progress on the 'cosmicworks' database.
@@ -143,8 +185,8 @@ When a createIndex operation is in progress, the response looks like:
143185
}
144186
```
145187

146-
## Related content
147-
148-
Check out the full sample here - https://github.com/Azure-Samples/cosmosdb-mongodb-vcore-wildcard-indexing
188+
## Next Steps
149189

150-
Check out [indexing best practices](how-to-create-indexes.md), which details best practices for indexing on Azure Cosmos DB for MongoDB vCore.
190+
- Refer for code sample - https://github.com/Azure-Samples/cosmosdb-mongodb-vcore-wildcard-indexing
191+
- Review here for [Indexing and Limitations](indexing.md)
192+
- Learn about [Indexing best practices](how-to-create-indexes.md)

0 commit comments

Comments
 (0)