无法使用 DynamoDB GSI

Can't use DynamoDB GSI

我在 Ruby On Jets 中编写并使用 Dynomite 来处理 DynamoDB。我对 GSI 有疑问。
我有一个包含 3 个字段的 table:display, value, title_hilight。我需要在所有三个字段中使用搜索。为此,我决定使用全局二级索引。出于测试目的,我决定为“显示”字段添加 GSI。 我创建了迁移

class SomeTableMigration<Dynomite::Migration
  def up
    create_table 'table-name' do | t |
      t.partition_key "id: string: hash" # required

      t.gsi do | i |
        i.partition_key "display: string"
      end
    end
  end
end

然后我创建了一个模型

require "active_model"

class ModelName<ApplicationItem
  self.set_table_name 'some-model-name'
  column :id, :display,:val, :title_hilight
end

现在我正尝试从“显示”字段中按值查找记录:
ModelName.where ({display: 'asd'}) 我收到了错误:

Aws::DynamoDB::Errors::ValidationException (Query condition missed key schema element)

这是aws dynamodb describe-table --table-name table-name --endpoint-url http://localhost:8000

的输出
{
    "Table": {
        "AttributeDefinitions": [
            {
                "AttributeName": "id",
                "AttributeType": "S"
            },
            {
                "AttributeName": "display",
                "AttributeType": "S"
            }
        ],
        "TableName": "some-table-name",
        "KeySchema": [
            {
                "AttributeName": "id",
                "KeyType": "HASH"
            }
        ],
        "TableStatus": "ACTIVE",
        "CreationDateTime": "2020-10-26T14:52:59.589000+03:00",
        "ProvisionedThroughput": {
            "LastIncreaseDateTime": "1970-01-01T03:00:00+03:00",
            "LastDecreaseDateTime": "1970-01-01T03:00:00+03:00",
            "NumberOfDecreasesToday": 0,
            "ReadCapacityUnits": 5,
            "WriteCapacityUnits": 5
        },
        "TableSizeBytes": 112,
        "ItemCount": 1,
        "TableArn": "arn:aws:dynamodb:ddblocal:000000000000:table/some-table-name",
        "GlobalSecondaryIndexes": [
            {
                "IndexName": "display-index",
                "KeySchema": [
                    {
                        "AttributeName": "display",
                        "KeyType": "HASH"
                    }
                ],
                "Projection": {
                    "ProjectionType": "ALL"
                },
                "IndexStatus": "ACTIVE",
                "ProvisionedThroughput": {
                    "ReadCapacityUnits": 5,
                    "WriteCapacityUnits": 5
                },
                "IndexSizeBytes": 112,
                "ItemCount": 1,
                "IndexArn": "arn:aws:dynamodb:ddblocal:000000000000:table/some-table-name/index/display-index"
            }
        ]
    }
}

我将真正的 table 的名称更改为 SomeTableName(有时只是 table-name)。其余代码保持不变。感谢帮助

如前所述here

In DynamoDB, you can optionally create one or more secondary indexes on a table and query those indexes in the same way that you query a table.

您需要在查询中明确指定 GSI 名称。

@jny 回答正确。他告诉我使用不同的索引。我不知道如何使用不同的模型(参见他的回答的评论),但索引的想法非常非常正确。这就是现在一切对我有用的方式

ModelName.query(
  index_name: 'display-index',
  expression_attribute_names: { "#display_name" => "display" },
  expression_attribute_values: { ":display_value" => "das" },
  key_condition_expression: "#display_name = :display_value",
)