AWS CLI を使用した Amazon Kendra の例 - AWS Command Line Interface

AWS CLI を使用した Amazon Kendra の例

次のコード例は、Amazon Kendra で AWS Command Line Interface を使用してアクションを実行し、一般的なシナリオを実装する方法を示しています。

アクションはより大きなプログラムからのコードの抜粋であり、コンテキスト内で実行する必要があります。アクションは個々のサービス機能を呼び出す方法を示していますが、コンテキスト内のアクションは、関連するシナリオで確認できます。

各例には、完全なソースコードへのリンクが含まれており、そこからコンテキストにおけるコードの設定方法と実行方法についての手順を確認できます。

トピック

アクション

次の例は、create-data-source を使用する方法を説明しています。

AWS CLI

Amazon Kendra データソースコネクタを作成するには

次の create-data-source は、Amazon Kendra データソースコネクタを作成して設定します。describe-data-source を使用してデータソースコネクタのステータスを表示し、ステータスが、データソースコネクタが完全に作成されなかったことを示す "FAILED" の場合は、エラーメッセージを確認することができます。

aws kendra create-data-source \ --name "example data source 1" \ --description "Example data source 1 for example index 1 contains the first set of example documents" \ --tags '{"Key": "test resources", "Value": "kendra"}, {"Key": "test resources", "Value": "aws"}' \ --role-arn "arn:aws:iam::my-account-id:role/KendraRoleForS3TemplateConfigDataSource" \ --index-id exampleindex1 \ --language-code "es" \ --schedule "0 0 18 ? * TUE,MON,WED,THU,FRI,SAT *" \ --configuration '{"TemplateConfiguration": {"Template": file://s3schemaconfig.json}}' \ --type "TEMPLATE" \ --custom-document-enrichment-configuration '{"PostExtractionHookConfiguration": {"LambdaArn": "arn:aws:iam::my-account-id:function/my-function-ocr-docs", "S3Bucket": "s3://my-s3-bucket/scanned-image-text-example-docs"}, "RoleArn": "arn:aws:iam:my-account-id:role/KendraRoleForCDE"}' \ --vpc-configuration '{"SecurityGroupIds": ["sg-1234567890abcdef0"], "SubnetIds": ["subnet-1c234","subnet-2b134"]}'

出力:

{ "Id": "exampledatasource1" }

詳細については、「Amazon Kendra Developer Guide」の「Getting started with an Amazon Kendra index and data source connector」を参照してください。

  • API の詳細については、「AWS CLI コマンドリファレンス」の「CreateDataSource」を参照してください。

次の例は、create-index を使用する方法を説明しています。

AWS CLI

Amazon Kendra インデックスを作成するには

次の create-index は、Amazon Kendra インデックスを作成して設定します。describe-index を使用してインデックスのステータスを表示し、ステータスが、インデックスが完全に作成されなかったことを示す "FAILED" の場合は、エラーメッセージを確認することができます。

aws kendra create-index \ --name "example index 1" \ --description "Example index 1 contains the first set of example documents" \ --tags '{"Key": "test resources", "Value": "kendra"}, {"Key": "test resources", "Value": "aws"}' \ --role-arn "arn:aws:iam::my-account-id:role/KendraRoleForExampleIndex" \ --edition "DEVELOPER_EDITION" \ --server-side-encryption-configuration '{"KmsKeyId": "my-kms-key-id"}' \ --user-context-policy "USER_TOKEN" \ --user-token-configurations '{"JsonTokenTypeConfiguration": {"GroupAttributeField": "groupNameField", "UserNameAttributeField": "userNameField"}}'

出力:

{ "Id": index1 }

詳細については、「Amazon Kendra Developer Guide」の「Getting started with an Amazon Kendra index and data source connector」を参照してください。

  • API の詳細については、「AWS CLI コマンドリファレンス」の「CreateIndex」を参照してください。

次のコード例は、describe-data-source を使用する方法を示しています。

AWS CLI

Amazon Kendra データソースコネクタに関する情報を取得するには

次の describe-data-source は、Amazon Kendra データソースコネクタに関する情報を取得します。データソースコネクタの設定を表示し、ステータスが、データソースコネクタが完全に作成されなかったことを示す "FAILED" の場合は、エラーメッセージを確認することができます。

