How to do a search using "contains" with DynamoDB

I am trying to execute a search function in a React application.

I have a DynamoDB table:

---------------------
movie_id | movie_name
---------------------
1        | name a
---------------------
2        | name b
---------------------

I want to make a search function to search for “b” in the search input of a React application and get the result “name b” from the database.

I tried querywith CONTAINS, but didn't work, and didn't seem to be the right way to do this.

const SEARCH_KEYWORD = "b";

let params = {
   TableName : 'TABLE_NAME',
   KeyConditionExpression: "contains(#movie_name, :movie_name)",
   ExpressionAttributeNames:{
     "#movie_name": 'movie_name'
   },
   ExpressionAttributeValues:{
       ":movie_name": SEARCH_KEYWORD
   }
};
documentClient.query(params, function(err, data) {
  console.log(data);
});

What is the best way to create a search function in my React app with DynamoDB?

Does it make sense to run a query on a search keyword to check if the data contains a keyword value?

+4
source share
1

CONTAINS API query. API scan (. ).

:

const AWS = require('aws-sdk');
const documentClient = new AWS.DynamoDB.DocumentClient();
const SEARCH_KEYWORD = "b";

let params = {
    TableName : 'TABLE_NAME',
    FilterExpression: "contains(#movie_name, :movie_name)",
    ExpressionAttributeNames: {
        "#movie_name": "movie_name",
    },
    ExpressionAttributeValues: {
        ":movie_name": SEARCH_KEYWORD,
    }       
};

documentClient.scan(params, function(err, data) {
    console.log(data);
});

:

{ 
    Items: [ 
        { 
            movie_id: 2,
            movie_name: 'name b' 
        } 
    ], 
    Count: 1, 
    ScannedCount: 2 
}
+3

Source: https://habr.com/ru/post/1676417/


All Articles