How to generate a JSON Filter for Amazon Bedrock
- Updated: 2024/09/11
How to generate a JSON Filter for Amazon Bedrock
Review this example to understand how you would create a JSON filter when using the Grounded by knowledge base Model connection to create an AI Skill.
Adding a filter in the Filter input field, when creating an AI Skill with Grounded by knowledge base Model connection helps narrow down the model's search to the specific content segment within a large document in the Amazon Knowledge Base. You would add a filter in the JSON format. To apply a filter, metadata should be configured for the data sources.
Lets go over an example scenario of creating and adding a JSON filter.
If you are using an Amazon S3 bucket, for every file in the bucket you must include .metadata.json file with the same name as the source document it's associated with. For example: If the bucket contains a file called articles.pdf, then you would create a metadata file with the name articles.pdf.metadata.json. The contents of a metadata file are generally key value blocks. For example, the content of articles.pdf.metadata.json could be as follows:
{
"metadataAttributes": {
"genre": "a360",
"year":2024
}
}
- Navigate to .
- Click on an Amazon Knowledge Base that was created.
- Navigate to on the left.
- In the Configurations screen, scroll down to Filters.
- Insert a filter criteria as shown below with the genre and year. Refer to
the screenshot for an OR condition.The JSON filter for this OR condition would be as follows:
{ "orAll": [ { "equals": { "key": "genre", "value": "a360" } }, { "equals": { "key": "year", "value": 2025 } } ] }
Similarly, for an AND condition, refer to the following screenshot:
The JSON filter for this AND condition would be as follows:{ "andAll": [ { "equals": { "key": "genre", "value": "a360" } }, { "equals": { "key": "year", "value": 2025 } } ] }
- To explore some examples for Metadata and filtering in Amazon Bedrock Knowledge Base. , see
- For additional examples in Amazon Bedrock, navigate to and .
To combine the filtering operations, use these logical operators:
To learn how to filter results using metadata, select the tab corresponding to your method of preference and follow these steps.
For Filter JSON, combine up to five filter groups by embedding them within another logical operator. You can create one level of embedding as follows:"retrievalConfiguration": { "vectorSearchConfiguration": { "filter": { "andAll | orAll": [ "andAll | orAll": [ "<filter-type>": { "key": "string", "value": "string" | number | boolean | ["string", "string", ...] }, "<filter-type>": { "key": "string", "value": "string" | number | boolean | ["string", "string", ...] }, ... ], "andAll | orAll": [ "<filter-type>": { "key": "string", "value": "string" | number | boolean | ["string", "string", ...] }, "<filter-type>": { "key": "string", "value": "string" | number | boolean | ["string", "string", ...] }, ... ] ] } } }
# genres = Strategy single_filter= { "equals": { "key": "genres", "value": "Strategy" } } # genres = Strategy AND year >= 2023 one_group_filter= { "andAll": [ { "equals": { "key": "genres", "value": "Strategy" } }, { "GreaterThanOrEquals": { "key": "year", "value": 2023 } } ] } # (genres = Strategy AND year >=2023) OR score >= 9 two_group_filter = { "orAll": [ { "andAll": [ { "equals": { "key": "genres", "value": "Strategy" } }, { "GreaterThanOrEquals": { "key": "year", "value": 2023 } } ] }, { "GreaterThanOrEquals": { "key": "score", "value": "9" } } ] }
Example:
{
"orAll": [
{
"andAll": [
{
"equals": {
"key": "genres",
"value": "Strategy"
}
},
{
"GreaterThanOrEquals": {
"key": "year",
"value": 2023
}
}
]
},
{
"GreaterThanOrEquals": {
"key": "score",
"value": "9"
}
}
]
}