0
votes

I have a DynamoDB Table "Music". On this it has a GSI with partition key "Category" and sort key "UserRating".

I can query easily as an example for songs that are in "Category" = "Rap" and "UserRating" = 1

How ever what I would like to do for example is query songs that are in "Category" = "Rap + Rock + Jazz" and "UserRating" = 1.

Is this possible or do I need to make multiple queries and join them on the client side?

This is the code I am currently querying with:

SongDatabaseMappingAdapter songs = new SongDatabaseMappingAdapter();
            songs.setCategory("Rap");

            String userRatingQueryString = "1";

            Condition rangeKeyCondition = new Condition()
                    .withComparisonOperator(ComparisonOperator.EQ)
                    .withAttributeValueList(new AttributeValue().withN(userRatingQueryString));

            DynamoDBQueryExpression queryExpression = new DynamoDBQueryExpression()
                    .withHashKeyValues(songs)
                    .withIndexName("Category-UserRating-index")
                    .withRangeKeyCondition("UserRating", rangeKeyCondition)
                    .withConsistentRead(false);

            return mapper.query(SongDatabaseMappingAdapter.class, queryExpression);
1

1 Answers

1
votes

You'll have to make 3 different calls (i.e. one each for 'Rap, 1', 'Rock, 1' and 'Jazz, 1') and then merge the response on the client side.

If Category and UserRating were partition-key and sort-key on your table (and not on the GSI, as is your case), then you could've used the BatchGetItem API to get your data in one go. Unfortunately, the API doesn't work on GSIs.