Search code examples
amazon-dynamodbboto3dynamodb-queries

Boto3 DynamoDb scan with contains filter not returning any results


I'm trying to use a simple contains filter with boto3 DynamoDb table resource. My code is as follows:

search = "SomeProductText"
response = table.scan(
    FilterExpression='contains(product_text, :pt )',
    ExpressionAttributeValues={
        ':pt': {'S': search}
    }
)
print(response['Items'])

while 'LastEvaluatedKey' in response:
    response = table.scan(
        ExclusiveStartKey=response['LastEvaluatedKey'],
        FilterExpression='contains(product_text, :pt )',
        ExpressionAttributeValues={
            ':pt': {'S': search}
        }
    )
    print(response['Items'])

This prints out only empty lists and I'm not getting any results. When I go to the dynamodb console and try this same scan there I get many results where the product_text contains "SomeProductText" as expected. What am I doing wrong here?


Solution

  • As you didn't provide the full code snippet I'll take an educated guess to the issue. The fact you call your client table leads me to believe you're using the Resource client which takes native JSON not DynamoDB-JSON:.

    search = "SomeProductText"
    response = table.scan(
        FilterExpression='contains(product_text, :pt )',
        ExpressionAttributeValues={
            ':pt': search
        }
    )
    print(response['Items'])
    
    while 'LastEvaluatedKey' in response:
        response = table.scan(
            ExclusiveStartKey=response['LastEvaluatedKey'],
            FilterExpression='contains(product_text, :pt )',
            ExpressionAttributeValues={
                ':pt': search
            }
        )
        print(response['Items'])