'Search Items by multiple Tags DynamoDB NodeJS

I need to do a search in my dynamoDB table that matches multiple values from a single item.

This is the type of Items i am storing:

{
   "id": "<product id>",
   "name": "Product Name",
   "price": 1.23,
   "tags": [
     "tag1",
     "tag2",
     "tag3"
]

I need to return an array of items having tags that match all of the tags a the comma-separated list.

For example: i am looking for items that only contains tags "tag1" and "tag2".

My first aproach was getting all the items from the dynamoDB table and then iterating each item to check if this condition matchs, then add the target item to an object of objects.

My approach is definetly not cost effective, Any suggestions with node.js?



Solution 1:[1]

There is not a way to index optimize this generic case (an arbitrary number of tags stored and searched) with DynamoDB.

You can optimize retrieval for one tag by adding extra items in the table where the tag is the partition key and then doing a query (with filter for the other tags) starting there.

Or you can duplicate the data to OpenSearch which is designed for this type of query.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 hunterhacker