DEV Community

Cover image for Hybrid Search and Reranker in RAG: Enhancing Quality in Turkish
Mustafa ERBAY
Mustafa ERBAY

Posted on • Originally published at mustafaerbay.com.tr

Hybrid Search and Reranker in RAG: Enhancing Quality in Turkish

In Retrieval-Augmented Generation (RAG) systems, integrating Hybrid Search and Reranker mechanisms to find the most relevant information for user queries and generate accurate responses significantly improves quality, especially in languages with complex grammatical structures like Turkish. These approaches combine both the precision of keyword-based search and the contextual depth of semantic search, enabling more accurate retrieval and ranking of relevant document chunks. Due to the morphological richness and variety of word derivations in Turkish content, the combined use of these two techniques markedly enhances the performance of RAG models.

Traditional search methods are often insufficient due to challenges arising from Turkish's unique linguistic structure. For example, it is difficult to retrieve accurate information by keyword or vector-based search alone while preserving the semantic integrity of words derived from the same root with different suffixes or various sentence structures. Hybrid Search and Reranker offer comprehensive solutions to these problems, enabling RAG systems to produce more reliable and accurate responses.

Basic Search Methods and Their Limitations in RAG Architectures

At the core of RAG systems lies the retrieval process, which involves fetching document chunks relevant to a user query from a large corpus of information. This process is typically carried out using two main search methods: lexical (keyword-based) search and semantic search. Both methods have their unique advantages and limitations, especially when dealing with Turkish content.

Lexical search focuses on how often words from the query appear in documents, while semantic search attempts to capture the underlying meaning of words and sentences. Using these two approaches independently may not provide the desired accuracy and coverage for complex Turkish queries.

Lexical (Keyword) Search and Turkish-Specific Challenges

Lexical search is typically performed using algorithms like BM25 (Okapi BM25), which determine how relevant query terms are to documents. BM25 is a ranking function used in information retrieval systems to estimate the relevance of a document to a specific search query. This method is highly effective for queries containing specific names, product codes, or technical terms because it relies on exact or near-exact matches. However, in agglutinative languages like Turkish, different forms of words derived from the same root appear due to various suffixes.

For example, the words "kitap" (book), "kitaplar" (books), "kitapçı" (bookseller), "kitaplık" (bookcase) are derived from the same root with different suffixes, and lexical search cannot directly capture their semantic connection. If "kitaplar" appears in the query but "kitap" appears in the document, the relevance might decrease or no match might be found at all. This situation, due to Turkish's rich morphological structure, makes it difficult for simple keyword matching to fully reflect document relevance. Pre-processing steps like stemming and lemmatization partially solve this problem but do not always yield perfect results and can sometimes lead to loss of meaning.

Semantic Search and Contextual Understanding

Semantic search works by using embeddings that represent documents and queries in high-dimensional vector spaces. These vectors capture the contextual meanings of words and sentences. Semantically closest documents are found using metrics like cosine similarity between the query embedding and document embeddings. This method is very successful at finding documents that do not contain the exact words of the query but are semantically related.

For example, the query "What is the capital of Turkey?" can retrieve semantically relevant information even if the documents do not contain the word "Ankara". Multilingual embedding models or those specifically trained for Turkish can yield good results in this area. However, semantic search also has limitations. Sometimes very specific keywords or rare terms may not be represented distinctly enough in the vector space, or general semantic similarity may not provide the precision that specific keyword matching offers. Especially for technical or domain-specific terms, semantic search sometimes offers a general similarity, while lexical search can provide more accurate results.

What is Hybrid Search and Why is it Important for Turkish Content?

Hybrid Search is a search strategy that combines lexical (keyword-based) search and semantic (meaning-based) search methods to leverage the strengths of both approaches. This integration aims to overcome the limitations of methods used alone and significantly improves search quality, especially in content with complex structures like Turkish. Hybrid Search considers both the frequency and position of specific keywords in the query within documents, and retrieves documents relevant to the overall semantic context of the query.

