

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

# 检索段落
<a name="searching-retrieve"></a>

您可以将 [https://docs.aws.amazon.com/kendra/latest/APIReference/API_Retrieve.html](https://docs.aws.amazon.com/kendra/latest/APIReference/API_Retrieve.html) API 用作检索增强生成（RAG）系统的检索器。

RAG 系统使用生成式人工智能来构建问答应用程序。RAG 系统由检索器和大型语言模型（LLM）组成。给定一个查询，检索器会从文档语料库中识别出最相关的文本块，并将其提供给 LLM 以提供最有用的答案。然后，LLM 会分析相关的文本块或段落，并为查询生成全面的响应。

`Retrieve` API 会查看被称为*段落*的文本块或摘录，并返回与查询最相关的前述段落。

与 [https://docs.aws.amazon.com/kendra/latest/APIReference/API_Query.html](https://docs.aws.amazon.com/kendra/latest/APIReference/API_Query.html) API 一样，`Retrieve` API 也搜索相关信息。Retrieve API 的信息检索会考虑查询的上下文以及已编制索引的文档中的所有可用信息。但是，默认情况下，`Query` API 仅返回最多 100 个标记词的摘录段落。使用 `Retrieve` API，您可以检索最多 200 个标记词和多达 100 个语义相关段落的较长段落。这不包括索引中的问题答案或常见问题解答类型的回复。这些段落（也称为文本块）是可以从多个文档或同一文档的不同部分中通过语义分析提取的文本摘录。Kendra 的 GenAI 企业版索引通过对向量与关键词索引使用混合搜索，结合深度学习模型排序，提供高精度的检索结果。

您还可以使用 `Retrieve` API 执行以下操作：
+ 覆盖指数级别的提升
+ 根据文档字段或属性进行筛选
+ 根据用户或其群组对文档的访问权限进行筛选
+ 查看置信度分数区以获取检索到的通过结果。 Amazon Kendra 的置信度分区提供相对排名，表示响应与查询相关的信心程度。
**注意**  
置信度分数桶目前仅适用于英语。

您还可以在响应中加入某些字段，这些字段可能会提供有用的其他信息。

`Retrieve` API 当前不支持以下功能：使用[高级查询语法](https://docs.aws.amazon.com/kendra/latest/dg/searching-example.html#searching-index-query-syntax)进行查询、对查询使用[建议的拼写更正](https://docs.aws.amazon.com/kendra/latest/dg/query-spell-check.html)、[分面](https://docs.aws.amazon.com/kendra/latest/dg/filtering.html#search-facets)，自动完成搜索查询的[查询建议](https://docs.aws.amazon.com/kendra/latest/dg/query-suggestions.html)以及[增量学习](https://docs.aws.amazon.com/kendra/latest/dg/submitting-feedback.html)。任何检索 API 查询都不会出现在分析控制面板中。

`Retrieve` API 共享您为索引设置的[查询容量单位](https://docs.aws.amazon.com/kendra/latest/APIReference/API_CapacityUnitsConfiguration.html)数。有关单个容量单位中包含的内容以及索引的默认基本容量的更多信息，请参阅[调整容量](https://docs.aws.amazon.com/kendra/latest/dg/adjusting-capacity.html)。

**注意**  
如果您使用的是 Amazon Kendra 开发者版，则无法添加容量；只能在使用 Amazon Kendra 企业版时添加容量。有关开发人员版和企业版中包含的内容的更多信息，请参阅 [Amazon Kendra 版本](https://docs.aws.amazon.com/kendra/latest/dg/what-is-kendra.html#kendra-editions)。

以下是使用 `Retrieve` API 从 "how does amazon kendra work?" 查询索引中的文档中检索前 100 条最相关的段落的示例

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

```
import boto3
import pprint

kendra = boto3.client("kendra")

# Provide the index ID
index_id = "index-id"
# Provide the query text
query = "how does amazon kendra work?"
# You can retrieve up to 100 relevant passages
# You can paginate 100 passages across 10 pages, for example
page_size = 10
page_number = 10

result = kendra.retrieve(
        IndexId = index_id,
        QueryText = query,
        PageSize = page_size,
        PageNumber = page_number)

print("\nRetrieved passage results for query: " + query + "\n")        

for retrieve_result in result["ResultItems"]:

    print("-------------------")
    print("Title: " + str(retrieve_result["DocumentTitle"]))
    print("URI: " + str(retrieve_result["DocumentURI"]))
    print("Passage content: " + str(retrieve_result["Content"]))
    print("------------------\n\n")
```

------
#### [ Java ]

```
package com.amazonaws.kendra;

import software.amazon.awssdk.services.kendra.KendraClient;
import software.amazon.awssdk.services.kendra.model.RetrieveRequest;
import software.amazon.awssdk.services.kendra.model.RetrieveResult;
import software.amazon.awssdk.services.kendra.model.RetrieveResultItem;

public class RetrievePassageExample {
    public static void main(String[] args) {
        KendraClient kendra = KendraClient.builder().build();
        
        String indxId = "index-id";
        String query = "how does amazon kendra work?";
        Integer pgSize = 10;
        Integer pgNumber = 10;

        RetrieveRequest retrieveRequest = retrieveRequest
            .builder()
            .indexId(indxId)
            .queryText(query)
            .pageSize(pgSize)
            .pageNumber(pgNumber)
            .build();

        RetrieveResult retrieveResult = kendra.retrieve(retrieveRequest);

        System.out.println(String.format("\nRetrieved passage results for query: %s", query));
        for(RetrieveResultItem item: retrieveResult.resultItems()) {
            System.out.println("----------------------");
            System.out.println(String.format("Title: %s", documentTitle));
            System.out.println(String.format("URI: %s", documentURI));
            System.out.println(String.format("Passage content: %s", content));
            System.out.println("-----------------------\n");
        }
    }
}
```

------