aws kendra describe-data-source \ --id exampledatasource1 \ --index-id exampleindex1

出力:

{ "Configuration": { "TemplateConfiguration": { "Template": { "connectionConfiguration": { "repositoryEndpointMetadata": { "BucketName": "my-bucket" } }, "repositoryConfigurations": { "document":{ "fieldMappings": [ { "indexFieldName":"_document_title", "indexFieldType":"STRING", "dataSourceFieldName": "title" }, { "indexFieldName":"_last_updated_at", "indexFieldType":"DATE", "dataSourceFieldName": "modified_date" } ] } }, "additionalProperties": { "inclusionPatterns": [ "*.txt", "*.doc", "*.docx" ], "exclusionPatterns": [ "*.json" ], "inclusionPrefixes": [ "PublicExampleDocsFolder" ], "exclusionPrefixes": [ "PrivateDocsFolder/private" ], "aclConfigurationFilePath": "ExampleDocsFolder/AclConfig.json", "metadataFilesPrefix": "metadata" }, "syncMode": "FULL_CRAWL", "type" : "S3", "version": "1.0.0" } } }, "CreatedAt": 2024-02-25T13:30:10+00:00, "CustomDocumentEnrichmentConfiguration": { "PostExtractionHookConfiguration": { "LambdaArn": "arn:aws:iam::my-account-id:function/my-function-ocr-docs", "S3Bucket": "s3://my-s3-bucket/scanned-image-text-example-docs/function" }, "RoleArn": "arn:aws:iam:my-account-id:role/KendraRoleForCDE" } "Description": "Example data source 1 for example index 1 contains the first set of example documents", "Id": exampledatasource1, "IndexId": exampleindex1, "LanguageCode": "en", "Name": "example data source 1", "RoleArn": "arn:aws:iam::my-account-id:role/KendraRoleForS3TemplateConfigDataSource", "Schedule": "0 0 18 ? * TUE,MON,WED,THU,FRI,SAT *", "Status": "ACTIVE", "Type": "TEMPLATE", "UpdatedAt": 1709163615, "VpcConfiguration": { "SecurityGroupIds": ["sg-1234567890abcdef0"], "SubnetIds": ["subnet-1c234","subnet-2b134"] } }

詳細については、「Amazon Kendra Developer Guide」の「Getting started with an Amazon Kendra index and data source connector」を参照してください。

  • API の詳細については、「AWS CLI コマンドリファレンス」の「DescribeDataSource」を参照してください。

次のコード例は、describe-index を使用する方法を示しています。

AWS CLI

Amazon Kendra インデックスに関する情報を取得するには

次の describe-index は、Amazon Kendra インデックスに関する情報を取得します。インデックスの設定を表示し、ステータスが、インデックスが完全に作成されなかったことを示す "FAILED" の場合は、エラーメッセージを確認することができます。

aws kendra describe-index \ --id exampleindex1

出力:

{ "CapacityUnits": { "QueryCapacityUnits": 0, "StorageCapacityUnits": 0 }, "CreatedAt": 2024-02-25T12:30:10+00:00, "Description": "Example index 1 contains the first set of example documents", "DocumentMetadataConfigurations": [ { "Name": "_document_title", "Relevance": { "Importance": 8 }, "Search": { "Displayable": true, "Facetable": false, "Searchable": true, "Sortable": false }, "Type": "STRING_VALUE" }, { "Name": "_document_body", "Relevance": { "Importance": 5 }, "Search": { "Displayable": true, "Facetable": false, "Searchable": true, "Sortable": false }, "Type": "STRING_VALUE" }, { "Name": "_last_updated_at", "Relevance": { "Importance": 6, "Duration": "2628000s", "Freshness": true }, "Search": { "Displayable": true, "Facetable": false, "Searchable": true, "Sortable": true }, "Type": "DATE_VALUE" }, { "Name": "department_custom_field", "Relevance": { "Importance": 7, "ValueImportanceMap": { "Human Resources" : 4, "Marketing and Sales" : 2, "Research and innvoation" : 3, "Admin" : 1 } }, "Search": { "Displayable": true, "Facetable": true, "Searchable": true, "Sortable": true }, "Type": "STRING_VALUE" } ], "Edition": "DEVELOPER_EDITION", "Id": "index1", "IndexStatistics": { "FaqStatistics": { "IndexedQuestionAnswersCount": 10 }, "TextDocumentStatistics": { "IndexedTextBytes": 1073741824, "IndexedTextDocumentsCount": 1200 } }, "Name": "example index 1", "RoleArn": "arn:aws:iam::my-account-id:role/KendraRoleForExampleIndex", "ServerSideEncryptionConfiguration": { "KmsKeyId": "my-kms-key-id" }, "Status": "ACTIVE", "UpdatedAt": 1709163615, "UserContextPolicy": "USER_TOKEN", "UserTokenConfigurations": [ { "JsonTokenTypeConfiguration": { "GroupAttributeField": "groupNameField", "UserNameAttributeField": "userNameField" } } ] }

詳細については、「Amazon Kendra Developer Guide」の「Getting started with an Amazon Kendra index and data source connector」を参照してください。

  • API の詳細については、「AWS CLI コマンドリファレンス」の「DescribeIndex」を参照してください。

次の例は、update-data-source を使用する方法を説明しています。

AWS CLI

Amazon Kendra データソースコネクタを更新するには

次の update-data-source は、Amazon Kendra データソースコネクタの設定を更新します。アクションが成功すると、出力は返されないか、HTTP ステータスコード 200 または AWS CLI リターンコード 0 が返されます。describe-data-source を使用して、データソースコネクタの設定とステータスを表示できます。

aws kendra update-data-source \ --id exampledatasource1 \ --index-id exampleindex1 \ --name "new name for example data source 1" \ --description "new description for example data source 1" \ --role-arn arn:aws:iam::my-account-id:role/KendraNewRoleForExampleDataSource \ --configuration '{"TemplateConfiguration": {"Template": file://s3schemanewconfig.json}}' \ --custom-document-enrichment-configuration '{"PostExtractionHookConfiguration": {"LambdaArn": "arn:aws:iam::my-account-id:function/my-function-ocr-docs", "S3Bucket": "s3://my-s3-bucket/scanned-image-text-example-docs"}, "RoleArn": "arn:aws:iam:my-account-id:role/KendraNewRoleForCDE"}' \ --language-code "es" \ --schedule "0 0 18 ? * MON,WED,FRI *" \ --vpc-configuration '{"SecurityGroupIds": ["sg-1234567890abcdef0"], "SubnetIds": ["subnet-1c234","subnet-2b134"]}'

このコマンドでは何も出力されません。

詳細については、「Amazon Kendra Developer Guide」の「Getting started with an Amazon Kendra index and data source connector」を参照してください。

  • API の詳細については、「AWS CLI コマンドリファレンス」の「UpdateDataSource」を参照してください。

次の例は、update-index を使用する方法を説明しています。

AWS CLI

Amazon Kendra インデックスを更新するには

次の update-index は、Amazon Kendra インデックスの設定を更新します。アクションが成功すると、出力は返されないか、HTTP ステータスコード 200 または AWS CLI リターンコード 0 が返されます。describe-index を使用して、インデックスの設定とステータスを表示できます。

aws kendra update-index \ --id enterpriseindex1 \ --name "new name for Enterprise Edition index 1" \ --description "new description for Enterprise Edition index 1" \ --role-arn arn:aws:iam::my-account-id:role/KendraNewRoleForEnterpriseIndex \ --capacity-units '{"QueryCapacityUnits": 2, "StorageCapacityUnits": 1}' \ --document-metadata-configuration-updates '{"Name": "_document_title", "Relevance": {"Importance": 6}}, {"Name": "_last_updated_at", "Relevance": {"Importance": 8}}' \ --user-context-policy "USER_TOKEN" \ --user-token-configurations '{"JsonTokenTypeConfiguration": {"GroupAttributeField": "groupNameField", "UserNameAttributeField": "userNameField"}}'

このコマンドでは何も出力されません。

詳細については、「Amazon Kendra Developer Guide」の「Getting started with an Amazon Kendra index and data source connector」を参照してください。

  • API の詳細については、「AWS CLI コマンドリファレンス」の「UpdateIndex」を参照してください。