通过批量上传将文档直接添加到索引中 - Amazon Kendra

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

通过批量上传将文档直接添加到索引中

您可以使用将文档直接添加到索引中BatchPutDocumentAPI。您无法使用控制台直接添加文档。如果使用控制台,则可以连接到数据来源,以便向索引中添加文档。您可以从 S3 存储桶添加文档,也可以将文档作为二进制数据提供。有关支持的文档类型的列表, Amazon Kendra 请参阅文档类型

使用 BatchPutDocument 将文档添加到索引中是一种异步操作。调用后 BatchPutDocumentAPI,您可以使用来监视BatchGetDocumentStatusAPI为文档编制索引的进度。当您使用文档列表调BatchGetDocumentStatusAPI用时IDs,它会返回文档的状态。当文档状态为 INDEXEDFAILED 时,表明文档处理已完成。当状态为时FAILEDBatchGetDocumentStatusAPI返回无法为文档编制索引的原因。

如果您想在文档提取过程中更改内容和文档元数据字段或属性,请参阅 Amazon Kendra 自定义文档富集。如果要使用自定义数据源,则使用提交的每个文档都BatchPutDocumentAPI需要数据源 ID 和执行 ID 作为属性或字段。有关更多信息,请参阅自定义数据来源的必需属性

注意

每个索引的每个文档 ID 必须是唯一的。您不能创建数据源来索引具有唯一性的文档,IDs然后使用BatchPutDocumentAPI为相同的文档编制索引,反之亦然。您可以删除数据源,然后使用索BatchPutDocumentAPI引相同的文档,反之亦然。将BatchPutDocumentBatchDeleteDocumentAPIs与 Amazon Kendra 数据源连接器结合使用同一组文档可能会导致数据不一致。我们建议使用 Amazon Kendra 自定义数据来源连接器

以下开发人员指南文档展示了如何将文档直接添加到索引中。

使用添加文档 BatchPutDocument API

以下示例通过调用BatchPutDocument将文本块添加到索引中。您可以使用将文档直接BatchPutDocumentAPI添加到索引中。有关支持的文档类型的列表, Amazon Kendra 请参阅文档类型

有关使用 AWS CLI 和创建索引的示例SDKs,请参阅创建索引。要设置CLI和SDKs,请参阅设置 Amazon Kendra

注意

添加到索引的文件必须采用 UTF -8 编码的字节流。

在以下示例中,UTF-8 编码的文本被添加到索引中。

CLI

在中 AWS Command Line Interface,使用以下命令。该命令针对 Linux 和 macOS 编排了格式。如果您使用 Windows,请将 Unix 行继续符(\)替换为脱字号(^)。

aws kendra batch-put-document \ --index-id index-id \ --documents '{"Id":"doc-id-1", "Blob":"Amazon.com is an online retailer.", "ContentType":"PLAIN_TEXT", "Title":"Information about Amazon.com"}'
Python
import boto3 kendra = boto3.client("kendra") # Provide the index ID index_id = "index-id" # Provide the title and text title = "Information about Amazon.com" text = "Amazon.com is an online retailer." document = { "Id": "1", "Blob": text, "ContentType": "PLAIN_TEXT", "Title": title } documents = [ document ] result = kendra.batch_put_document( IndexId = index_id, Documents = documents ) print(result)
Java
package com.amazonaws.kendra; import software.amazon.awssdk.core.SdkBytes; import software.amazon.awssdk.services.kendra.KendraClient; import software.amazon.awssdk.services.kendra.model.BatchPutDocumentRequest; import software.amazon.awssdk.services.kendra.model.BatchPutDocumentResponse; import software.amazon.awssdk.services.kendra.model.ContentType; import software.amazon.awssdk.services.kendra.model.Document; public class AddDocumentsViaAPIExample { public static void main(String[] args) { KendraClient kendra = KendraClient.builder().build(); String indexId = "yourIndexId"; Document testDoc = Document .builder() .title("The title of your document") .id("a_doc_id") .blob(SdkBytes.fromUtf8String("your text content")) .contentType(ContentType.PLAIN_TEXT) .build(); BatchPutDocumentRequest batchPutDocumentRequest = BatchPutDocumentRequest .builder() .indexId(indexId) .documents(testDoc) .build(); BatchPutDocumentResponse result = kendra.batchPutDocument(batchPutDocumentRequest); System.out.println(String.format("BatchPutDocument Result: %s", result)); } }

从 S3 存储桶添加文档

您可以使用将 Amazon S3 存储桶中的文档直接添加到索引中BatchPutDocumentAPI。在同一次调用中最多可以添加 10 个文档。使用 S3 存储桶时,必须为 IAM 角色提供访问包含您的文档的存储桶的权限。该角色在 RoleArn 参数中指定。

使用BatchPutDocumentAPI从 Amazon S3 存储桶中添加文档是一次性操作。要使索引与存储桶的内容保持同步,请创建 Amazon S3 数据源。有关更多信息,请参阅 Amazon S3 数据来源

有关使用 AWS CLI 和创建索引的示例SDKs,请参阅创建索引。要设置CLI和SDKs,请参阅设置 Amazon Kendra。有关创建 S3 存储桶的信息,请参阅 Amazon Simple Storage Service 文档

在以下示例中,使用将两个 Microsoft Word 文档添加到索引中BatchPutDocumentAPI。

Python
import boto3 kendra = boto3.client("kendra") # Provide the index ID index_id = "index-id" # Provide the IAM role ARN required to index documents in an S3 bucket role_arn = "arn:aws:iam::${acccountID}:policy/${roleName}" doc1_s3_file_data = { "Bucket": "bucket-name", "Key": "document1.docx" } doc1_document = { "S3Path": doc1_s3_file_data, "Title": "Document 1 title", "Id": "doc_1" } doc2_s3_file_data = { "Bucket": "bucket-name", "Key": "document2.docx" } doc2_document = { "S3Path": doc2_s3_file_data, "Title": "Document 2 title", "Id": "doc_2" } documents = [ doc1_document, doc2_document ] result = kendra.batch_put_document( Documents = documents, IndexId = index_id, RoleArn = role_arn ) print(result)
Java
package com.amazonaws.kendra; import software.amazon.awssdk.services.kendra.KendraClient; import software.amazon.awssdk.services.kendra.model.BatchPutDocumentRequest; import software.amazon.awssdk.services.kendra.model.BatchPutDocumentResponse; import software.amazon.awssdk.services.kendra.model.Document; import software.amazon.awssdk.services.kendra.model.S3Path; public class AddFilesFromS3Example { public static void main(String[] args) { KendraClient kendra = KendraClient.builder().build(); String indexId = "yourIndexId"; String roleArn = "yourIndexRoleArn"; Document pollyDoc = Document .builder() .s3Path( S3Path.builder() .bucket("an-aws-kendra-amzn-s3-demo-bucket") .key("What is Amazon Polly.docx") .build()) .title("What is Amazon Polly") .id("polly_doc_1") .build(); Document rekognitionDoc = Document .builder() .s3Path( S3Path.builder() .bucket("an-aws-kendra-amzn-s3-demo-bucket") .key("What is Amazon Rekognition.docx") .build()) .title("What is Amazon rekognition") .id("rekognition_doc_1") .build(); BatchPutDocumentRequest batchPutDocumentRequest = BatchPutDocumentRequest .builder() .indexId(indexId) .roleArn(roleArn) .documents(pollyDoc, rekognitionDoc) .build(); BatchPutDocumentResponse result = kendra.batchPutDocument(batchPutDocumentRequest); System.out.println(String.format("BatchPutDocument result: %s", result)); } }