query method

Future<QueryResult> query({
  1. required String indexId,
  2. required String queryText,
  3. AttributeFilter? attributeFilter,
  4. List<Facet>? facets,
  5. int? pageNumber,
  6. int? pageSize,
  7. QueryResultType? queryResultTypeFilter,
  8. List<String>? requestedDocumentAttributes,
  9. SortingConfiguration? sortingConfiguration,
  10. UserContext? userContext,
  11. String? visitorId,
})

Searches an active index. Use this API to search your documents using query. The Query operation enables to do faceted search and to filter results based on document attributes.

It also enables you to provide user context that Amazon Kendra uses to enforce document access control in the search results.

Amazon Kendra searches your index for text content and question and answer (FAQ) content. By default the response contains three types of results.

  • Relevant passages
  • Matching FAQs
  • Relevant documents
You can specify that the query return only one type of result using the QueryResultTypeConfig parameter.

Each query returns the 100 most relevant results.

May throw ValidationException. May throw ConflictException. May throw ResourceNotFoundException. May throw ThrottlingException. May throw AccessDeniedException. May throw ServiceQuotaExceededException. May throw InternalServerException.

Parameter indexId : The unique identifier of the index to search. The identifier is returned in the response from the operation.

Parameter queryText : The text to search for.

Parameter attributeFilter : Enables filtered searches based on document attributes. You can only provide one attribute filter; however, the AndAllFilters, NotFilter, and OrAllFilters parameters contain a list of other filters.

The AttributeFilter parameter enables you to create a set of filtering rules that a document must satisfy to be included in the query results.

Parameter facets : An array of documents attributes. Amazon Kendra returns a count for each attribute key specified. You can use this information to help narrow the search for your user.

Parameter 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.

Parameter 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.

Parameter queryResultTypeFilter : Sets the type of query. Only results for the specified query type are returned.

Parameter requestedDocumentAttributes : An array of document attributes to include in the response. No other document attributes are included in the response. By default all document attributes are included in the response.

Parameter 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.

Parameter userContext : The user context token.

Parameter 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 the VisitorId.

Implementation

Future<QueryResult> query({
  required String indexId,
  required String queryText,
  AttributeFilter? attributeFilter,
  List<Facet>? facets,
  int? pageNumber,
  int? pageSize,
  QueryResultType? queryResultTypeFilter,
  List<String>? requestedDocumentAttributes,
  SortingConfiguration? sortingConfiguration,
  UserContext? userContext,
  String? visitorId,
}) async {
  ArgumentError.checkNotNull(indexId, 'indexId');
  _s.validateStringLength(
    'indexId',
    indexId,
    36,
    36,
    isRequired: true,
  );
  ArgumentError.checkNotNull(queryText, 'queryText');
  _s.validateStringLength(
    'queryText',
    queryText,
    1,
    1000,
    isRequired: true,
  );
  _s.validateStringLength(
    'visitorId',
    visitorId,
    1,
    256,
  );
  final headers = <String, String>{
    'Content-Type': 'application/x-amz-json-1.1',
    'X-Amz-Target': 'AWSKendraFrontendService.Query'
  };
  final jsonResponse = await _protocol.send(
    method: 'POST',
    requestUri: '/',
    exceptionFnMap: _exceptionFns,
    // TODO queryParams
    headers: headers,
    payload: {
      'IndexId': indexId,
      'QueryText': queryText,
      if (attributeFilter != null) 'AttributeFilter': attributeFilter,
      if (facets != null) 'Facets': facets,
      if (pageNumber != null) 'PageNumber': pageNumber,
      if (pageSize != null) 'PageSize': pageSize,
      if (queryResultTypeFilter != null)
        'QueryResultTypeFilter': queryResultTypeFilter.toValue(),
      if (requestedDocumentAttributes != null)
        'RequestedDocumentAttributes': requestedDocumentAttributes,
      if (sortingConfiguration != null)
        'SortingConfiguration': sortingConfiguration,
      if (userContext != null) 'UserContext': userContext,
      if (visitorId != null) 'VisitorId': visitorId,
    },
  );

  return QueryResult.fromJson(jsonResponse.body);
}