Search code examples
amazon-dynamodbcomposite-keysecondary-indexesdynamodb-gsi

Dynamodb global secondary index composite sort key


I'm new to dynamodb and trying to create a global secondary index for addition access patterns.

How can I create dynamoDB GSI with composite sort key?

For example: my primary table stored orders, which has these fields User_ID,ID, Type, Product_name, Total_Value, Created_at

On my primary table, the partition key is User_ID and sort key is Created_at, so I can get user's order history for the UI. My application also needs to get all orders by name, filtered by type, sorted by total_value and Created_at so I'm thinking of create a GSI with:

  • Parition key: Product_name
  • Sort key: Type#Total_value#Created_at

But when creating a new GSI I encountered this error:

Global Secondary Index range key not specified in Attribute Definitions.Type unknown.

So what should I do to create that GSI?


Solution

  • There is no automatic concatenation of values. You need to create an attribute for the GSI sort key (using any legal attribute name), construct a GSI referencing it, and put into it (via your code) the concatenated value you want there.