You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Amplify Gen 2模型创建全局二级索引GSI?

在Amplify Gen2中定义GSI的方法

你没遗漏关键内容,Amplify Gen2只是把GSI的配置逻辑和本地二级索引(LSI)做了统一,文档表述上和Gen1有差异,以下是具体实现方式:

  • 标准GSI配置:通过模型的indexes字段声明
    Gen2里,全局二级索引(GSI)和本地二级索引(LSI)都通过模型定义中的indexes方法配置。当你指定的索引分区键不是模型的主分区键时,Amplify会自动创建为GSI;如果是主分区键搭配额外排序键,则为LSI。示例代码如下:

    import { type ClientSchema, a, defineData } from '@aws-amplify/backend';
    
    const schema = a.schema({
      Post: a
        .model({
          title: a.string(),
          authorId: a.string(),
          createdAt: a.dateTime(),
        })
        .authorization((allow) => [allow.publicApiKey()])
        .indexes((index) => [
          // 分区键为authorId、排序键为createdAt的GSI
          index('byAuthor', { partitionKey: 'authorId', sortKey: 'createdAt' }),
          // 仅以createdAt为分区键的GSI
          index('byCreatedDate', { partitionKey: 'createdAt' })
        ])
    });
    
    export const data = defineData({
      schema,
      authorizationModes: {
        defaultAuthorizationMode: 'apiKey',
      },
    });
    
  • 文档对应说明
    你找到的「secondary-index」文档已经覆盖了GSI的配置逻辑,Gen2将LSI和GSI统一归类为「secondary indexes」,并没有像Gen1那样单独拆分GSI的配置入口。

  • 进阶自定义场景
    如果需要修改GSI的投影类型、TTL等底层配置,才需要用到「override-resources」中提到的资源覆盖能力,直接调整底层DynamoDB的资源属性,这属于进阶操作,普通场景用模型自带的indexes配置即可满足需求。

内容的提问来源于stack exchange,提问作者nwarp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 23:12:09