25

I have a DynamoDB table with feed_guid as the global secondary index. I want to query with a set of feed_guid in that table. Since feed_guid is not my primary keys, I can't use getBatchItem. When I tried the following method, I got this error:

Invalid operator used in KeyConditionExpression: OR

    $options = array(
                'TableName' => 'feed',
                    'IndexName' => 'GuidIndex',
                    'KeyConditionExpression' => 'feed_guid = :v_guid1 or feed_guid = :v_guid2',

                    'ExpressionAttributeValues' =>  array (
                        ':v_guid1' => array('S' => '8a8106e48bdbe81bf88d611f4b2104b5'),
                        ':v_guid2' => array('S' => '19cab76242a6d85717de64fe4f8acbd4')
                    ),
                    'Select' => 'ALL_ATTRIBUTES',
                );
                $response = $dynamodbClient->query($options);
craigcaulfield
  • 3,381
  • 10
  • 32
  • 40
Arun SS
  • 1,791
  • 8
  • 29
  • 48

2 Answers2

11

You can't use OR condition. You should use

rangeAttributeName BETWEEN :rangeval1 AND :rangeval2

if possible or

feed_guid IN (:v_guid1, :v_guid2)

See ExpressionAttributeValues and KeyConditionExpression

skwidbreth
  • 7,888
  • 11
  • 58
  • 105
user1697575
  • 2,830
  • 1
  • 24
  • 37
8

In order to achieve what you want here, you'll need to take the union of two separate queries.

Currently, DynamoDB's Query API only supports having one condition on Hash AND Range Keys in only the KeyConditionExpression because this limits the items you search and ultimately reduces cost of say a more complex query like what you've described here.

Raymond Lin
  • 491
  • 2
  • 5
  • 12
    Can you please give an example "How to use union of queries?" Every where in Amazon documentation they have described theoretically. – TheTiger May 12 '16 at 09:40
  • 2
    They wrote "union of two **separate** queries" (my emphasis) I.e. you should make two separate queries and combine the results – chrisbunney Jun 26 '20 at 12:28