Preparing a query

Now that you have the right permissions to query our index, try a sample query.

This is a suggestion from Amazon Kendra’s documentation:

import boto3
import pprint

kendra = boto3.client('kendra')


        QueryText = query,
        IndexId = index_id)

print ('\nSearch results for query: ' + query + '\n')        

for query_result in response['ResultItems']:

    print('Type: ' + str(query_result['Type']))
    if query_result['Type']=='ANSWER':
        answer_text = query_result['DocumentExcerpt']['Text']

    if query_result['Type']=='DOCUMENT':
        if 'DocumentTitle' in query_result:
            document_title = query_result['DocumentTitle']['Text']
            print('Title: ' + document_title)
        document_text = query_result['DocumentExcerpt']['Text']

    print ('------------------\n\n')  

As you see there are 2 variables you need to fill: query and index_id.

Follow these steps to get the Amazon Kendra index ID.

  1. On the Kendra Console on the Indexes list, click on the Index you created for the workshop.

  1. Under index settings, you will find your Index ID.