- Navigation GuideYou are on a Command (operation) page with structural examples. Use the navigation breadcrumb if you would like to return to the Client landing page.
QueryCommand
Searches an index given an input query.
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.
If you're using an Amazon Kendra Gen AI Enterprise Edition index, you can only use ATTRIBUTE_FILTER
to filter search results by user context. If you're using an Amazon Kendra Gen AI Enterprise Edition index and you try to use USER_TOKEN
to configure user context policy, Amazon Kendra returns a ValidationException
error.
Example Syntax
Use a bare-bones client and the command you need to make an API call.
import { KendraClient, QueryCommand } from "@aws-sdk/client-kendra"; // ES Modules import
// const { KendraClient, QueryCommand } = require("@aws-sdk/client-kendra"); // CommonJS import
const client = new KendraClient(config);
const input = { // QueryRequest
IndexId: "STRING_VALUE", // required
QueryText: "STRING_VALUE",
AttributeFilter: { // AttributeFilter
AndAllFilters: [ // AttributeFilterList
{
AndAllFilters: [
"<AttributeFilter>",
],
OrAllFilters: [
"<AttributeFilter>",
],
NotFilter: "<AttributeFilter>",
EqualsTo: { // DocumentAttribute
Key: "STRING_VALUE", // required
Value: { // DocumentAttributeValue
StringValue: "STRING_VALUE",
StringListValue: [ // DocumentAttributeStringListValue
"STRING_VALUE",
],
LongValue: Number("long"),
DateValue: new Date("TIMESTAMP"),
},
},
ContainsAll: {
Key: "STRING_VALUE", // required
Value: {
StringValue: "STRING_VALUE",
StringListValue: [
"STRING_VALUE",
],
LongValue: Number("long"),
DateValue: new Date("TIMESTAMP"),
},
},
ContainsAny: {
Key: "STRING_VALUE", // required
Value: {
StringValue: "STRING_VALUE",
StringListValue: [
"STRING_VALUE",
],
LongValue: Number("long"),
DateValue: new Date("TIMESTAMP"),
},
},
GreaterThan: {
Key: "STRING_VALUE", // required
Value: {
StringValue: "STRING_VALUE",
StringListValue: [
"STRING_VALUE",
],
LongValue: Number("long"),
DateValue: new Date("TIMESTAMP"),
},
},
GreaterThanOrEquals: {
Key: "STRING_VALUE", // required
Value: {
StringValue: "STRING_VALUE",
StringListValue: [
"STRING_VALUE",
],
LongValue: Number("long"),
DateValue: new Date("TIMESTAMP"),
},
},
LessThan: "<DocumentAttribute>",
LessThanOrEquals: "<DocumentAttribute>",
},
],
OrAllFilters: [
"<AttributeFilter>",
],
NotFilter: "<AttributeFilter>",
EqualsTo: "<DocumentAttribute>",
ContainsAll: "<DocumentAttribute>",
ContainsAny: "<DocumentAttribute>",
GreaterThan: "<DocumentAttribute>",
GreaterThanOrEquals: "<DocumentAttribute>",
LessThan: "<DocumentAttribute>",
LessThanOrEquals: "<DocumentAttribute>",
},
Facets: [ // FacetList
{ // Facet
DocumentAttributeKey: "STRING_VALUE",
Facets: [
{
DocumentAttributeKey: "STRING_VALUE",
Facets: "<FacetList>",
MaxResults: Number("int"),
},
],
MaxResults: Number("int"),
},
],
RequestedDocumentAttributes: [ // DocumentAttributeKeyList
"STRING_VALUE",
],
QueryResultTypeFilter: "DOCUMENT" || "QUESTION_ANSWER" || "ANSWER",
DocumentRelevanceOverrideConfigurations: [ // DocumentRelevanceOverrideConfigurationList
{ // DocumentRelevanceConfiguration
Name: "STRING_VALUE", // required
Relevance: { // Relevance
Freshness: true || false,
Importance: Number("int"),
Duration: "STRING_VALUE",
RankOrder: "ASCENDING" || "DESCENDING",
ValueImportanceMap: { // ValueImportanceMap
"<keys>": Number("int"),
},
},
},
],
PageNumber: Number("int"),
PageSize: Number("int"),
SortingConfiguration: { // SortingConfiguration
DocumentAttributeKey: "STRING_VALUE", // required
SortOrder: "DESC" || "ASC", // required
},
SortingConfigurations: [ // SortingConfigurationList
{
DocumentAttributeKey: "STRING_VALUE", // required
SortOrder: "DESC" || "ASC", // required
},
],
UserContext: { // UserContext
Token: "STRING_VALUE",
UserId: "STRING_VALUE",
Groups: [ // Groups
"STRING_VALUE",
],
DataSourceGroups: [ // DataSourceGroups
{ // DataSourceGroup
GroupId: "STRING_VALUE", // required
DataSourceId: "STRING_VALUE", // required
},
],
},
VisitorId: "STRING_VALUE",
SpellCorrectionConfiguration: { // SpellCorrectionConfiguration
IncludeQuerySpellCheckSuggestions: true || false, // required
},
CollapseConfiguration: { // CollapseConfiguration
DocumentAttributeKey: "STRING_VALUE", // required
SortingConfigurations: [
"<SortingConfiguration>",
],
MissingAttributeKeyStrategy: "IGNORE" || "COLLAPSE" || "EXPAND",
Expand: true || false,
ExpandConfiguration: { // ExpandConfiguration
MaxResultItemsToExpand: Number("int"),
MaxExpandedResultsPerItem: Number("int"),
},
},
};
const command = new QueryCommand(input);
const response = await client.send(command);
// { // QueryResult
// QueryId: "STRING_VALUE",
// ResultItems: [ // QueryResultItemList
// { // QueryResultItem
// Id: "STRING_VALUE",
// Type: "DOCUMENT" || "QUESTION_ANSWER" || "ANSWER",
// Format: "TABLE" || "TEXT",
// AdditionalAttributes: [ // AdditionalResultAttributeList
// { // AdditionalResultAttribute
// Key: "STRING_VALUE", // required
// ValueType: "TEXT_WITH_HIGHLIGHTS_VALUE", // required
// Value: { // AdditionalResultAttributeValue
// TextWithHighlightsValue: { // TextWithHighlights
// Text: "STRING_VALUE",
// Highlights: [ // HighlightList
// { // Highlight
// BeginOffset: Number("int"), // required
// EndOffset: Number("int"), // required
// TopAnswer: true || false,
// Type: "STANDARD" || "THESAURUS_SYNONYM",
// },
// ],
// },
// },
// },
// ],
// DocumentId: "STRING_VALUE",
// DocumentTitle: {
// Text: "STRING_VALUE",
// Highlights: [
// {
// BeginOffset: Number("int"), // required
// EndOffset: Number("int"), // required
// TopAnswer: true || false,
// Type: "STANDARD" || "THESAURUS_SYNONYM",
// },
// ],
// },
// DocumentExcerpt: {
// Text: "STRING_VALUE",
// Highlights: [
// {
// BeginOffset: Number("int"), // required
// EndOffset: Number("int"), // required
// TopAnswer: true || false,
// Type: "STANDARD" || "THESAURUS_SYNONYM",
// },
// ],
// },
// DocumentURI: "STRING_VALUE",
// DocumentAttributes: [ // DocumentAttributeList
// { // DocumentAttribute
// Key: "STRING_VALUE", // required
// Value: { // DocumentAttributeValue
// StringValue: "STRING_VALUE",
// StringListValue: [ // DocumentAttributeStringListValue
// "STRING_VALUE",
// ],
// LongValue: Number("long"),
// DateValue: new Date("TIMESTAMP"),
// },
// },
// ],
// ScoreAttributes: { // ScoreAttributes
// ScoreConfidence: "VERY_HIGH" || "HIGH" || "MEDIUM" || "LOW" || "NOT_AVAILABLE",
// },
// FeedbackToken: "STRING_VALUE",
// TableExcerpt: { // TableExcerpt
// Rows: [ // TableRowList
// { // TableRow
// Cells: [ // TableCellList
// { // TableCell
// Value: "STRING_VALUE",
// TopAnswer: true || false,
// Highlighted: true || false,
// Header: true || false,
// },
// ],
// },
// ],
// TotalNumberOfRows: Number("int"),
// },
// CollapsedResultDetail: { // CollapsedResultDetail
// DocumentAttribute: {
// Key: "STRING_VALUE", // required
// Value: {
// StringValue: "STRING_VALUE",
// StringListValue: [
// "STRING_VALUE",
// ],
// LongValue: Number("long"),
// DateValue: new Date("TIMESTAMP"),
// },
// },
// ExpandedResults: [ // ExpandedResultList
// { // ExpandedResultItem
// Id: "STRING_VALUE",
// DocumentId: "STRING_VALUE",
// DocumentTitle: {
// Text: "STRING_VALUE",
// Highlights: [
// {
// BeginOffset: Number("int"), // required
// EndOffset: Number("int"), // required
// TopAnswer: true || false,
// Type: "STANDARD" || "THESAURUS_SYNONYM",
// },
// ],
// },
// DocumentExcerpt: {
// Text: "STRING_VALUE",
// Highlights: [
// {
// BeginOffset: Number("int"), // required
// EndOffset: Number("int"), // required
// TopAnswer: true || false,
// Type: "STANDARD" || "THESAURUS_SYNONYM",
// },
// ],
// },
// DocumentURI: "STRING_VALUE",
// DocumentAttributes: [
// {
// Key: "STRING_VALUE", // required
// Value: {
// StringValue: "STRING_VALUE",
// StringListValue: [
// "STRING_VALUE",
// ],
// LongValue: Number("long"),
// DateValue: new Date("TIMESTAMP"),
// },
// },
// ],
// },
// ],
// },
// },
// ],
// FacetResults: [ // FacetResultList
// { // FacetResult
// DocumentAttributeKey: "STRING_VALUE",
// DocumentAttributeValueType: "STRING_VALUE" || "STRING_LIST_VALUE" || "LONG_VALUE" || "DATE_VALUE",
// DocumentAttributeValueCountPairs: [ // DocumentAttributeValueCountPairList
// { // DocumentAttributeValueCountPair
// DocumentAttributeValue: {
// StringValue: "STRING_VALUE",
// StringListValue: [
// "STRING_VALUE",
// ],
// LongValue: Number("long"),
// DateValue: new Date("TIMESTAMP"),
// },
// Count: Number("int"),
// FacetResults: [
// {
// DocumentAttributeKey: "STRING_VALUE",
// DocumentAttributeValueType: "STRING_VALUE" || "STRING_LIST_VALUE" || "LONG_VALUE" || "DATE_VALUE",
// DocumentAttributeValueCountPairs: [
// {
// DocumentAttributeValue: "<DocumentAttributeValue>",
// Count: Number("int"),
// FacetResults: "<FacetResultList>",
// },
// ],
// },
// ],
// },
// ],
// },
// ],
// TotalNumberOfResults: Number("int"),
// Warnings: [ // WarningList
// { // Warning
// Message: "STRING_VALUE",
// Code: "QUERY_LANGUAGE_INVALID_SYNTAX",
// },
// ],
// SpellCorrectedQueries: [ // SpellCorrectedQueryList
// { // SpellCorrectedQuery
// SuggestedQueryText: "STRING_VALUE",
// Corrections: [ // CorrectionList
// { // Correction
// BeginOffset: Number("int"),
// EndOffset: Number("int"),
// Term: "STRING_VALUE",
// CorrectedTerm: "STRING_VALUE",
// },
// ],
// },
// ],
// FeaturedResultsItems: [ // FeaturedResultsItemList
// { // FeaturedResultsItem
// Id: "STRING_VALUE",
// Type: "DOCUMENT" || "QUESTION_ANSWER" || "ANSWER",
// AdditionalAttributes: [
// {
// Key: "STRING_VALUE", // required
// ValueType: "TEXT_WITH_HIGHLIGHTS_VALUE", // required
// Value: {
// TextWithHighlightsValue: "<TextWithHighlights>",
// },
// },
// ],
// DocumentId: "STRING_VALUE",
// DocumentTitle: "<TextWithHighlights>",
// DocumentExcerpt: "<TextWithHighlights>",
// DocumentURI: "STRING_VALUE",
// DocumentAttributes: [
// {
// Key: "STRING_VALUE", // required
// Value: "<DocumentAttributeValue>", // required
// },
// ],
// FeedbackToken: "STRING_VALUE",
// },
// ],
// };
QueryCommand Input
Parameter | Type | Description |
---|
Parameter | Type | Description |
---|---|---|
IndexId Required | string | undefined | The identifier of the index for the search. |
AttributeFilter | AttributeFilter | undefined | Filters search results by document fields/attributes. You can only provide one attribute filter; however, the The For Amazon Kendra Gen AI Enterprise Edition indices use |
CollapseConfiguration | CollapseConfiguration | undefined | 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. |
DocumentRelevanceOverrideConfigurations | DocumentRelevanceConfiguration[] | undefined | 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. |
Facets | Facet[] | undefined | 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. |
PageNumber | number | undefined | Query results are returned in pages the size of the |
PageSize | number | undefined | 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. |
QueryResultTypeFilter | QueryResultType | undefined | Sets the type of query result or response. Only results for the specified type are returned. |
QueryText | string | undefined | 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, |
RequestedDocumentAttributes | string[] | undefined | 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. |
SortingConfiguration | SortingConfiguration | undefined | 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. |
SortingConfigurations | SortingConfiguration[] | undefined | 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. |
SpellCorrectionConfiguration | SpellCorrectionConfiguration | undefined | Enables suggested spell corrections for queries. |
UserContext | UserContext | undefined | The user context token or user and group information. |
VisitorId | string | undefined | Provides an identifier for a specific user. The |
QueryCommand Output
Parameter | Type | Description |
---|
Parameter | Type | Description |
---|---|---|
$metadata Required | ResponseMetadata | Metadata pertaining to this request. |
FacetResults | FacetResult[] | undefined | Contains the facet results. A |
FeaturedResultsItems | FeaturedResultsItem[] | undefined | 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. |
QueryId | string | undefined | The identifier for the search. You also use |
ResultItems | QueryResultItem[] | undefined | The results of the search. |
SpellCorrectedQueries | SpellCorrectedQuery[] | undefined | A list of information related to suggested spell corrections for a query. |
TotalNumberOfResults | number | undefined | 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. |
Warnings | Warning[] | undefined | 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 . |
Throws
Name | Fault | Details |
---|
Name | Fault | Details |
---|---|---|
AccessDeniedException | client | You don't have sufficient access to perform this action. Please ensure you have the required permission policies and user accounts and try again. |
ConflictException | client | A conflict occurred with the request. Please fix any inconsistences with your resources and try again. |
InternalServerException | server | 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. |
ResourceNotFoundException | client | The resource you want to use doesn’t exist. Please check you have provided the correct resource and try again. |
ServiceQuotaExceededException | client | |
ThrottlingException | client | The request was denied due to request throttling. Please reduce the number of requests and try again. |
ValidationException | client | The input fails to satisfy the constraints set by the Amazon Kendra service. Please provide the correct input and try again. |
KendraServiceException | Base exception class for all service exceptions from Kendra service. |