javaindexingamazon-dynamodbsecondary-indexes

Conditionally adding/removing records to DynamoDb global secondary index


I have a base DDB table Attachment and it has 2 fields called customerState and isIntermediateState.

customerState can be Attaching, Detaching, Attached and Detached. isIntermediateState can be 1 (if customerState == Attaching || Detaching) or 0 (if customerState == Attached || Detached).

The customerState will go from Attaching/Detaching -> Attached/Detached.

Now I want to construct a GSI where I only want to add records from the base Attachment table if isIntermediateState = 1 and remove the records (eventually) when the isIntermediateState = 0, or the customerState becomes Attached || Detached.

Does GSI in DynamoDb allow conditionally adding and removing records from the index based on the values of the fields in the base table? Also, will it continue updating the index as the base table updates or only while initially adding the record, will it be decided whether to add to index or not?


Solution

  • There’s no conditional projection based on an expression. You can have a sparse GSI by having a GSI partition key attribute that only exists on items where YOU decide to populate it, based on any logic you like.

    Attribute exists? Goes to GSI. Attribute absent? Doesn’t go to GSI.

    So basically add a GSI1PK attribute to items whose other values indicate it should be in the GSI.