简体   繁体   English

使用 GSI 返回的 dynamoose 搜索找不到索引进行查询

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

Here's my dynamoose schema for table seller这是我的桌子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
});

As you can see above, I created a GSI with the SK as the hashkey named SKIndex and having location as the rangeKey.正如您在上面看到的,我创建了一个 GSI,其中SK作为哈希键,名为SKIndexlocation作为 rangeKey。 So I tried to perform the query below所以我尝试执行下面的查询

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()

but it will always return the error "InvalidParameter: Index can't be found for query."但它总是会返回错误"InvalidParameter: Index can't be found for query."

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

It will display the error message ValidationException: Query condition missed key schema element它将显示错误消息ValidationException: Query condition missed key schema element

Full error log:完整的错误日志:

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)"
}

As descrribed in the Dynamoose documentation , where takes in a key attribute.如 Dynamoose文档中所述, where接收关键属性。 This key represents an attribute name ( SK ), not an index name ( SKIndex ).此键表示属性名称 ( SK ),而不是索引名称 ( SKIndex )。

Changing your code to the following should work.将您的代码更改为以下应该可以工作。

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

You can also use the using function to manually set a specific index to run your query on.您还可以使用using函数手动设置特定索引以运行查询。 However this is optional.然而,这是可选的。 Dynamoose will use a system to look through your indexes and pick one that best matches your query you are making. Dynamoose 将使用一个系统来查看您的索引,并选择最符合您查询的索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM