问题描述
我正在尝试在我的 React 应用上创建搜索功能.
I'm trying to make search function on my React app.
我有这张 DynamoDB 表:
I have this DynamoDB table:
---------------------
movie_id | movie_name
---------------------
1 | name a
---------------------
2 | name b
---------------------
我想创建一个搜索功能,在 React 应用的搜索输入中搜索b",并从数据库中获取name b"作为结果.
I want to make a search function to search "b" on the React app's search input and get "name b" from the DB as the result.
我尝试使用 CONTAINS
进行 query
,但没有奏效,而且似乎不是正确的方法.
I tried to query
with CONTAINS
but didn't work and does not seem to be a proper way to do it.
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);
});
使用 DynamoDB 在我的 React 应用程序上创建搜索功能的最佳方法是什么?
What is the best way to create search function on my React app with DynamoDB?
通过搜索关键字运行查询以检查数据是否包含关键字值是否有意义?
Does it even make sense to run a query by the search keyword to check if the data contains keyword value?
推荐答案
CONTAINS
运算符在 query
API 中不可用.您需要为此使用 scan
API(查看此链接).
The CONTAINS
operator is not available in the query
API. You need to use the scan
API for this (see this link).
尝试以下方法:
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
}
这篇关于如何使用“包含"进行搜索?使用 DynamoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!