Key to the success of RAG systems is the quality (relevance) of the information retrieved. Traditionally, search systems have relied on keyword/phrase matching with BM25 (usually) being the mechanism to provide a relevance score to the search results. Research has indicated that BM25 provides a surprisingly good baseline to beat in benchmarks for retrieval systems. This mechanism of information retrieval is also called lexical or sparse model retrieval.
A typical drawback of phrase/keyword matching is that terms in the query have to be present in the document. While certain leeway can be provided by techniques such as stemming, it is still a keyword matching mechanism that does not take into account the semantic content or intent of the users’ queries. Semantic Retrieval using vector embeddings using, say, cosine similarity is a mechanism where we can encode the data in our corpus in N dimensions and identify documents that are the closest in terms of intent/concept to the user query which overcomes some of these limitations.
So we now have a couple of techniques on how to perform information retrieval –
- Lexical or Sparse Model search – leveraging BM25/TF-IDF mechanisms to perform keyword matching of user queries with documents
- Vector or Dense Model search – leveraging vector similarity on vector embeddings of data and the question to identify the most similar documents
Apart from using these mechanisms individually, we can also explore a few advanced mechanisms on how to put these two together
- Lexical pre-filter – Being able to apply lexical search and then rerank the lexical search results using a similarity search can help improve the relevance of the documents based on the user query
- Hybrid Search – here we leverage both search mechanisms individually taking advantage of their inherent strengths and then use a re-ranking algorithm to bring the results together
Both systems can suffer from drawbacks individually – Lexical search systems require that the user have used the same terms/jargon as what is found in relevant documents. They can also result in poorer searches because of the influence of the part of the query that is meant for the LLM versus information retrieval. Semantic search is influenced by the length of the document, domain specificity and other considerations. Research has shown that Hybrid search can improve the results of information retrieval more than any one approach. The idea being that a document that shows up in both search results is likely to be relevant depending on its relative ranking compared to other results thereby promoting relevant results and suppressing less relevant results.
Let’s take a look at how Hybrid search can be incorporated into Dataworkz.
Hybrid Search in Dataworkz
Dataworkz RAG Builder strives to make advanced RAG mechanisms easy to use by RAG Designers. Towards this, Dataworkz RAG Builder provides the following retrieval mechanisms –
- Lexical Pre-filter (Lexical first followed by Semantic based re-ranking)
- Semantic Only (only vector search is performed)
- Hybrid Search (Independent Lexical and Semantic Search followed by merging and re-ranking)
RAG Designers can take advantage of the powerful retrieval mechanism provided by Hybrid Search by just making a single selection of the retrieval mechanism to use.
How does Hybrid Search work in Dataworkz?
When data is ingested, data is stored in multiple indices –
- A sparse encoding such as a lexical search system (such as ElasticSearch, MongoDB, RedisSearch, etc)
- A dense encoding such as a embeddings (created vai mpnet, nomic, etc) and stored in a vector storage system (such as MongoDB, Pinecone, etc)
During retrieval, Dataworkz RAG Engine takes the users’ search query and performs multiple retrieval searches on different stores
- lexical search retrieval applies stop words removal and stemming and collect a list of relevant documents sorted by score
- Semantic search retrieval applies the similarity algorithm (e.g. cosine) to identify most similar documents sorted by score
Relevant documents returned by multiple retrieval systems have to be combined using some mechanism. A couple of approaches to this are –
- Reciprocal Rank Fusion (RRF) – a simple but effective mechanism that re-ranks search results taking into account their presence in multiple search results and their relative ranking. It has the advantage of not needing score normalization between different systems.
- Weighted sum of scores – a powerful mechanism that can combine results based on weights of scores from different systems
Dataworkz RAG engine performs RRF on document results and creates a combined retrieval mechanism. Weighted sum of scores can provide better results than RRF when the correct weights and other parameters are set up. However, the complexity of getting the right weights (sometimes per dataset) and parameters right can be challenging for users and negate the advantages over RRF. RRF is quite resilient and requires no setup from the user which significantly improves the success rate of Hybrid search using RRF.
Conclusion
Hybrid search is a powerful technique that combines the advantages of sparse (lexical) and dense (semantic) search mechanisms. Dataworkz RAG makes leveraging Hybrid search easy by pulling togehter all the search queries and combining them with the industry standard RRF algorithm. Context retrieval is the key to a successful RAG implementation and user experience. Hybrid search allows for a wider range of user queries to return successful responses. Hybrid Search capabilities are aligned with Dataworkz’ philosophy of helping RAG Designers be successful in their implementations by easily incorporating the latest advancements and best practices as well as making advanced techniques as easy to use as a click of a button. Dataworkz RAG will continue to look at improvements in this space to incorporate new research (publicly or our own), techniques and best practices to make Hybrid search results more relevant.