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')

query='${searchString}'
index_id='${indexID}'

response=kendra.query(
        QueryText = query,
        IndexId = index_id)

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

for query_result in response['ResultItems']:

    print('-------------------')
    print('Type: ' + str(query_result['Type']))
        
    if query_result['Type']=='ANSWER':
        answer_text = query_result['DocumentExcerpt']['Text']
        print(answer_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(document_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.