

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 创建 索引
<a name="create-index"></a>

您可以使用控制台或通过调用 [CreateIndex](https://docs.aws.amazon.com/kendra/latest/APIReference/API_CreateIndex.html)API 来创建索引。您可以将 AWS Command Line Interface (AWS CLI) 或 SDK 与 API 配合使用。创建索引后，您可以直接向索引中添加文档，也可以从数据来源添加文档。

要创建索引，您必须提供 () 角色的 Amazon 资源名称 AWS Identity and Access Management (ARN IAM) 以供索引访问。 CloudWatch有关更多信息，请参阅[索引的IAM 角色](https://docs.aws.amazon.com/kendra/latest/dg/iam-roles.html#iam-roles-index)。

以下选项卡提供了使用创建索引的过程，以及使用 AWS 管理控制台、Python 和 Java 的 AWS CLI代码示例 SDKs。

------
#### [ Console ]

**创建索引**

1. 登录 AWS 管理控制台并打开控制 Amazon Kendra 台，网址为[https://console.aws.amazon.com/kendra/](https://console.aws.amazon.com/kendra/)。

1. 在**索引**部分选择**创建索引**。

1. 在**指定索引详细信息**中，指定索引都名称和描述。

1. 在**IAM 角色**中提供一个 IAM 角色。要查找角色，请在您的账户中选择包含“kendra”一词的角色，或者输入其他角色的名称。有关该角色所需权限的更多信息，请参阅[索引的IAM 角色](https://docs.aws.amazon.com/kendra/latest/dg/iam-roles.html#iam-roles-index)。

1. 选择**下一步**。

1. 在**配置用户访问权限控制**页面上选择**下一步**。创建索引后，您可以更新索引以使用令牌进行访问权限控制。有关更多信息，请参阅[控制对文档的访问权限](https://docs.aws.amazon.com/kendra/latest/dg/create-index-access-control.html)。

1. 在**预配置详细信息**页面上选择**创建**。

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

------
#### [ AWS CLI ]

**创建索引**

1. 使用以下命令创建索引。`role-arn`必须是可以运行 Amazon Kendra 操作的 IAM 角色的 Amazon 资源名称 (ARN)。有关更多信息，请参阅 [IAM 角色](https://docs.aws.amazon.com/kendra/latest/dg/iam-roles.html)。

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

   ```
   aws kendra create-index \
    --name {{index name}} \
    --description "{{index description}}" \
    --role-arn arn:aws:iam::{{account ID}}:role/{{role name}}
   ```

1. 索引可能需要一些时间才能创建完成。要检查索引的状态，请在以下命令中使用 `create-index` 返回的索引 ID。当索引的状态为 `ACTIVE` 时，说明您的索引就已经准备就绪。

   ```
   aws kendra describe-index \
    --index-id {{index ID}}
   ```

------
#### [ Python ]

**创建索引**
+ ‌为以下代码示例中的变量提供值：
  + `description` - 正在创建的索引的描述。这是可选的。
  + `index_name` - 正在创建的索引的名称。
  + `role_arn`— 可以运行的角色的亚马逊资源名称 (ARN)。 Amazon Kendra APIs有关更多信息，请参阅 [IAM 角色](https://docs.aws.amazon.com/kendra/latest/dg/iam-roles.html)。

  ```
  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`— 可以运行的角色的亚马逊资源名称 (ARN)。 Amazon Kendra APIs有关更多信息，请参阅 [IAM 角色](https://docs.aws.amazon.com/kendra/latest/dg/iam-roles.html)。

  ```
  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.");
      }
  }
  ```

------

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

**Topics**
+ [通过批量上传将文档直接添加到索引中](in-adding-documents.md)
+ [向索引中添加常见问题 (FAQs)](in-creating-faq.md)
+ [创建自定义文档字段](custom-attributes.md)
+ [使用令牌控制用户访问文档](create-index-access-control.md)