Add an inverted index

In this step, you add an inverted index to the table. An inverted index is created like any other global secondary index (GSI).

In the code you downloaded, a add_inverted_index.py script is in the scripts/ directory. This Python script adds an inverted index to your table.

import boto3

dynamodb = boto3.client('dynamodb')

try:
    dynamodb.update_table(
        TableName='battle-royale',
        AttributeDefinitions=[
            {
                "AttributeName": "PK",
                "AttributeType": "S"
            },
            {
                "AttributeName": "SK",
                "AttributeType": "S"
            }
        ],
        GlobalSecondaryIndexUpdates=[
            {
                "Create": {
                    "IndexName": "InvertedIndex",
                    "KeySchema": [
                        {
                            "AttributeName": "SK",
                            "KeyType": "HASH"
                        },
                        {
                            "AttributeName": "PK",
                            "KeyType": "RANGE"
                        }
                    ],
                    "Projection": {
                        "ProjectionType": "ALL"
                    },
                    "ProvisionedThroughput": {
                        "ReadCapacityUnits": 1,
                        "WriteCapacityUnits": 1
                    }
                }
            }
        ],
    )
    print("Table updated successfully.")
    
except Exception as e:
    print("Could not update table. Error:")
    print(e)

Edit scripts/add_inverted_index.py, set both ReadCapacityUnits and WriteCapacityUnits to 100 for InvertedIndex.

In this script, you call an update_table() method on the DynamoDB client. In the method, you pass details about the secondary index you want to create, including the key schema for the index, the provisioned throughput, and the attributes to project into the index.

Run the script by typing the following command in your terminal:

python scripts/add_inverted_index.py

Your terminal will display output that your index was created successfully.

Table updated successfully.

It will take a few minutes for the new secondary index to get populated. You need to wait until the secondary index is active.

You can find out the current status of the table and its indexes by either way:

  • Checking under Services, Database, DynamoDB in the AWS console.

  • Running the command below in the Cloud9 Terminal:

    aws dynamodb describe-table --table-name battle-royale --query "Table.GlobalSecondaryIndexes[].IndexStatus"
    

    You also can script the command to run every 2 seconds using watch.

    # Watch checks every 2 seconds by default
    watch -n 2 "aws dynamodb describe-table --table-name battle-royale --query \"Table.GlobalSecondaryIndexes[].IndexStatus\""
    

    Press Ctrl + C to end watch after the global secondary index has been created.