Query
Searches an index given an input query.
Note
If you are working with large language models (LLMs) or implementing retrieval
augmented generation (RAG) systems, you can use Amazon Kendra's Retrieve API, which can return longer semantically relevant passages. We
recommend using the Retrieve
API instead of filing a service limit increase
to increase the Query
API document excerpt length.
You can configure boosting or relevance tuning at the query level to override boosting at the index level, filter based on document fields/attributes and faceted search, and filter based on the user or their group access to documents. You can also include certain fields in the response that might provide useful additional information.
A query response contains three types of results.
-
Relevant suggested answers. The answers can be either a text excerpt or table excerpt. The answer can be highlighted in the excerpt.
-
Matching FAQs or questions-answer from your FAQ file.
-
Relevant documents. This result type includes an excerpt of the document with the document title. The searched terms can be highlighted in the excerpt.
You can specify that the query return only one type of result using the
QueryResultTypeFilter
parameter. Each query returns the 100 most relevant
results. If you filter result type to only question-answers, a maximum of four results are
returned. If you filter result type to only answers, a maximum of three results are
returned.
Request Syntax
{
"AttributeFilter": {
"AndAllFilters": [
"AttributeFilter"
],
"ContainsAll": {
"Key": "string
",
"Value": {
"DateValue": number
,
"LongValue": number
,
"StringListValue": [ "string
" ],
"StringValue": "string
"
}
},
"ContainsAny": {
"Key": "string
",
"Value": {
"DateValue": number
,
"LongValue": number
,
"StringListValue": [ "string
" ],
"StringValue": "string
"
}
},
"EqualsTo": {
"Key": "string
",
"Value": {
"DateValue": number
,
"LongValue": number
,
"StringListValue": [ "string
" ],
"StringValue": "string
"
}
},
"GreaterThan": {
"Key": "string
",
"Value": {
"DateValue": number
,
"LongValue": number
,
"StringListValue": [ "string
" ],
"StringValue": "string
"
}
},
"GreaterThanOrEquals": {
"Key": "string
",
"Value": {
"DateValue": number
,
"LongValue": number
,
"StringListValue": [ "string
" ],
"StringValue": "string
"
}
},
"LessThan": {
"Key": "string
",
"Value": {
"DateValue": number
,
"LongValue": number
,
"StringListValue": [ "string
" ],
"StringValue": "string
"
}
},
"LessThanOrEquals": {
"Key": "string
",
"Value": {
"DateValue": number
,
"LongValue": number
,
"StringListValue": [ "string
" ],
"StringValue": "string
"
}
},
"NotFilter": "AttributeFilter",
"OrAllFilters": [
"AttributeFilter"
]
},
"CollapseConfiguration": {
"DocumentAttributeKey": "string
",
"Expand": boolean
,
"ExpandConfiguration": {
"MaxExpandedResultsPerItem": number
,
"MaxResultItemsToExpand": number
},
"MissingAttributeKeyStrategy": "string
",
"SortingConfigurations": [
{
"DocumentAttributeKey": "string
",
"SortOrder": "string
"
}
]
},
"DocumentRelevanceOverrideConfigurations": [
{
"Name": "string
",
"Relevance": {
"Duration": "string
",
"Freshness": boolean
,
"Importance": number
,
"RankOrder": "string
",
"ValueImportanceMap": {
"string
" : number
}
}
}
],
"Facets": [
{
"DocumentAttributeKey": "string
",
"Facets": [
"Facet"
],
"MaxResults": number
}
],
"IndexId": "string
",
"PageNumber": number
,
"PageSize": number
,
"QueryResultTypeFilter": "string
",
"QueryText": "string
",
"RequestedDocumentAttributes": [ "string
" ],
"SortingConfiguration": {
"DocumentAttributeKey": "string
",
"SortOrder": "string
"
},
"SortingConfigurations": [
{
"DocumentAttributeKey": "string
",
"SortOrder": "string
"
}
],
"SpellCorrectionConfiguration": {
"IncludeQuerySpellCheckSuggestions": boolean
},
"UserContext": {
"DataSourceGroups": [
{
"DataSourceId": "string
",
"GroupId": "string
"
}
],
"Groups": [ "string
" ],
"Token": "string
",
"UserId": "string
"
},
"VisitorId": "string
"
}
Request Parameters
For information about the parameters that are common to all actions, see Common Parameters.
The request accepts the following data in JSON format.
- AttributeFilter
-
Filters search results by document fields/attributes. You can only provide one attribute filter; however, the
AndAllFilters
,NotFilter
, andOrAllFilters
parameters contain a list of other filters.The
AttributeFilter
parameter means you can create a set of filtering rules that a document must satisfy to be included in the query results.Type: AttributeFilter object
Required: No
- CollapseConfiguration
-
Provides configuration to determine how to group results by document attribute value, and how to display them (collapsed or expanded) under a designated primary document for each group.
Type: CollapseConfiguration object
Required: No
- DocumentRelevanceOverrideConfigurations
-
Overrides relevance tuning configurations of fields/attributes set at the index level.
If you use this API to override the relevance tuning configured at the index level, but there is no relevance tuning configured at the index level, then Amazon Kendra does not apply any relevance tuning.
If there is relevance tuning configured for fields at the index level, and you use this API to override only some of these fields, then for the fields you did not override, the importance is set to 1.
Type: Array of DocumentRelevanceConfiguration objects
Array Members: Minimum number of 0 items. Maximum number of 500 items.
Required: No
- Facets
-
An array of documents fields/attributes for faceted search. Amazon Kendra returns a count for each field key specified. This helps your users narrow their search.
Type: Array of Facet objects
Required: No
- IndexId
-
The identifier of the index for the search.
Type: String
Length Constraints: Fixed length of 36.
Pattern:
[a-zA-Z0-9][a-zA-Z0-9-]*
Required: Yes
- PageNumber
-
Query results are returned in pages the size of the
PageSize
parameter. By default, Amazon Kendra returns the first page of results. Use this parameter to get result pages after the first one.Type: Integer
Required: No
- PageSize
-
Sets the number of results that are returned in each page of results. The default page size is 10. The maximum number of results returned is 100. If you ask for more than 100 results, only 100 are returned.
Type: Integer
Required: No
- QueryResultTypeFilter
-
Sets the type of query result or response. Only results for the specified type are returned.
Type: String
Valid Values:
DOCUMENT | QUESTION_ANSWER | ANSWER
Required: No
- QueryText
-
The input query text for the search. Amazon Kendra truncates queries at 30 token words, which excludes punctuation and stop words. Truncation still applies if you use Boolean or more advanced, complex queries. For example,
Timeoff AND October AND Category:HR
is counted as 3 tokens:timeoff
,october
,hr
. For more information, see Searching with advanced query syntax in the Amazon Kendra Developer Guide.Type: String
Required: No
- RequestedDocumentAttributes
-
An array of document fields/attributes to include in the response. You can limit the response to include certain document fields. By default, all document attributes are included in the response.
Type: Array of strings
Array Members: Minimum number of 1 item. Maximum number of 100 items.
Length Constraints: Minimum length of 1. Maximum length of 200.
Pattern:
[a-zA-Z0-9_][a-zA-Z0-9_-]*
Required: No
- SortingConfiguration
-
Provides information that determines how the results of the query are sorted. You can set the field that Amazon Kendra should sort the results on, and specify whether the results should be sorted in ascending or descending order. In the case of ties in sorting the results, the results are sorted by relevance.
If you don't provide sorting configuration, the results are sorted by the relevance that Amazon Kendra determines for the result.
Type: SortingConfiguration object
Required: No
- SortingConfigurations
-
Provides configuration information to determine how the results of a query are sorted.
You can set upto 3 fields that Amazon Kendra should sort the results on, and specify whether the results should be sorted in ascending or descending order. The sort field quota can be increased.
If you don't provide a sorting configuration, the results are sorted by the relevance that Amazon Kendra determines for the result. In the case of ties in sorting the results, the results are sorted by relevance.
Type: Array of SortingConfiguration objects
Array Members: Minimum number of 1 item.
Required: No
- SpellCorrectionConfiguration
-
Enables suggested spell corrections for queries.
Type: SpellCorrectionConfiguration object
Required: No
- UserContext
-
The user context token or user and group information.
Type: UserContext object
Required: No
- VisitorId
-
Provides an identifier for a specific user. The
VisitorId
should be a unique identifier, such as a GUID. Don't use personally identifiable information, such as the user's email address, as theVisitorId
.Type: String
Length Constraints: Minimum length of 1. Maximum length of 256.
Pattern:
[a-zA-Z0-9][a-zA-Z0-9_-]*
Required: No
Response Syntax
{
"FacetResults": [
{
"DocumentAttributeKey": "string",
"DocumentAttributeValueCountPairs": [
{
"Count": number,
"DocumentAttributeValue": {
"DateValue": number,
"LongValue": number,
"StringListValue": [ "string" ],
"StringValue": "string"
},
"FacetResults": [
"FacetResult"
]
}
],
"DocumentAttributeValueType": "string"
}
],
"FeaturedResultsItems": [
{
"AdditionalAttributes": [
{
"Key": "string",
"Value": {
"TextWithHighlightsValue": {
"Highlights": [
{
"BeginOffset": number,
"EndOffset": number,
"TopAnswer": boolean,
"Type": "string"
}
],
"Text": "string"
}
},
"ValueType": "string"
}
],
"DocumentAttributes": [
{
"Key": "string",
"Value": {
"DateValue": number,
"LongValue": number,
"StringListValue": [ "string" ],
"StringValue": "string"
}
}
],
"DocumentExcerpt": {
"Highlights": [
{
"BeginOffset": number,
"EndOffset": number,
"TopAnswer": boolean,
"Type": "string"
}
],
"Text": "string"
},
"DocumentId": "string",
"DocumentTitle": {
"Highlights": [
{
"BeginOffset": number,
"EndOffset": number,
"TopAnswer": boolean,
"Type": "string"
}
],
"Text": "string"
},
"DocumentURI": "string",
"FeedbackToken": "string",
"Id": "string",
"Type": "string"
}
],
"QueryId": "string",
"ResultItems": [
{
"AdditionalAttributes": [
{
"Key": "string",
"Value": {
"TextWithHighlightsValue": {
"Highlights": [
{
"BeginOffset": number,
"EndOffset": number,
"TopAnswer": boolean,
"Type": "string"
}
],
"Text": "string"
}
},
"ValueType": "string"
}
],
"CollapsedResultDetail": {
"DocumentAttribute": {
"Key": "string",
"Value": {
"DateValue": number,
"LongValue": number,
"StringListValue": [ "string" ],
"StringValue": "string"
}
},
"ExpandedResults": [
{
"DocumentAttributes": [
{
"Key": "string",
"Value": {
"DateValue": number,
"LongValue": number,
"StringListValue": [ "string" ],
"StringValue": "string"
}
}
],
"DocumentExcerpt": {
"Highlights": [
{
"BeginOffset": number,
"EndOffset": number,
"TopAnswer": boolean,
"Type": "string"
}
],
"Text": "string"
},
"DocumentId": "string",
"DocumentTitle": {
"Highlights": [
{
"BeginOffset": number,
"EndOffset": number,
"TopAnswer": boolean,
"Type": "string"
}
],
"Text": "string"
},
"DocumentURI": "string",
"Id": "string"
}
]
},
"DocumentAttributes": [
{
"Key": "string",
"Value": {
"DateValue": number,
"LongValue": number,
"StringListValue": [ "string" ],
"StringValue": "string"
}
}
],
"DocumentExcerpt": {
"Highlights": [
{
"BeginOffset": number,
"EndOffset": number,
"TopAnswer": boolean,
"Type": "string"
}
],
"Text": "string"
},
"DocumentId": "string",
"DocumentTitle": {
"Highlights": [
{
"BeginOffset": number,
"EndOffset": number,
"TopAnswer": boolean,
"Type": "string"
}
],
"Text": "string"
},
"DocumentURI": "string",
"FeedbackToken": "string",
"Format": "string",
"Id": "string",
"ScoreAttributes": {
"ScoreConfidence": "string"
},
"TableExcerpt": {
"Rows": [
{
"Cells": [
{
"Header": boolean,
"Highlighted": boolean,
"TopAnswer": boolean,
"Value": "string"
}
]
}
],
"TotalNumberOfRows": number
},
"Type": "string"
}
],
"SpellCorrectedQueries": [
{
"Corrections": [
{
"BeginOffset": number,
"CorrectedTerm": "string",
"EndOffset": number,
"Term": "string"
}
],
"SuggestedQueryText": "string"
}
],
"TotalNumberOfResults": number,
"Warnings": [
{
"Code": "string",
"Message": "string"
}
]
}
Response Elements
If the action is successful, the service sends back an HTTP 200 response.
The following data is returned in JSON format by the service.
- FacetResults
-
Contains the facet results. A
FacetResult
contains the counts for each field/attribute key that was specified in theFacets
input parameter.Type: Array of FacetResult objects
- FeaturedResultsItems
-
The list of featured result items. Featured results are displayed at the top of the search results page, placed above all other results for certain queries. If there's an exact match of a query, then certain documents are featured in the search results.
Type: Array of FeaturedResultsItem objects
- QueryId
-
The identifier for the search. You also use
QueryId
to identify the search when using the SubmitFeedback API.Type: String
Length Constraints: Minimum length of 1. Maximum length of 36.
Pattern:
[a-zA-Z0-9][a-zA-Z0-9-]*
- ResultItems
-
The results of the search.
Type: Array of QueryResultItem objects
- SpellCorrectedQueries
-
A list of information related to suggested spell corrections for a query.
Type: Array of SpellCorrectedQuery objects
- TotalNumberOfResults
-
The total number of items found by the search. However, you can only retrieve up to 100 items. For example, if the search found 192 items, you can only retrieve the first 100 of the items.
Type: Integer
- Warnings
-
A list of warning codes and their messages on problems with your query.
Amazon Kendra currently only supports one type of warning, which is a warning on invalid syntax used in the query. For examples of invalid query syntax, see Searching with advanced query syntax.
Type: Array of Warning objects
Array Members: Fixed number of 1 item.
Errors
For information about the errors that are common to all actions, see Common Errors.
- AccessDeniedException
-
You don't have sufficient access to perform this action. Please ensure you have the required permission policies and user accounts and try again.
HTTP Status Code: 400
- ConflictException
-
A conflict occurred with the request. Please fix any inconsistences with your resources and try again.
HTTP Status Code: 400
- InternalServerException
-
An issue occurred with the internal server used for your Amazon Kendra service. Please wait a few minutes and try again, or contact Support
for help. HTTP Status Code: 500
- ResourceNotFoundException
-
The resource you want to use doesn’t exist. Please check you have provided the correct resource and try again.
HTTP Status Code: 400
- ServiceQuotaExceededException
-
You have exceeded the set limits for your Amazon Kendra service. Please see Quotas for more information, or contact Support
to inquire about an increase of limits. HTTP Status Code: 400
- ThrottlingException
-
The request was denied due to request throttling. Please reduce the number of requests and try again.
HTTP Status Code: 400
- ValidationException
-
The input fails to satisfy the constraints set by the Amazon Kendra service. Please provide the correct input and try again.
HTTP Status Code: 400
See Also
For more information about using this API in one of the language-specific AWS SDKs, see the following: