

文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 [AWS SDK 範例](https://github.com/awsdocs/aws-doc-sdk-examples)。

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

# 使用 的 Amazon Kendra 範例 AWS CLI
<a name="cli_2_kendra_code_examples"></a>

下列程式碼範例示範如何使用 AWS Command Line Interface 搭配 Amazon Kendra 執行動作和實作常見案例。

*Actions* 是大型程式的程式碼摘錄，必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數，但您可以在其相關情境中查看內容中的動作。

每個範例均包含完整原始碼的連結，您可在連結中找到如何設定和執行內容中程式碼的相關指示。

**Topics**
+ [動作](#actions)

## 動作
<a name="actions"></a>

### `create-data-source`
<a name="kendra_CreateDataSource_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `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://amzn-s3-demo-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 開發人員指南》**中的 [Amazon Kendra 索引和資料來源連接器入門](https://docs.aws.amazon.com/kendra/latest/dg/getting-started.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [CreateDataSource](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/kendra/create-data-source.html)。

### `create-index`
<a name="kendra_CreateIndex_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `create-index`。

**AWS CLI**  
**建立 Amazon Kendra 索引**  
以下 `create-index` 會建立和設定 Amazon Kendra 索引。您可以使用 `describe-index` 檢視索引的狀態，如果狀態顯示要完整建立的索引「失敗」，則讀取任何錯誤訊息。  

```
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 開發人員指南》**中的 [Amazon Kendra 索引和資料來源連接器入門](https://docs.aws.amazon.com/kendra/latest/dg/getting-started.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [CreateIndex](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/kendra/create-index.html)。

### `describe-data-source`
<a name="kendra_DescribeDataSource_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `describe-data-source`。

**AWS CLI**  
**取得 Amazon Kendra 資料來源連接器的相關資訊**  
以下內容`describe-data-source`會取得 Amazon Kendra 資料來源連接器的相關資訊。您可以檢視資料來源連接器的組態，如果狀態顯示要完整建立的資料來源連接器「失敗」，您可以讀取任何錯誤訊息。  

```
aws kendra describe-data-source \
    --id exampledatasource1 \
    --index-id exampleindex1
```
輸出：  

```
{
    "Configuration": {
        "TemplateConfiguration": {
            "Template": {
                "connectionConfiguration": {
                    "repositoryEndpointMetadata": {
                        "BucketName": "amzn-s3-demo-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://amzn-s3-demo-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 開發人員指南》**中的 [Amazon Kendra 索引和資料來源連接器入門](https://docs.aws.amazon.com/kendra/latest/dg/getting-started.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [DescribeDataSource](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/kendra/describe-data-source.html)。

### `describe-index`
<a name="kendra_DescribeIndex_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `describe-index`。

**AWS CLI**  
**取得 Amazon Kendra 索引的相關資訊**  
以下 `describe-index` 會取得 Amazon Kendra 索引的相關資訊。您可以檢視索引的組態，如果狀態顯示要完整建立的索引「失敗」，則讀取任何錯誤訊息。  

```
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 開發人員指南》**中的 [Amazon Kendra 索引和資料來源連接器入門](https://docs.aws.amazon.com/kendra/latest/dg/getting-started.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [DescribeIndex](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/kendra/describe-index.html)。

### `update-data-source`
<a name="kendra_UpdateDataSource_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `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://amzn-s3-demo-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 開發人員指南》**中的 [Amazon Kendra 索引和資料來源連接器入門](https://docs.aws.amazon.com/kendra/latest/dg/getting-started.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [UpdateDataSource](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/kendra/update-data-source.html)。

### `update-index`
<a name="kendra_UpdateIndex_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `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 開發人員指南》**中的 [Amazon Kendra 索引和資料來源連接器入門](https://docs.aws.amazon.com/kendra/latest/dg/getting-started.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [UpdateIndex](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/kendra/update-index.html)。