This combination is a critical step for RAG systems to produce more comprehensive and accurate responses to user queries. Given that Turkish's morphological richness makes it difficult to correctly understand words derived with different suffixes and various sentence structures, Hybrid Search plays a vital role in overcoming these challenges.

How Hybrid Search Works

Hybrid Search typically follows these steps:

  1. Lexical Search: The user query is searched keyword-based on indexed documents using an algorithm like BM25. This step finds documents containing specific terms from the query and assigns them a score.
  2. Semantic Search: The same user query is transformed into a vector space via an embedding model. This vector is compared with the embeddings of document chunks to find the semantically closest documents, which are then assigned a score.
  3. Score Combination: The scores obtained from both lexical and semantic search results are combined using a specific weighting strategy (e.g., Reciprocal Rank Fusion - RRF). RRF is a method used to combine multiple result sets with different relevance indicators into a single result set. This combination ensures a final relevance score is obtained by leveraging the advantages of both methods.

You can see a visual flow of this process in the Mermaid diagram below:

Diagram

This flow ensures that the query is evaluated from both a keyword and semantic perspective, creating a richer and more accurate initial set of documents.

Importance of Hybrid Search in Turkish Content

Since Turkish is a language that can derive new words by adding different suffixes to word roots and exhibits flexibility in sentence structure, relying solely on keyword matching or semantic similarity is often insufficient.

  • Morphological Richness: Words like "gitmek" (to go), "gidiyor" (is going), "gitti" (went), "gidecek" (will go) take different suffixes to express different tenses and meanings. Lexical search may struggle to capture these variations at the root level, while semantic search can capture the similar meanings of different forms. Hybrid Search both searches for the exact word "gitti" and finds other texts semantically related to the root "gitmek".
  • Idioms and Metaphors: Idioms and proverbs are commonly used in Turkish, and there can be a difference between their literal and figurative meanings. Semantic search better captures the contextual meaning of such expressions, while lexical search will only look for a word-level match.
  • Synonym and Antonym Cases: In cases where different words have the same meaning (synonymy) or opposite meanings (antonymy), semantic search is more successful. Hybrid Search also considers these semantic relationships, offering a wider range of relevance.

Hybrid Search balances these complex grammatical challenges, enabling RAG systems to retrieve more accurate and contextually rich information in Turkish content. This significantly increases the quality and accuracy of responses generated by the RAG model.

Rerankers: How They Improve the Quality of Retrieved Documents

The initial set of documents retrieved by Hybrid Search, while combining semantic and lexical relevance, may still contain document chunks that are not fully relevant or have low relevance to the query. This is where Rerankers come into play. A Reranker analyzes the initially retrieved documents in more detail, re-evaluates their true relevance to the query, and re-ranks them from highest to lowest. This optimizes the number of documents sent to the Large Language Model (LLM) while maximizing the quality of the information provided.

Rerankers are a critical component of the RAG pipeline because they both increase precision and reduce the risk of LLM "hallucinations." Especially in nuanced languages like Turkish, Rerankers improve the accuracy of the information presented to the LLM by enabling a better understanding of grammatical structure and contextual subtleties.

Reranker Working Principle

The basic working steps of a Reranker are as follows:

  1. Initial Retrieval: A certain number of document chunks (e.g., the top 50-100) are retrieved using Hybrid Search or solely lexical/semantic search.
  2. Query-Document Pair Creation: Each retrieved document chunk is treated as a pair with the original user query.
  3. Detailed Relevance Assessment: The Reranker model (typically a Cross-Encoder or a more advanced Transformer-based model) examines each query-document pair individually. This model analyzes the interaction, semantic alignment, and contextual relevance between the query and the document much more deeply.
  4. Rescoring and Ranking: The model assigns a relevance score between 0 and 1 to each pair. Using these scores, the documents are re-ranked, and the most relevant ones are moved to the top of the list.
  5. Submission to LLM: Only the top N (e.g., top 5-10) document chunks from the re-ranked documents are presented to the LLM as context.

This process prevents unnecessary or irrelevant information from going to the LLM, while presenting the most accurate and concise information, directly improving response quality. Rerankers are particularly valuable when the initial set of retrieved documents is large or when relevance levels are close to each other.

