How to generate a JSON Filter for Amazon Bedrock

Review this example to understand how you would create a JSON filter when using the 기술 자료에 기반함 모델 연결 to create an AI 기술:.

Adding a filter in the Filter input field, when creating an AI 기술: with 기술 자료에 기반함 모델 연결 helps narrow down the model's search to the specific content segment within a large document in the Amazon 기술 자료. 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
    }
}
You can generate the JSON filter following these steps:
  1. Navigate to Amazon Bedrock > Knowledge Bases.
  2. Click on an Amazon 기술 자료 that was created.
  3. Navigate to Test Knowledge Base > Configurations on the left.
  4. In the Configurations screen, scroll down to Filters.
  5. Insert a filter criteria as shown below with the genre and year. Refer to the screenshot for an OR condition.

    AWS JSON filter with genre and year 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:

    AWS JSON filter with genre and year for an AND condition

    The JSON filter for this AND condition would be as follows:
    {
      "andAll": [
        {
          "equals": {
            "key": "genre",
            "value": "a360"
          }
        },
        {
          "equals": {
            "key": "year",
            "value": 2025
          }
        }
      ]
    }  
  6. To explore some examples for Metadata and filtering > Filtering Operators and Logical Operators, see Metadata and filtering in Amazon Bedrock Knowledge Base.
  7. For additional examples in Amazon Bedrock, navigate to Configure and customize queries and response generation > Console and Configure and customize queries and response generation > API.

    AWS filtering operators

    To combine the filtering operations, use these logical operators:

    AWS 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:

AWS JSON filter example

See the JSON representation as follows: Amazon Bedrock Knowledge Bases now supports metadata filtering to improve retrieval accuracy
{
    "orAll": [
        {
            "andAll": [
                {
                    "equals": {
                        "key": "genres",
                        "value": "Strategy"
                    }
                },
                {
                    "GreaterThanOrEquals": {
                        "key": "year",
                        "value": 2023
                    }
                }
            ]
        },
        {
            "GreaterThanOrEquals": {
                "key": "score",
                "value": "9"
            }
        }
    ]
}