Reputation: 33
I'm trying to filter the results of a query using a String Set. My String set is made up of countries.
Basically, after performing the query, I want the filter to look at the contents of the string set and then only display the results who's countries are in the String Set. Is this possible? Here is the code I have:
String[] countries = { "Japan", "Vietnam", "Thailand"};
Map<String, AttributeValue> eav = new HashMap<String, AttributeValue>();
eav.put(":val2", new AttributeValue().withSS(countries));
DynamoDBQueryExpression queryExpression = new DynamoDBQueryExpression()
.withFilterExpression("Place IN (:val2)") // this is the line i'm confused about
.withExpressionAttributeValues(eav)
.withHashKeyValues(someHashValue);
// Results of query should only show those with Place = Japan, Vietnam, or Thailand
Where "Place" is the name of the column I'm trying to check.
I know one method of doing this is to use multiple OR statements to check. However, I want this filter to be compatible with any String set size, so it seems like OR statements aren't the way to go.
I appreciate any guidance at all. Thanks in advance!
Upvotes: 3
Views: 3329
Reputation: 1355
One thing - a StringSet is a value type for a field of a DynamoDB item, and not something you can use to query for multiple things at once.
I looked around a bit and found some methods that would work elegantly with your design, but they're deprecated. What I would recommend is building up the query string yourself. For example...
String[] countries = {"Japan", "Vietnam", "Thailand"};
List<String> queries = new ArrayList<>();
Map<String, AttributeValue> eav = new HashMap<>();
for (Integer i = 0; i < countries.length; i++) {
String placeholder = ":val" + i;
eav.put(placeholder, new AttributeValue().withS(countries[i]));
queries.add("Place = " + placeholder);
}
String query = String.join(" or ", queries);
DynamoDBQueryExpression queryExpression = new DynamoDBQueryExpression()
.withFilterExpression(query)
.withExpressionAttributeValues(eav)
.withHashKeyValues(someHashValue);
I haven't tested it, but this is the sort of thing you'd be looking at doing (unless there's a better way that I still haven't seen).
Upvotes: 1