Cross-Encoder Models and Turkish Performance

One of the most commonly used models as a Reranker is Cross-Encoders. A Cross-Encoder takes the query and the document simultaneously as input into a single model and directly evaluates the relationship between them. This requires more computational power than bi-encoders (like those used in semantic searches, which compare two separate embeddings) but offers much higher relevance accuracy.

The performance of Cross-Encoders for Turkish depends on the quality and size of the datasets they are trained on. Multilingual Cross-Encoder models (e.g., mBERT, XLM-R based) generally perform well on Turkish texts. However, if working with Turkish content specific to a certain domain (e.g., law, medicine, manufacturing), a Cross-Encoder fine-tuned with domain-specific data may yield better results than general-purpose models.

# Simplified Python example of Cross-Encoder Reranker usage
from sentence_transformers import CrossEncoder

# Load a Turkish or multilingual Cross-Encoder model
# This model is trained to evaluate the semantic relationship between a query and a document.
model = CrossEncoder('cross-encoder/mmarco-mMiniLMv2-L12-H384-v1') #cite: 23

query = "Türkiye'nin en kalabalık şehri neresidir?" # What is the most populous city in Turkey?
documents = [
    "Türkiye'nin başkenti Ankara'dır ve önemli bir ticaret merkezidir.", # Ankara is the capital of Turkey and an important trade center.
    "İstanbul, Türkiye'nin en büyük şehri ve kültür başkentidir. Nüfusu oldukça fazladır.", # Istanbul is Turkey's largest city and cultural capital. Its population is quite high.
    "İzmir, Ege Bölgesi'nde yer alan güzel bir liman şehridir.", # Izmir is a beautiful port city located in the Aegean Region.
    "Ankara, Türkiye'nin merkezi konumu nedeniyle idari bir öneme sahiptir." # Ankara has administrative importance due to its central location in Turkey.
]

# Calculate relevance score for each document with the query
# The model takes [query, document] pairs and produces a score
features = [[query, doc] for doc in documents]
scores = model.predict(features) #cite: 2, 14

# Rank documents by scores
ranked_documents = sorted(zip(documents, scores), key=lambda x: x[1], reverse=True)

print("Sıralanmış Belgeler:") # Ranked Documents:
for doc, score in ranked_documents:
    print(f"Skor: {score:.4f}, Belge: {doc}") # Score: {score:.4f}, Document: {doc}
Enter fullscreen mode Exit fullscreen mode

As seen in the example above, the Cross-Encoder considers each document along with the query to calculate a more nuanced relevance score. The document "İstanbul, Türkiye'nin en büyük şehri ve kültür başkentidir. Nüfusu oldukça fazladır." receives the highest relevance score for the query "en kalabalık şehir" (most populous city), while other documents receive lower scores. This helps the Reranker select the context to be sent to the LLM more accurately.

Practical Applications of Hybrid Search and Reranker Combination

The combined use of Hybrid Search and Reranker in RAG systems optimizes the information retrieval process, maximizing the quality of generated responses. This combination is an indispensable strategy, especially in enterprise applications working with large and heterogeneous Turkish datasets, customer service bots, knowledge management systems, and ERP integrations like production planning.

Integrating these two techniques is not just a theoretical approach; it requires understanding how to practically build a pipeline and which tools can be used.

Building an Integrated RAG Pipeline

