【问题标题】:dynamoose search using GSI returns Index can't be found for querydynamoose search 使用 GSI 返回 Index can't be found for query
【发布时间】:2021-06-26 20:36:06
【问题描述】:

这是我的表 seller 的 dynamoose 架构

const schema = new dynamoose.Schema({
    PK: {
        type: String,   //ni letak emel.toLowerCase() + #main/business/delivery/ehailing
        hashKey: true,
    },
    SK: {        
        type: String,  
        rangeKey: true,
        "index": {  //utk 'auto' display kedai bila user ada kat location tu
            "name": "SKIndex",
            "global": true,
            "rangeKey": "location"
        }
    },
    "location": String,
}, {
    "saveUnknown": true,
    "timestamps": true
});

如您在上面看到的,我创建了一个 GSI,其中 SK 作为名为 SKIndex 的 hashkey,并以 location 作为 rangeKey。所以我尝试执行下面的查询

var SKIndex_search = "some value"
var locality = "some value too"
var filter = new dynamoose.Condition().where("SKIndex").eq(SKIndex_search).filter("location").beginsWith(locality);
var getResult = await Seller.query(filter).exec()

但它总是会返回错误"InvalidParameter: Index can't be found for query."

=============== 运行此查询时Seller.query(SKIndex_search).using("SKIndex").filter("location").beginsWith(locality).exec()

会显示错误信息ValidationException: Query condition missed key schema element

完整的错误日志:

aws:dynamodb:describeTable:response - {
    "Table": {
        "AttributeDefinitions": [
            {
                "AttributeName": "PK",
                "AttributeType": "S"
            },
            {
                "AttributeName": "SK",
                "AttributeType": "S"
            },
            {
                "AttributeName": "location",
                "AttributeType": "S"
            }
        ],
        "TableName": "earthlings_seller",
        "KeySchema": [
            {
                "AttributeName": "PK",
                "KeyType": "HASH"
            },
            {
                "AttributeName": "SK",
                "KeyType": "RANGE"
            }
        ],
        "TableStatus": "ACTIVE",
        "CreationDateTime": "2021-06-26T20:50:13.233Z",
        "ProvisionedThroughput": {
            "LastIncreaseDateTime": "1970-01-01T00:00:00.000Z",
            "LastDecreaseDateTime": "1970-01-01T00:00:00.000Z",
            "NumberOfDecreasesToday": 0,
            "ReadCapacityUnits": 1,
            "WriteCapacityUnits": 1
        },
        "TableSizeBytes": 312,
        "ItemCount": 1,
        "TableArn": "arn:aws:dynamodb:ddblocal:000000000000:table/earthlings_seller",
        "GlobalSecondaryIndexes": [
            {
                "IndexName": "SKIndex",
                "KeySchema": [
                    {
                        "AttributeName": "SK",
                        "KeyType": "HASH"
                    },
                    {
                        "AttributeName": "location",
                        "KeyType": "RANGE"
                    }
                ],
                "Projection": {
                    "ProjectionType": "ALL"
                },
                "IndexStatus": "ACTIVE",
                "ProvisionedThroughput": {
                    "ReadCapacityUnits": 1,
                    "WriteCapacityUnits": 1
                },
                "IndexSizeBytes": 312,
                "ItemCount": 1,
                "IndexArn": "arn:aws:dynamodb:ddblocal:000000000000:table/earthlings_seller/index/SKIndex"
            }
        ]
    }
}
aws:dynamodb:query:request - {
    "ExpressionAttributeNames": {
        "#qra": "location"
    },
    "ExpressionAttributeValues": {
        ":qrv": {
            "S": "nilai"
        }
    },
    "TableName": "earthlings_seller",
    "IndexName": "SKIndex",
    "KeyConditionExpression": "begins_with (#qra, :qrv)"
}

【问题讨论】:

  • where 接受属性名称 (SK) 而不是索引名称 (SKIndex)。您可以使用using 方法设置索引名称。但是,如果您将条件更改为使用where("SK"),它应该可以工作。
  • 将其更改为where("SK") 有效,谢谢!所以在幕后,dynamoose/dynamodb 将尝试自行确定查询中的给定属性名称是“主”索引还是全局索引?无论如何,我也尝试了 usingSeller.query(SKIndex_search).using("SKIndex").filter("location").beginsWith(locality).exec() 的方法,但我会收到这个错误 ValidationException: Query condition missed key schema element
  • SKIndex_search 设置为什么?
  • @CharlieFish SKIndex_search 只是用户提供的一个值,在这种情况下它是用户的国家代码,例如,myau
  • 哦,从来不知道 Dynamoose 有自己的 Slack 频道。到时我会在那里见你。谢谢!

标签: amazon-dynamodb dynamoose


【解决方案1】:

Dynamoose documentation 中所述,where 包含一个关键属性。此键表示属性名称 (SK),而不是索引名称 (SKIndex)。

将您的代码更改为以下应该可以工作。

new dynamoose.Condition().where("SK").eq(SKIndex_search).filter("location").beginsWith(locality);

您还可以使用using 函数手动设置特定索引以运行您的查询。但是,这是可选的。 Dynamoose 将使用一个系统来查看您的索引并选择最符合您正在查询的索引。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-24
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 2017-10-24
    • 2018-03-25
    相关资源
    最近更新 更多