Apply a metadata filter before retrieval

Read identifiers and quoted passages in short groups. · 176 words

Suppose a museum chatbot searches six event records. The user asks for Saturday events that are outdoors and suitable for children. Metadata is structured information attached to a record, such as audience=children or setting=outdoors. Before semantic retrieval, the system keeps only records where both fields match: E2, E4, and E6. It then compares the query with those three records and ranks E2 first, E4 second, and E6 third. Filtering first is called pre-filtering because it reduces the candidate set before the search algorithm searches.

By contrast, post-filtering searches all six records and removes nonmatches afterward. If the search returns only the top three, it might return E1, E2, and E3; removing E1 and E3 leaves only E2, even though E4 and E6 are valid. The chatbot should answer from E2, E4, and E6, not from records excluded by the request. Grounded generation means the final answer is supported by retrieved evidence. A useful design check is to print the candidate IDs before and after filtering and verify that every later citation belongs to the allowed set.

Check your recall

For the museum query, which event IDs survive the pre-filter, and what happens next?

Check your answer

E2, E4, and E6 survive. The retrieval system then compares the query with only those three candidates and ranks them.

Sources and credits

Original KeyTouch lesson. Background reading · Lewis et al. · Retrieval-Augmented Generation.