A typical flow for combining Hybrid Search and Reranker in a RAG pipeline might be as follows:

  1. Data Preparation and Indexing:

    • Document Chunking: Large documents are broken down into meaningful chunks that fit within the LLM's context window.
    • Embedding Generation: An embedding vector is created for each document chunk and stored in a Vector Database.
    • Lexical Indexing: Documents are indexed for keyword-based search (e.g., in Elasticsearch or OpenSearch). Turkish-specific analyzers (stemmer, stop-word lists) can be used during this indexing.
  2. Query Processing and Hybrid Search:

    • The user query is received.
    • Both lexical search (e.g., Elasticsearch) and semantic search (Vector Database) are performed simultaneously.
    • Results and scores from both search methods are combined using a specific strategy (e.g., Reciprocal Rank Fusion - RRF). This step creates the initial set of documents to be retrieved.
  3. Re-ranking with Reranker:

    • The top N documents (e.g., 50-100) obtained from Hybrid Search are sent to a Cross-Encoder Reranker model.
    • The Reranker rescores and re-ranks each document based on its relevance to the user query.
    • After this stage, the top K documents (e.g., 5-10) with the highest scores are selected.
  4. Response Generation with LLM:

    • The selected K most relevant document chunks, along with the user query, are sent as context to an LLM (e.g., Gemini Flash, GPT-4).
    • The LLM uses this context to generate the final response to the user.

We can see a combined flow of these steps in the following pseudo-code example:

# Example RAG Pipeline Flow (Pseudo-code)

def run_rag_pipeline(query: str, lexical_indexer, vector_db, reranker_model, llm_model):
    # Step 1: Initial Document Retrieval with Hybrid Search
    print(f"Starting Hybrid Search for query: '{query}'...")

    # Lexical Search
    lexical_results = lexical_indexer.search(query, top_k=50) # Top 50 lexical results
    print(f"Lexical Search returned {len(lexical_results)} results.")

    # Semantic Search
    query_embedding = vector_db.create_embedding(query)
    semantic_results = vector_db.search(query_embedding, top_k=50) # Top 50 semantic results
    print(f"Semantic Search returned {len(semantic_results)} results.")

    # Combine Hybrid Search results (e.g., RRF)
    # This part may involve complex score combination logic
    combined_results = combine_hybrid_results(lexical_results, semantic_results) #cite: 4, 5, 6, 9
    print(f"After Hybrid Search, {len(combined_results)} unique documents obtained.")

    # Step 2: Re-ranking with Reranker
    if reranker_model:
        print("Re-ranking documents with Reranker...")
        documents_to_rerank = [doc.text for doc in combined_results]
        reranked_scores = reranker_model.predict([[query, doc_text] for doc_text in documents_to_rerank]) #cite: 2, 14

        # Map scores to documents and sort
        scored_documents = sorted(zip(documents_to_rerank, reranked_scores), key=lambda x: x[1], reverse=True)
        final_context_docs = [doc for doc, score in scored_documents[:10]] # Top 10 documents
        print(f"After Reranker, {len(final_context_docs)} documents will be sent to LLM.")
    else:
        final_context_docs = [doc.text for doc in combined_results[:10]] # If no Reranker, take top 10
        print(f"Reranker not used, {len(final_context_docs)} documents will be sent to LLM.")

    # Step 3: Generate Response with LLM
    context_str = "\n".join(final_context_docs)
    prompt = f"Using the following context, answer the question '{query}':\n\nContext:\n{context_str}\n\nAnswer:"

    print("Waiting for response from LLM...")
    response = llm_model.generate(prompt) #cite: 11, 17, 25, 27, 31, 33, 41
    return response

# In real integrations, libraries like LangChain, LlamaIndex abstract these steps. #cite: 10, 13, 15, 16, 18, 20, 26, 36, 38
# Example usage:
# response = run_rag_pipeline("What are the most common types of failures on the production line?",
#                             my_elastic_search_client, my_qdrant_client, my_reranker, my_llm)
# print(response)
Enter fullscreen mode Exit fullscreen mode

This pseudo-code illustrates the basic logic of a RAG pipeline. In the real world, libraries like LangChain or LlamaIndex abstract most of these steps, offering easier integration.

Optimization and Considerations for Turkish Content

