Arun A K
Arun A K

Reputation: 2225

Exclude Path in Azure Cosmos DB

What is the correct JSON to exclude certain keys from an input json to be not indexed by Azure CosmosDB. We are using the CosmosDB in mongodb mode. Was planning to change the index configuration on the Azure Portal after creating the collection.

Sample Input Json being

{
    "name": "test",
    "age": 1,
    "location": "l1",
    "height":5.7
}

If I were to include name and age in the index and remove location and height from the index, what does the includedPaths and excludedPaths look like.

Upvotes: 2

Views: 3917

Answers (2)

Tomasz Swider
Tomasz Swider

Reputation: 2382

It looks like underlying implementation has changed and at the time of writing the documentation does not cover changing indexPolicy in MongoDB flavoured CosmosBD. Because the documents are really stored in a wired way where all the keys start from root $v and all scalar fields are stored as documents, containing value and type information. So your document will be stored something like:

{
  '_etag': '"2f00T0da-0000-0d00-0000-4cd987940000"',
  'id': 'SDSDFASDFASFAFASDFASDF',
  '_self': 'dbs/sMsxAA==/colls/dVsxAI8MBXI=/docs/sMsxAI8MBXIKAAAAAAAAAA==/',
  '_rid': 'sMsxAI8MBXIKAAAAAAAAAA==',
  '$t': 3,
  '_attachments': 'attachments/',
  '$v': {
    '_id': {
      '$t': 7,
      '$v': "\\Ù\x87\x14\x01\x15['\x18m\x01ú"
    },
    'name': {
      '$t': 2,
      '$v': 'test'
    },
    'age': {
      '$t': 1,
      '$v': 1
    },
    ...
  },
  '_ts': 1557759892
}

Therefore the indexingPolicy paths need to include the root $v and use /* (objects) instead of /? (scalars).

{
  "indexingMode": "consistent",
  "automatic": true,
  "includedPaths": [
    {
      "path": "/*",
      "indexes": [
        {
          "kind": "Range",
          "dataType": "Number"
        },
        {
          "kind": "Hash",
          "dataType": "String"
        }
      ]
    }
  ],
  "excludedPaths": [
    {"path": "/\"$v\"/\"location\"/*"},
    {"path": "/\"$v\"/\"height\"/*"}
  ]
}

PS: Also mongo api can be used to drop all the default indexes and create specific indexes as required https://learn.microsoft.com/en-us/azure/cosmos-db/mongodb-indexing

Upvotes: 0

Arun A K
Arun A K

Reputation: 2225

Finally got it to work with the below spec:-

{
    "indexingMode": "consistent",
    "automatic": true,
    "includedPaths": [{
        "path": "/*",
        "indexes": [{
                "kind": "Range",
                "dataType": "Number",
                "precision": -1
            },
            {
                "kind": "Hash",
                "dataType": "String",
                "precision": 3
            }
        ]
    }],
    "excludedPaths": [{
            "path": "/\"location\"/?"
        },
        {
            "path": "/\"height\"/?"
        }
    ]
}

Upvotes: 5

Related Questions