简体   繁体   中英

Query GSI in DynamoDb with a substring of the key (begins_with)

Let's take the best practices for sort keys official documentation of DynamoDb as an example: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-sort-keys.html

Imagine we have a table like the documentation mentions, where our sort key is a composite that looks like:

[country]#[region]#[city]#[neighborhood]

For example, something like this:

Partition Key (Employee Name) Sort Key Other columns...
Antonio Spain#Madrid#Getafe#Whatever ...
Maria Spain#Andalucia#Sevilla#Whatever2 ...
Mike Spain#Madrid#Alcorcon#Whatever ...

And I'd like to get all the records from a specific country + region, so we have a partial sort key:

[country]#[region] like Spain#Madrid to get Antonio and Mike .

I know it's not possible to query by sort key directly, so I created a GSI with the inverted index (like mentioned here https://stackoverflow.com/a/64141405 )

Partition Key Sort Key Other columns...
Spain#Madrid#Getafe#Whatever Antonio ...
Spain#Andalucia#Sevilla#Whatever2 Maria ...
Spain#Madrid#Alcorcon#Whatever Mike ...

But it still looks like it's not possible to query using the begins_with operator.

var request = new QueryRequest
{
    IndexName = "GSI_Name",
    KeyConditionExpression = "begins_with(SortKey, :v_SortKey)",
    ExpressionAttributeValues = new Dictionary<string, AttributeValue> {
        {":v_SortKey", new AttributeValue { S = sortKey }},
    },
};

My question is: is there any way to achieve this without using the Scan operation which is not ideal? Or any suggestion to change my table definition to achieve this? I've been trying to think of ways of restructuring the table to accomplish this behavior, but I'm not fluent enough with DynamoDB.

Use the country as the PK and the rest as the SK. That spreads the data nicely across partitions while also enabling your access pattern.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM