簡體   English   中英

使用 GSI 返回的 dynamoose 搜索找不到索引進行查詢

[英]dynamoose search using GSI returns Index can't be found for query

這是我的桌子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作為哈希鍵,名為SKIndexlocation作為 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)"
}

如 Dynamoose文檔中所述, where接收關鍵屬性。 此鍵表示屬性名稱 ( SK ),而不是索引名稱 ( SKIndex )。

將您的代碼更改為以下應該可以工作。

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

您還可以使用using函數手動設置特定索引以運行查詢。 然而,這是可選的。 Dynamoose 將使用一個系統來查看您的索引,並選擇最符合您查詢的索引。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM