2

The format of my data looks like this

{
  ID:'some uuid'
  Email:'[email protected]',
  Tags=[tag1,tag2,tag3...],
  Content:' some content'
}

The partition key is ID and the sort key is Email I created a secondary index of email which is "email_index" if I only want to query by Email, Now I want to query data both by Email and by a specific tag For example I want to find all data that Email='[email protected]' and Tags contains 'tag2', I want to first query by "email_index"

result=table.query(
            IndexName='EmailIndex',
            KeyConditionExpression='Email=:email',
            ExpressionAttributeValues={
                ':email':'[email protected]'
            }
)['Items']

then scan the result with Attr('Tags').contains('tag2') So is it possible to do both at the same time? Or I have to write a loop to filter query results in Python?

3
  • Why not add the Tags attribute to the EmailIndex as a sort key? Commented Nov 11, 2020 at 1:39
  • I'm not sure how to create sort key for a list.I thought it's only possible to create sort key for number, string and binary Commented Nov 11, 2020 at 4:06
  • Tags is a list so it's possible to add many different tags to it Commented Nov 11, 2020 at 4:07

1 Answer 1

3

Tags can be a tricky use case for DynamoDB.

One option is to use a FilterExpression on your query operation

result=table.query(
            IndexName='EmailIndex',
            KeyConditionExpression='Email=:email',
            FilterExpression: 'contains(Tags, :tag)',
            ExpressionAttributeValues={
                ':email':'[email protected]',
                ':tag': 'tag1'
            }
)['Items']

Another option, as you've outlined, is to do the check in your application code.

If this isn't flexible enough for your use case, you may want to look into a more robust search solution like Elasticsearch.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.