

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

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

您可以使用 主控台或呼叫 [CreateIndex](https://docs.aws.amazon.com/kendra/latest/APIReference/API_CreateIndex.html) API 來建立索引。您可以搭配 API 使用 AWS Command Line Interface (AWS CLI) 或 SDK。建立索引之後，您可以直接新增文件或從資料來源新增文件。

若要建立索引，您必須提供 () 角色的 Amazon Resource Name AWS Identity and Access Management (ARN IAM)，索引才能存取 CloudWatch。如需詳細資訊，請參閱[IAM 索引的角色](https://docs.aws.amazon.com/kendra/latest/dg/iam-roles.html#iam-roles-index)。

下列索引標籤提供使用 建立索引的程序 AWS 管理主控台，以及使用 的程式碼範例 AWS CLI，以及 Python 和 Java SDKs。

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

**建立 索引**

1. 登入 AWS 管理主控台，並在 https：//[https://console.aws.amazon.com/kendra/](https://console.aws.amazon.com/kendra/) 開啟 Amazon 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 Resource Name (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. 可能需要一些時間才能建立索引。若要檢查索引的狀態，請使用 傳回的索引 ID `create-index`搭配下列命令。當索引的狀態為 時`ACTIVE`，您的索引即可使用。

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

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

**建立 索引**
+ 在下列程式碼範例中提供下列變數的值：
  + `description`- 您正在建立之索引的描述。這是選用的。
  + `index_name`- 您正在建立的索引名稱。
  + `role_arn`- 可執行 Amazon Kendra APIs之角色的 Amazon Resource Name (ARN)。如需詳細資訊，請參閱 [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`- 可執行 Amazon Kendra APIs之角色的 Amazon Resource Name (ARN)。如需詳細資訊，請參閱 [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)