我试图在排序键上使用ComparisonOperator IN进行查询筛选,但遇到了此错误。
QueryFilter只能包含非主键属性
我知道ComparisonOperator IN是不可索引的,这就是为什么我不将其设置为RangeKeyCondition。但是,我只想对查询结果进行扫描筛选。
这是我的代码:
Tax hashKeyValue = new Tax();
hashKeyValue.setUserId(userId);
Condition scanFilterCondition = new Condition()
.withComparisonOperator(ComparisonOperator.IN)
.withAttributeValueList(taxIds.stream()
.map(t -> new AttributeValue().withS(t))
.collect(Collectors.toList())
);
Map<String, Condition> conditions = new HashMap<>();
conditions.put("id", scanFilterCondition);
DynamoDBQueryExpression<Transaction> dynamoDBQueryExpression = new DynamoDBQueryExpression<Transaction>()
.withIndexName(null)
.withConsistentRead(false)
.withHashKeyValues(hashKeyValue)
.withQueryFilter(conditions);
return getMapper().query(Transaction.class, dynamoDBQueryExpression);
我找到了一个变通方法,但对我来说效果很好。
我使用了另一个GSI来查询表,这样我筛选的排序键就不再是主键了。
我愿意接受其他解决方案或意见:)