我正在尝试在我的 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
在 Angular 2/Typescript 中使用 IScrollUse IScroll in Angular 2 / Typescript(在 Angular 2/Typescript 中使用 IScroll)
Anime.js 在 Ionic 3 项目中不起作用anime.js not working in Ionic 3 project(Anime.js 在 Ionic 3 项目中不起作用)
Ionic 3 - 使用异步数据更新 ObservableIonic 3 - Update Observable with Asynchronous Data(Ionic 3 - 使用异步数据更新 Observable)
Angular 2:在本地 .json 文件中找不到文件Angular 2: file not found on local .json file(Angular 2:在本地 .json 文件中找不到文件)
在 Ionic 2 中,如何创建使用 Ionic 组件的自定义指In Ionic 2, how do I create a custom directive that uses Ionic components?(在 Ionic 2 中,如何创建使用 Ionic 组件的自定义指令?)
将 ViewChild 用于动态元素 - Angular 2 &离子2Use ViewChild for dynamic elements - Angular 2 amp; ionic 2(将 ViewChild 用于动态元素 - Angular 2 amp;离子2)