sid*_*491 1 amazon-dynamodb boto3
Is there any way to pass a list of search strings in the contains() method of FilterExpression in DynamoDb?
Something like below:
search_str = ['value-1', 'value-2', 'value-3']
result = kb_table.scan(
FilterExpression="contains (title, :titleVal)",
ExpressionAttributeValues={ ":titleVal": search_str }
)
Run Code Online (Sandbox Code Playgroud)
For now I can only think of looping through the list and scanning the table multiple times (as in below code), but I think it will be resource heavy.
for item in search_str:
result += kb_table.scan(
FilterExpression="contains (title, :titleVal)",
ExpressionAttributeValues={ ":titleVal": item }
)
Run Code Online (Sandbox Code Playgroud)
Any suggestions.
对于上述场景,CONTAINS应与OR条件一起使用。当您将数组作为 的输入时CONTAINS,DynamoDB 将检查 SET 属性(“SS”、“NS”或“BS”)。它不会在字符串属性上查找子序列。
如果比较的目标属性是字符串类型,则运算符检查子字符串匹配。如果比较的目标属性是 Binary 类型,则运算符会查找与输入匹配的目标的子序列。如果比较的目标属性是一个集合(“SS”、“NS”或“BS”),那么如果找到与集合的任何成员完全匹配的运算符,则计算结果为真。
例子:-
movies1 = "MyMovie"
movies2 = "Big New"
fe1 = Attr('title').contains(movies1)
fe2 = Attr('title').contains(movies2)
response = table.scan(
FilterExpression=fe1 or fe2
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3420 次 |
| 最近记录: |