RAG - Meta Filtering and Reranking
Generally, when a user asks a query, the system searches for the relevant chunks stored in the vector database using cosine similarity. The better we can filter the data, the smaller the search space becomes, resulting in faster and more efficient retrieval. Suppose we have a book with 10 chapters. If we want to search for a particular topic, all the points in the vector database are compared with the user query, and only the closest points are retrieved. This process is called KNN (K-Nearest Neighbors) . Another algorithm is ANN (Approximate Nearest Neighbors) . Instead of checking all the points in the vector database, ANN searches only within a smaller region based on the proximity of the data. As the name suggests, it does not always return the exact result, but it provides the most preferred or approximate results much faster. Is there any other method we can use to make the search more effective? Metadata Filtering Metadata means data about the data . Metadata is stored along with each chunk. It can contain information related to the chunk, such as the chapter name, topic description, author, or any other relevant details. When the user query contains information related to the metadata (for example, a chapter name or topic), the system can directly filter the relevant chunks before performing vector similarity search. This technique is called metadata filtering . Metadata filtering is supported by: Pinecone ChromaDB Qdrant FAISS does not provide built-in support for metadata filtering. Reranking Documents are first split into chunks, and each chunk is converted into vectors and stored in the vector database. When a user query arrives, it is converted into a vector and searched against the vector database to retrieve the closest chunks. However, we do not know whether the retrieved documents are actually the most relevant to the query. It is not always true that the closest vectors represent the most relevant documents. How Reranking Works The documents retrie