gpt4 book ai didi

java - JSON 子数组搜索

转载 作者:行者123 更新时间:2023-12-01 08:48:50 24 4
gpt4 key购买 nike

{
"name": "testcase #1",
"words": [
"the",
"quick",
"brown",
"fox",
"jump",
"over",
"the",
"lazy",
"dog"
],
"values": [
1,
1,
4,
4,
1,
6,
1,
3,
5
]
},
{
"name": "testcase #2",
"words": [
"the",
"second",
"test",
"about",
"jump",
"over",
"the",
"lazy",
"dog"
],
"values": [
3,
2,
4,
5,
3,
6,
4,
3,
1
]
}

我如何在 dynamoDb 中制定查询来搜索某个“单词”在特定值范围内的所有记录。对于上面的示例记录,它将匹配以下任何一条记录并返回整个匹配记录。

"the" < 2
"the" = 1
"brown" > 3

我还可以要求返回所有值小于 3 的“单词”;

"the", "quick", "second", "dog"

我四处搜索,但找不到关于如何在不必扫描整个表的情况下执行此操作的明确文档,这可能会对性能和成本产生重大影响。

最佳答案

实际上,用帖子中提供的上述结构来构成过滤表达式是不可行的。

数据模型的拟议更改:-

words 属性存储为 map DynamoDB 数据类型。

示例:-

Storing words as map

查询:-

请注意,如果您想使用 DynamoDB 查询 API,您必须拥有 HASH Key 数据。如果您没有哈希键数据,则需要使用Scan API或需要GSI(全局二级索引)。

按字数过滤数据的示例查询表达式:-

请注意,我在 KeyConditionExpression 中使用了哈希键值“testcase 1”,并在 FilterExpression 上使用了其他属性。

如果您没有哈希 key ,则需要使用 Scan API。

var table = "testcase";

var params = {
TableName : table,
KeyConditionExpression : '#name = :hkey',
FilterExpression: 'words.the < :wordval1 and words.the = :wordval2 and words.brown > :wordval3',
ExpressionAttributeNames : {
'#name' : 'name'
},
ExpressionAttributeValues : {
':hkey' : 'testcase 1',
':wordval1' : 2,
':wordval2' : 1,
':wordval3' : 3
}
};

docClient.query(params, function(err, data) {
if (err) {
console.error("Unable to read item. Error JSON:", JSON.stringify(err,
null, 2));
} else {
console.log("GetItem succeeded:", JSON.stringify(data, null, 2));
}
});

关于java - JSON 子数组搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42517208/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com