When optimizing the use of Hybrid Search and Reranker in Turkish RAG systems, some specific points need attention:

  • Turkish Analyzers: If Elasticsearch or OpenSearch is used for lexical indexing, employing Turkish-specific analyzers (Turkish Analyzer) ensures more accurate stemming, lemmatization, and stop-word filtering. This improves the quality of keyword matching.
  • Turkish Embedding Models: For semantic search, Turkish or multilingual embedding models that perform well on Turkish should be preferred. For example, models like stsb-distilbert-base-multilingual-cased or newer, more powerful Turkish models developed by institutions like Hacettepe University and Marmara University can be used.
  • Reranker Fine-tuning: If working with Turkish content specific to a certain domain, fine-tuning the Reranker model on domain-specific datasets can significantly increase the accuracy of relevance assessment. This applies to specialized datasets such as ERP data from a manufacturing company or internal documentation from a bank.
  • Evaluation Metrics: When evaluating the performance of RAG systems, it is necessary to focus not only on response quality but also on the precision, recall of retrieved documents, and the faithfulness of the LLM's context usage. Due to the complexity of Turkish content, careful monitoring of these metrics is crucial for continuous system improvement.
  • Cost and Latency: Rerankers, being typically Cross-Encoder based, can require more computational power and thus introduce more latency compared to bi-encoders. In real-time applications, the cost-performance balance of the Reranker should be well-adjusted. Perhaps a smaller or faster Reranker model could be preferred.

These practical applications and optimization tips will help leverage the full potential of Hybrid Search and Reranker in developing Turkish RAG systems.

Optimization and Considerations for Turkish RAG Systems

When developing Retrieval-Augmented Generation (RAG) systems with Turkish content, specific strategies are needed to overcome challenges arising from the language's unique structure and to optimize system performance. The use of Hybrid Search and Reranker is an important part of these strategies, but additional considerations are also crucial for enhancing the overall effectiveness of the system. Data quality, model selection, cost-performance balance, and continuous monitoring are critical for a successful Turkish RAG system.

A RAG system must not only correctly integrate technical components but also consider the linguistic subtleties of Turkish. This enables the construction of a system that can produce both accurate and natural, fluent responses.

Data Quality and Pre-processing

The quality of the knowledge base (corpus) that forms the foundation of RAG systems directly affects the performance of the entire system. To improve data quality in Turkish content, the following should be considered:

  • Cleaning and Normalization: Text should be cleaned of spelling errors, punctuation, unnecessary spaces, and special characters. Especially in Turkish, factors such as case sensitivity and the correct use of vowels are important.
  • Turkish Tokenization and Stemming/Lemmatization: Turkish-specific tokenization and root-finding algorithms should be used during document indexing and chunking. This helps correctly match different inflected forms of words.
  • Semantic Chunking: Documents should be chunked not only by word count but also by preserving their semantic integrity. Instead of cutting in the middle of a paragraph or section, parts that form a meaningful whole should be treated as separate chunks.

Model Selection and Fine-Tuning

The Turkish performance of models used for Hybrid Search and Reranker is critically important:

  • Turkish Embedding Models: For query and document embeddings, models well-trained on Turkish or multilingual models that perform well on Turkish should be preferred. For example, Turkish BERT models from Hacettepe University or models developed by Marmara University.
  • Reranker Models: Cross-Encoders specifically fine-tuned for Turkish content or multilingual ones should be used. General-purpose English models may not fully capture the nuances of Turkish. Especially when working with domain-specific texts, fine-tuning the model with domain-specific data significantly improves performance.
  • LLM Selection: The LLM to be used with the RAG system should have high Turkish language proficiency and context understanding capabilities. Models like Gemini Flash, GPT-4 generally have good Turkish performance.

Cost, Latency, and Scalability

When deploying RAG systems in enterprise applications, the balance between cost and performance must be considered:

  • Computational Cost: Hybrid Search and Rerankers can require intensive computational resources, especially with large datasets. Using smaller and more optimized models or efficiently utilizing GPU resources can reduce costs.
  • Latency: In applications requiring real-time responses, the latency of each component (lexical search, semantic search, reranker, LLM) must be optimized. Parallel processing, caching, and appropriate hardware selection can help reduce latency.
  • Scalability: Horizontally scalable infrastructures (e.g., Docker containers on Kubernetes) should be used to allow the system to handle increasing user loads. Vector Databases and lexical search engines must also be resilient to high query volumes.

Monitoring and Improvement

