Skip to content

Bedrock knowledge base filtering on string / text fields returns error - "failed to create query: Field 'name.keyword' not found Rewrite first"

1

I am trying to use the bedrock knowledge base filtering functionality.

I am now getting for every possible filtering combination using a string as value (i.e. title = "Dogs") the following response:

  • "failed to create query: Rewrite first" (in red) if testing the console
  • "botocore.errorfactory.ValidationException: An error occurred (ValidationException) when calling the Retrieve operation: failed to create query: Rewrite first" when testing via cli/boto3.

My Knowledge-Base is correctly set-up and synced, filtering by numeric values works just fine. I can still filter a string-field with an int-value (receving an empty list as response) and I can still successfully filter number-fields with int-values receiving results from my database. Additionally, also the filtering by string-values used to work before and only stopped working recently.

To be more precise on this aspect: I had a collection with an older and a younger index (created May 20th and May 30th). The younger index was a superset of the older one, containing some additional string fields. I used the younger index in my knowledge base - only the fields also existent in the older index of the same OSS collection were filterable by strings (returning results) while those only added in the younger collection returned the exception. This seemed very odd or to potentially be a coincidence to me.

To make sure that no set-up / initialization relicts were causing this error, I set up an entirely new OSS collection + index (with identical fields as the "young index" from before) and a new bedrock knowledge base - and syncing the exact same datasource bucket as before (i.e. non of the metadata attribute-values has changed + before at least some fields mapped into the "old index" could be filtered by strings). Now, however, none of the string-fields are filterable with strings, not even those which had worked in the other kb (with the identical setup).


I am using boto3==1.34.116 and previously used 1.34.90, both behaved the same way + I also tried filtering in the console via browser with the same results.

Related issues are: https://repost.aws/es/questions/QU3eHU3zRATX6sYPuWVEHGsw/issue-with-metadata-filtering-in-amazon-bedrock?sc_ichannel=ha&sc_ilang=en&sc_isite=repost&sc_iplace=hp&sc_icontent=QU3eHU3zRATX6sYPuWVEHGsw&sc_ipos=13 (here, the author encountered the same behavior, however, figured they needed to filter for numbers anyways so I didn't matter anymore).

It also seems very much related to this: https://github.com/opensearch-project/k-NN/issues/1286

  • Hi, I may not be able to give you the exact answer you are looking for, but recently we did a hackathon based on this workshop. The participants extended the solution with their own knowledge base and metadata - it worked very well for all the participants. I would suggest you compare your code/steps with this, hopefully you may be able to find a work-around. I would suggest to raise the main issue with AWS support, though. Thanks!

  • Hi, did you ever resolve this?

  • @Adam Weinberger: No, I did not. I eventually resorted to numerical replacements for these columns.

  • I am also seeing the same issues through the console and boto3. Filtering on strings does not work and always throws the same error mentioned above. Seems like this is a problem on the AWS side. AWS needs to fix this

2 Answers
0
Accepted Answer

Working on a different project today, I believe I have finally been able to solve the issue myself (which per default still exists).

By now, the error message has gotten slightly more verbose: from ""failed to create query: Rewrite first" to "failed to create query: Field 'path.keyword' not found. Rewrite first" - where "path" is the name of the field I tried to filter. This change can be correlated to an older MR in the OSS-library, where developers figured some error-messages weren't quite helpful. Arguably, the MR did indeed increase the response's usefulness.

Recreating the problem:

  • creating a Bedrock Knowledge Base using a self-generated AWS OpenSearch Serverless-collection and -index as default-collection-generation won't create metadata-fields (or does it? I believe there is no such option yet)
  • trying to query this KB using any filter on any text-field fails with above error message

Cause: can be deduced from this thread https://forum.opensearch.org/t/document-fields-do-not-always-get-keyword-entries/16474 .

--> Basically: the text-fields are lacking "keyword"-subfields for indexing

Solution / Workaround: create your index in the AOSS-collection using json-mode and make sure all text-fields which you want to filter on contain the necessary "keyword"-subfield. See below example for a fully functional json to create an index with 3 text-fields.

Example:

  • make sure to include a non-filterable field for the bedrock-metadata !
  • make sure to use the correct vector index settings (these ones are apt for using Titan Embeddings Text V2 and FAISS)
  • in this example, the text-fields which shouild contain metadatae are "name" and "path" - and "description" is going to be my text-field for inputs (also filterable though)

{
    "settings": {
        "index.knn": "true",
        "number_of_shards": 1,
        "knn.algo_param.ef_search": 512,
        "number_of_replicas": 0
    },
    "mappings": {
        "properties": {
            "embeddings": {
                "type": "knn_vector",
                "dimension": 1024,
                "method": {
                    "name": "hnsw",
                    "engine": "faiss",
                    "space_type": "l2"
                }
            },
            "description": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "bedrock-metadata": {
                "type": "text",
                "index": "false"
            },
            "name": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "path": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            }
        }
    }
}

answered 2 years ago

0

I did face this error message when I explicitly defined the filter field under the OpenSearch serverless index when configuring the metadata mapping fields. To fix the problem, I recreated the index and allowed Bedrock Knowledge base to add the filter mapping field automatically when tagging the metadata.json for the documents ingested into the Knowledgebase. Now I am able to apply the filters and it works all good.

AWS

answered 2 years ago

  • thanks for your reply back then, @nihilson. How did you allow BR KB to add filter mapping fields automatically? I havn't been able to do that yet. I did find another solution by now though (see accepted answer)...

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.