將索引典新增至索引 - Amazon Kendra

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

將索引典新增至索引

下列程序顯示如何將包含同義字的索引典檔案新增至索引。您最多可能需要 30 分鐘才能看到更新的索引典檔案的效果。如需索引典檔案的詳細資訊,請參閱。建立同義字辭典檔案

Console
若要新增索引典
  1. 在左側導覽窗格中,在您要新增同義字清單的索引下,選擇同義詞字典。

  2. 在「同義字」頁面上,選擇「新增同義字辭典」。

  3. 定義索引典中,為您的索引典指定名稱和選擇性說明。

  4. 索引典設定中,提供索引典檔案的 Amazon S3 路徑。檔案必須小於 5 MB。

  5. 對於 IAM 角色,請選取角色或選取建立新角色並指定角色名稱以建立新角色。 Amazon Kendra 使用此角色代表您存取 Amazon S3 資源。IAM 角色的前置詞為「AmazonKendra-」。

  6. 選擇 [存] 以儲存組態並新增索引典。一旦擷取同義字辭典,它就是作用中的,且同義字會在結果中反白顯示。查看索引典檔案的效果最多可能需要 30 分鐘。

CLI

若要使用新增索引至索引 AWS CLI,請呼叫:create-thesaurus

aws kendra create-thesaurus \ --index-id index-id \ --name "thesaurus-name" \ --description "thesaurus-description" \ --source-s3-path "Bucket=bucket-name,Key=thesaurus/synonyms.txt" \ --role-arn role-arn

呼叫list-thesauri以查看索引典清單:

aws kendra list-thesauri \ --index-id index-id

若要檢視索引典的詳細資料,請呼叫:describe-thesaurus

aws kendra describe-thesaurus \ --index-id index-id \ --index-id thesaurus-id

查看索引典檔案的效果最多可能需要 30 分鐘。

Python
import boto3 from botocore.exceptions import ClientError import pprint import time kendra = boto3.client("kendra") print("Create a thesaurus") thesaurus_name = "thesaurus-name" thesaurus_description = "thesaurus-description" thesaurus_role_arn = "role-arn" index_id = "index-id" s3_bucket_name = "bucket-name" s3_key = "thesaurus-file" source_s3_path= { 'Bucket': s3_bucket_name, 'Key': s3_key } try: thesaurus_response = kendra.create_thesaurus( Description = thesaurus_description, Name = thesaurus_name, RoleArn = thesaurus_role_arn, IndexId = index_id, SourceS3Path = source_s3_path ) pprint.pprint(thesaurus_response) thesaurus_id = thesaurus_response["Id"] print("Wait for Kendra to create the thesaurus.") while True: # Get thesaurus description thesaurus_description = kendra.describe_thesaurus( Id = thesaurus_id, IndexId = index_id ) # If status is not CREATING quit status = thesaurus_description["Status"] print("Creating thesaurus. Status: " + status) if status != "CREATING": break time.sleep(60) except ClientError as e: print("%s" % e) print("Program ends.")
Java
package com.amazonaws.kendra; import software.amazon.awssdk.services.kendra.KendraClient; import software.amazon.awssdk.services.kendra.model.CreateThesaurusRequest; import software.amazon.awssdk.services.kendra.model.CreateThesaurusResponse; import software.amazon.awssdk.services.kendra.model.DescribeThesaurusRequest; import software.amazon.awssdk.services.kendra.model.DescribeThesaurusResponse; import software.amazon.awssdk.services.kendra.model.S3Path; import software.amazon.awssdk.services.kendra.model.ThesaurusStatus; public class CreateThesaurusExample { public static void main(String[] args) throws InterruptedException { KendraClient kendra = KendraClient.builder().build(); String thesaurusName = "thesaurus-name"; String thesaurusDescription = "thesaurus-description"; String thesaurusRoleArn = "role-arn"; String s3BucketName = "bucket-name"; String s3Key = "thesaurus-file"; String indexId = "index-id"; System.out.println(String.format("Creating a thesaurus named %s", thesaurusName)); CreateThesaurusRequest createThesaurusRequest = CreateThesaurusRequest .builder() .name(thesaurusName) .indexId(indexId) .description(thesaurusDescription) .roleArn(thesaurusRoleArn) .sourceS3Path(S3Path.builder() .bucket(s3BucketName) .key(s3Key) .build()) .build(); CreateThesaurusResponse createThesaurusResponse = kendra.createThesaurus(createThesaurusRequest); System.out.println(String.format("Thesaurus response %s", createThesaurusResponse)); String thesaurusId = createThesaurusResponse.id(); System.out.println(String.format("Waiting until the thesaurus with ID %s is created.", thesaurusId)); while (true) { DescribeThesaurusRequest describeThesaurusRequest = DescribeThesaurusRequest.builder() .id(thesaurusId) .indexId(indexId) .build(); DescribeThesaurusResponse describeThesaurusResponse = kendra.describeThesaurus(describeThesaurusRequest); ThesaurusStatus status = describeThesaurusResponse.status(); if (status != ThesaurusStatus.CREATING) { break; } TimeUnit.SECONDS.sleep(60); } System.out.println("Thesaurus creation is complete."); } }