A RAG system needs to be continuously monitored and its performance evaluated in a live environment:

  • Metrics: Retrieval metrics such as precision, recall, and F1 score, along with generation metrics such as the faithfulness, relevance, and fluency of LLM responses, should be regularly tracked.
  • User Feedback: User feedback is a valuable data source for improving the system. Which queries led to incorrect responses or which documents were irrelevant should be examined.
  • A/B Testing: Different Hybrid Search weighting strategies, Reranker models, or chunking methods can be evaluated with A/B tests to determine the best-performing configuration.

Future Trends and Development Directions

RAG systems are continuously evolving with rapid advancements in the field of artificial intelligence. Beyond techniques like Hybrid Search and Reranker, new trends and development directions exist that will further enhance the performance of Turkish RAG systems. These developments will enable us to create smarter, more flexible, and contextually aware systems.

Future RAG architectures will incorporate innovations that not only change document retrieval and ranking processes but also how LLMs access information and make decisions.

Adaptive Retrieval

Current RAG systems typically use a fixed retrieval strategy. However, adaptive retrieval aims to dynamically select or combine different search strategies based on the complexity of the query or the user's intent. For example, a fast semantic search might suffice for a simple information query, while a query requiring in-depth analysis might trigger multi-stage Hybrid Search and multiple Rerankers. Given the linguistic diversity of Turkish, this adaptability will be invaluable.

Multi-modal RAG

Today, RAG systems typically work with text-based data. However, in the future, multi-modal RAG systems that can retrieve and utilize information from different modalities such as images, audio recordings, videos, and other structured data will become more common. In a manufacturing ERP, being able to retrieve information not only from text-based product manuals but also from technical drawings or assembly videos of the product will provide much richer and more comprehensive responses. New techniques will be developed for indexing and accessing Turkish visual and audio data.

Agent Patterns and Autonomous RAG

Agent patterns enable LLMs not only to generate responses but also to act as autonomous "agents" that can perform specific tasks and interact with external tools (e.g., databases, APIs, calculators). In the context of RAG, an agent can analyze a query, decide which search strategy is most appropriate, retrieve information from different sources, process this information, and even ask additional questions to complete missing information. This will transform RAG systems from passive information retrievers into active problem solvers. These agents will be critical for automating more complex workflows in Turkish.

Advanced Prompt Engineering and RAG Integration

RAG systems are highly dependent on the quality of the prompt given to the LLM. In the future, more advanced prompt engineering techniques will be used to present retrieved information to the LLM more effectively. This means not just adding document chunks, but structured prompts that will enable the LLM to focus on specific information or help the LLM itself detect contradictions between retrieved information. Dynamically adjusting prompts according to the nuances of Turkish queries and contexts will further improve response quality.

These trends indicate that RAG technology will evolve from being merely an information retrieval tool to a central component of a more integrated, intelligent, and contextually aware artificial intelligence system. For Turkish content, these developments will open the door to RAG applications that can produce more complex and accurate responses and integrate deeply into business processes.

Conclusion

The use of Hybrid Search and Reranker in Retrieval-Augmented Generation (RAG) systems significantly improves the quality of the information retrieval process and, consequently, the accuracy of generated responses, especially in languages with rich grammatical structures like Turkish. Hybrid Search, a combination of lexical and semantic search, provides both keyword precision and semantic context depth, enabling a more comprehensive and relevant document set to be obtained in the initial stage. The subsequent Reranker then re-ranks this document set with a more detailed relevance assessment, selecting the highest quality and most concise information to be sent to the Large Language Model (LLM).

The integration of these two techniques plays a critical role in overcoming challenges arising from Turkish's morphological complexity, idiomatic expressions, and contextual nuances. They make RAG systems more reliable and useful across a wide range of applications, from enterprise solutions to personal projects. As developers, correctly implementing these techniques and performing Turkish-specific optimizations is indispensable for the success of AI-powered solutions. In the future, adaptive, multi-modal, and agent-based RAG approaches will further expand the capabilities of systems working with Turkish content.

Official Resources

Top comments (0)