Haibin Liu
Haibin Liu

Reputation: 610

Query global secondary index for items where field is undefined

I have this table test1 with a hash key s1 and range key s2 and the table has a global secondary index gsi1 with a hash key s3 and range key s4.

$ aws dynamodb describe-table \
    --table-name test1  
{
    "Table": {
        "AttributeDefinitions": [
            {
                "AttributeName": "s1",
                "AttributeType": "S"
            },
            {
                "AttributeName": "s2",
                "AttributeType": "S"
            },
            {
                "AttributeName": "s3",
                "AttributeType": "S"
            },
            {
                "AttributeName": "s4",
                "AttributeType": "S"
            }
        ],
        "TableName": "test1",
        "KeySchema": [
            {
                "AttributeName": "s1",
                "KeyType": "HASH"
            },
            {
                "AttributeName": "s2",
                "KeyType": "RANGE"
            }
        ],
        "TableStatus": "ACTIVE",
        "CreationDateTime": "2020-11-19T23:36:40.457000+08:00",
        "ProvisionedThroughput": {
            "LastIncreaseDateTime": "1970-01-01T08:00:00+08:00",
            "LastDecreaseDateTime": "1970-01-01T08:00:00+08:00",
            "NumberOfDecreasesToday": 0,
            "ReadCapacityUnits": 3,
            "WriteCapacityUnits": 3
        },
        "TableSizeBytes": 50,
        "ItemCount": 3,
        "TableArn": "arn:aws:dynamodb:ap-southeast-1:000000000000:table/test1",
        "GlobalSecondaryIndexes": [
            {
                "IndexName": "gsi1",
                "KeySchema": [
                    {
                        "AttributeName": "s3",
                        "KeyType": "HASH"
                    },
                    {
                        "AttributeName": "s4",
                        "KeyType": "RANGE"
                    }
                ],
                "Projection": {
                    "ProjectionType": "ALL"
                },
                "IndexStatus": "ACTIVE",
                "ProvisionedThroughput": {
                    "ReadCapacityUnits": 3,
                    "WriteCapacityUnits": 3
                },
                "IndexSizeBytes": 37,
                "ItemCount": 1,
                "IndexArn": "arn:aws:dynamodb:ddblocal:000000000000:table/test1/index/gsi1"
            }
        ]
    }
}

I have this item. Note that s4 is not there.

$ aws dynamodb get-item \
    --table-name test1 \       
    --key '{"s1":{"S":"chen"}, "s2":{"S":"yan"}}'
{
    "Item": {
        "s3": {
            "S": "fei"
        },
        "s1": {
            "S": "chen"
        },
        "s2": {
            "S": "yan"
        }
    }
}

I tried to query the index gsi1 for that item with this query and got the following error.

$ aws dynamodb query \         
    --table-name test1 \  
    --index-name gsi1 \
    --expression-attribute-values '{
        ":s3": {"S": "fei"},
        ":s4": {"NULL": true}    
    }' \
    --key-condition-expression 's3 = :s3 AND s4 = :s4'

An error occurred (ValidationException) when calling the Query operation: One or more parameter values were invalid: Condition parameter type does not match schema type

I also tried this query and got no items.

$ aws dynamodb query \
    --table-name test1 \
    --index-name gsi1 \
    --expression-attribute-values '{
        ":s3": {"S": "fei"} 
    }' \
    --key-condition-expression 's3 = :s3'
{
    "Items": [],
    "Count": 0,
    "ScannedCount": 0,
    "ConsumedCapacity": null
}

Any idea how I can query that index for that item?

Upvotes: 0

Views: 812

Answers (1)

Charles
Charles

Reputation: 23803

You can't.

If the item doesn't have an attribute required for the GSI keys, it's not added to the GSI.

This is known as a "sparse index"

Upvotes: 2

Related Questions