创建索引 - Amazon Kendra

创建索引

您可以使用控制台或通过调用 CreateIndex API 来创建索引。您可以将 AWS Command Line Interface(AWS CLI)或 SDK 与该 API 配合使用。创建索引后,您可以直接向索引中添加文档,也可以从数据来源添加文档。

要创建索引,您必须提供 AWS Identity and Access Management(IAM)角色的 Amazon 资源名称(ARN)以供索引访问 CloudWatch。有关更多信息,请参阅索引的 IAM 角色

以下选项卡提供了使用 AWS 管理控制台创建索引的过程,以及使用 AWS CLI、Python 和 Java 软件开发工具包创建索引的代码示例。

Console
创建索引
  1. 登录 AWS 管理控制台,然后通过以下网址打开 Amazon Kendra 控制台:https://console.aws.amazon.com/kendra/

  2. 索引部分选择创建索引

  3. 指定索引详细信息中,指定索引都名称和描述。

  4. IAM 角色中提供一个 IAM 角色。要查找角色,请在您的账户中选择包含“kendra”一词的角色,或者输入其他角色的名称。有关该角色所需权限的更多信息,请参阅索引的 IAM 角色

  5. 选择下一步

  6. 配置用户访问权限控制页面上选择下一步。创建索引后,您可以更新索引以使用令牌进行访问权限控制。有关更多信息,请参阅控制对文档的访问权限

  7. 预配置详细信息页面上选择创建

  8. 索引可能需要一些时间才能创建完成。查看索引列表以了解索引创建进度。当索引的状态为 ACTIVE 时,说明您的索引就已经准备就绪。

AWS CLI
创建索引
  1. 使用以下命令创建索引。role-arn 必须是可运行 Amazon Kendra 操作的 IAM 角色的 Amazon 资源名称(ARN)。有关更多信息,请参阅 IAM 角色

    该命令针对 Linux 和 macOS 编排了格式。如果您使用 Windows,请将 Unix 行继续符(\)替换为脱字号(^)。

    aws kendra create-index \ --name index name \ --description "index description" \ --role-arn arn:aws:iam::account ID:role/role name
  2. 索引可能需要一些时间才能创建完成。要检查索引的状态,请在以下命令中使用 create-index 返回的索引 ID。当索引的状态为 ACTIVE 时,说明您的索引就已经准备就绪。

    aws kendra describe-index \ --index-id index ID
Python
创建索引
  • ‌为以下代码示例中的变量提供值:

    • description - 正在创建的索引的描述。该项为可选项。

    • index_name - 正在创建的索引的名称。

    • role_arn - 可运行 Amazon Kendra API 的角色的 Amazon 资源名称(ARN)。有关更多信息,请参阅 IAM 角色

    import boto3 from botocore.exceptions import ClientError import pprint import time kendra = boto3.client("kendra") print("Create an index.") # Provide a name for the index index_name = "index-name" # Provide an optional description for the index description = "index description" # Provide the IAM role ARN required for indexes role_arn = "arn:aws:iam::${account id}:role/${role name}" try: index_response = kendra.create_index( Name = index_name, Description = description, RoleArn = role_arn ) pprint.pprint(index_response) index_id = index_response["Id"] print("Wait for Amazon Kendra to create the index.") while True: # Get the details of the index, such as the status index_description = kendra.describe_index( Id = index_id ) # If status is not CREATING, then quit status = index_description["Status"] print(" Creating index. Status: "+status) if status != "CREATING": break time.sleep(60) except ClientError as e: print("%s" % e) print("Program ends.")
Java
创建索引
  • ‌为以下代码示例中的变量提供值:

    • description - 正在创建的索引的描述。该项为可选项。

    • index_name - 正在创建的索引的名称。

    • role_arn - 可运行 Amazon Kendra API 的角色的 Amazon 资源名称(ARN)。有关更多信息,请参阅 IAM 角色

    package com.amazonaws.kendra; import java.util.concurrent.TimeUnit; import software.amazon.awssdk.services.kendra.KendraClient; import software.amazon.awssdk.services.kendra.model.CreateIndexRequest; import software.amazon.awssdk.services.kendra.model.CreateIndexResponse; import software.amazon.awssdk.services.kendra.model.DescribeIndexRequest; import software.amazon.awssdk.services.kendra.model.DescribeIndexResponse; import software.amazon.awssdk.services.kendra.model.IndexStatus; public class CreateIndexExample { public static void main(String[] args) throws InterruptedException { String indexDescription = "Getting started index for Kendra"; String indexName = "java-getting-started-index"; String indexRoleArn = "arn:aws:iam::<your AWS account ID>:role/KendraRoleForGettingStartedIndex"; System.out.println(String.format("Creating an index named %s", indexName)); CreateIndexRequest createIndexRequest = CreateIndexRequest .builder() .description(indexDescription) .name(indexName) .roleArn(indexRoleArn) .build(); KendraClient kendra = KendraClient.builder().build(); CreateIndexResponse createIndexResponse = kendra.createIndex(createIndexRequest); System.out.println(String.format("Index response %s", createIndexResponse)); String indexId = createIndexResponse.id(); System.out.println(String.format("Waiting until the index with ID %s is created.", indexId)); while (true) { DescribeIndexRequest describeIndexRequest = DescribeIndexRequest.builder().id(indexId).build(); DescribeIndexResponse describeIndexResponse = kendra.describeIndex(describeIndexRequest); IndexStatus status = describeIndexResponse.status(); if (status != IndexStatus.CREATING) { break; } TimeUnit.SECONDS.sleep(60); } System.out.println("Index creation is complete."); } }

创建索引后,您可以向其中添加文档。您可以直接添加,也可以创建定期更新索引的数据来源。