amazon-dynamodbvtl

What are the options for querying a DynamoDB GSI to find matching substrings?


I have a dynamodb table with a structure something like:

{
  Type: 'AWS::DynamoDB::Table',
  Properties: {
    KeySchema: [
      {
        AttributeName: 'itemID',
        KeyType: 'HASH'
      }
    ],

    AttributeDefinitions: [
      {
        AttributeName: 'itemID',
        AttributeType: 'S'
      },
      {
        AttributeName: 'label',
        AttributeType: 'S'
      }
    ],

    GlobalSecondaryIndexes: [
      {
        IndexName: 'byLabel',
        KeySchema: [
          {
            AttributeName: 'label',
            KeyType: 'HASH'
          }
        ],
        Projection: {
          ProjectionType: 'ALL'
        }
      }
    ]
  }
}

For one of my access patterns I want to get all the data that contains a label with a specific sub string. I'm very new to dynamodb, so I am unsure of the best way to achieve this.

I have it working for equals like below. HoweverI would prefer more results so returning data that contains the input would be ideal.

Current request VTL template:

{
    "version": "2018-05-29",
    "operation": "Query",
    "query": {
        "expression": "label = :label",
        "expressionValues": {
            ":label": $util.dynamodb.toDynamoDBJson($context.arguments.label)
        }
    },
    "index": "byLabel",
}

I am looking for advice on how this is usually done with a dynamodb implementation.


Solution

  • When you do a Query in dynamodb, you provide one exact (complete) hash key.

    If you want to a substring match on your hash key, you cannot use a Query. Instead you will need to do a Scan which include a FilterExpression.

    Whilst this scan will return only the items you want, it will evaluate every single item in your table, and will therefore cost you more RCUs and may slow down as your table scales (although you can offset this by using a ParallelScan).