- Newest
- Most votes
- Most comments
Hello,
please try this solution it will be helpful for you.
To fetch specific data fields from an AWS Kendra response and control the default fields returned, you can utilize the AttributeFilter and ResponseFilter functionalities provided by AWS Kendra. With AttributeFilter, you can narrow down search results based on specific attributes, while ResponseFilter allows you to specify which document attributes should be included or excluded in the response.
For instance, if you're using the AWS SDK for Python (Boto3), you can define a query request with the desired ResponseFilter, indicating the specific fields you want to retrieve.
import boto3
# Initialize Kendra client
client = boto3.client('kendra')
# Define index ID and query text
index_id = 'your-index-id'
query_text = 'your-query-text'
# Specify the fields to include in the response
response_filter = {
'DocumentAttributeKeyList': ['Title', 'Excerpt', 'DocumentURI']
}
# Perform the query with the response filter
response = client.query(
IndexId=index_id,
QueryText=query_text,
AttributeFilter={},
RequestedDocumentAttributes=response_filter['DocumentAttributeKeyList']
)
# Extract and print the filtered response
for result in response['ResultItems']:
document_title = result.get('DocumentAttributes', {}).get('Title', {}).get('Value', '')
document_excerpt = result.get('DocumentAttributes', {}).get('Excerpt', {}).get('Value', '')
document_uri = result.get('DocumentAttributes', {}).get('DocumentURI', {}).get('Value', '')
print(f'Title: {document_title}')
print(f'Excerpt: {document_excerpt}')
print(f'URL: {document_uri}')
this example, RequestedDocumentAttributes is used to specify the desired fields (Title, Excerpt, DocumentURI) in the response. By customizing the RequestedDocumentAttributes, you can effectively control which data fields are included in the query response, tailoring it to your specific needs.
Relevant content
- asked 3 years ago
- asked 3 years ago
- asked a year ago
- asked 3 years ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 3 years ago