简体   繁体   English

无法使用 DynamoDB GSI

[英]Can't use DynamoDB GSI

I write in Ruby On Jets and use Dynomite to work with DynamoDB.我写在Ruby On Jets中并使用Dynomite与 DynamoDB 一起工作。 And I have a problem with GSI.我对 GSI 有疑问。
I have a table that has 3 fields: display, value, title_hilight .我有一个包含 3 个字段的表: display, value, title_hilight I need to use search across all three fields.我需要在所有三个字段中使用搜索。 For this, I decided to use the global secondary index.为此,我决定使用全局二级索引。 For testing purposes, I decided to add GSI for the "display" field.出于测试目的,我决定为“显示”字段添加 GSI。 I created migration我创建了迁移

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

Then I created a model然后我创建了一个 model

require "active_model"

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

Now I'm trying to find a record by value from the "display" field:现在我试图从“显示”字段中按值查找记录:
ModelName.where ({display: 'asd'}) and I'm getting that error: ModelName.where ({display: 'asd'})我收到了这个错误:

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

Here is the output of aws dynamodb describe-table --table-name table-name --endpoint-url http://localhost:8000这是 output 的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"
            }
        ]
    }
}

I changed the name of the real table to SomeTableName (sometimes just table-name).我将真实表的名称更改为 SomeTableName(有时只是表名)。 The rest of the code remained unchanged.代码rest保持不变。 Thanks for help感谢帮助

As mentioned 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.在 DynamoDB 中,您可以选择在表上创建一个或多个二级索引,并以与查询表相同的方式查询这些索引。

You need to specify GSI name explicitly in your query.您需要在查询中明确指定 GSI 名称。

@jny answer is correct. @jny 答案是正确的。 He told me to use a different index.他告诉我使用不同的索引。 I don't know how to use a different model (see comments on his answer), but the idea with an index is very, very correct.我不知道如何使用不同的 model(请参阅他的回答评论),但带有索引的想法非常非常正确。 This is how everything works for me now这就是现在一切对我有用的方式

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

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

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