- Newest
- Most votes
- Most comments
When working with Amazon Bedrock Knowledge Bases and handling large numbers of posts, the behavior you're experiencing with the Retrieve API is related to how relevance ranking works rather than strict ordering by metadata.
The issue you're facing occurs because the Retrieve API is primarily designed to return the most relevant results for a query, not necessarily to return results in a specific order based on metadata. When you request 100 results, Bedrock is returning what it considers the most relevant chunks across the entire dataset that match your filter criteria, which may include posts with higher PIDs.
For your use case of processing posts in order, here are some recommended approaches:
-
Use smaller batch sizes: Instead of requesting 100 results at once, consider using smaller batch sizes (e.g., 20-30) to reduce the likelihood of getting posts from across the entire range.
-
Implement more specific filtering: Modify your filter to include an upper bound on the PIDs for each batch:
{
andAll => [
{ equals => { key => "threadid", value => $threadid } },
{ greaterThanOrEquals => { key => "pid", value => $firstpid } },
{ lessThan => { key => "pid", value => $firstpid + 100 } }
]
}
Then increment the range for each subsequent batch.
- Post-process the results: After receiving results from the Retrieve API, sort them by PID on your side and track which PIDs you've already processed to avoid duplicates.
For incremental summarization, your approach should work well. When new posts are added, you can use a filter that only retrieves posts with PIDs greater than the last processed PID from your previous summarization. This ensures you're only processing the new content.
If you continue to face challenges with ordering and batching, you might consider implementing your own pagination logic by tracking the highest PID you've processed and using that as the starting point for subsequent batches, ensuring you don't miss any posts or process duplicates.
Sources
Query a knowledge base and retrieve data - Amazon Bedrock
Multi-tenancy in RAG applications in a single Amazon Bedrock knowledge base with metadata filtering | AWS Machine Learning Blog
Bedrock Agent with knowledgebase metadata search | AWS re:Post
answered 9 months ago

your earlier suggestion, I have implemented the following updates:
Used smaller batch sizes (numberOfResults => 20 or 30).
Added a filter with an upper PID bound:
{ andAll => [ { equals => { key => "threadid", value => $threadid } }, { greaterThanOrEquals => { key => "pid", value => $firstpid } }, { lessThan => { key => "pid", value => $firstpid + 100 } } ] }
Manually sorted the results by PID after retrieval.
However, I’m still facing the same issue mentioned earlier.
When there are around 150 posts in a thread and I request 20–30 results (e.g., numberOfResults => 30), Bedrock sometimes returns posts from across the entire PID range — for example, it may include a post with pid = 150 even in the first batch. As a result, when I try to fetch the next batch (for example, posts 31–60 based on firstpid + 100), the second batch either becomes empty or overlaps, since Bedrock already included higher PIDs in the first response.
To handle this, I tried modifying the filter as follows:
{ lessThan => { key => "pid", value => $firstpid + $numberOfResults } }
This approach works correctly when PIDs are strictly sequential (for example, 70187771, 70187772, 70187773, and so on). In that case, the first batch retrieves PIDs 70187771–70187800, and the next batch correctly starts from 70187801–70187830.
However, the problem arises because PIDs are globally auto-incremented and not topic-based. So if new posts are added in other threads, the PID sequenc