cosmos db Hierarchical partition keys usage

Yang Chow Mun 121 Reputation points
2024-04-10T08:38:08.4033333+00:00

Hi all,

Would like to check on how is this feature can be integrated with the python sdk for below cases

  1. when quering from cosmos container how we define the partition_key. eg my Hierarchical 1 value is '123' and Hierarchical 2 value is 'ABC'. How should we define the partition (x) in the query below? cosmos.query_items(query=query, enable_cross_partition_query=True, partition_key=x)
  2. Similarly for partial document update cosmos.patch_item(item=id, partition_key=x, patch_operations=operations). How to define the x here.

Thanks!

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,631 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Amira Bedhiafi 24,531 Reputation points
    2024-04-10T11:22:22.6666667+00:00

    The hierarchical partition key is constructed by combining multiple values into a single partition key path. This is defined in your container settings.

    For example, if your partition key path is /level1/level2, you would combine level1 and level2 values into a single partition key when inserting or querying data.

    In your case with level1 value as '123' and level2 value as 'ABC', your partition key for these levels combined would typically look something like 123-ABC if you choose to use a hyphen (-) as a separator.

    However, the actual format of your hierarchical partition key depends on how you've chosen to format these keys when you inserted the data into your container.

    When you want to query or make operations on items with hierarchical partition keys with the Python SDK, you should specify the partition key by using the combined format.

    
    partition_key = '123-ABC'  # Assuming '-' is used as a separator
    
    query = "SELECT * FROM c WHERE c.someField = 'someValue'"
    
    items = cosmos.query_items(query=query, enable_cross_partition_query=True, partition_key=partition_key)
    
    

    If the case is updating a document partially, you define the partition key in the same way:

    
    partition_key = '123-ABC'  # Use the combined partition key value
    
    id = '<your-document-id>'
    
    operations = [
    
        {
    
            'op': 'add',
    
            'path': '/newField',
    
            'value': 'newValue'
    
        }
    
        # Other operations as needed
    
    ]
    
    cosmos.patch_item(item=id, partition_key=partition_key, patch_operations=operations)
    
    

    The separator I used is just an example. You need to use whatever format you've chosen when you designed your data model and inserted data into Cosmos DB and make sure that the format of the partition_key you use in your queries and operations matches exactly with how it's stored in Cosmos DB, including the order of the hierarchical levels and the separator.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.