Skip to content

Enhance RRF ranking in HybridContentRetriever with source-aware scoring #345

Closed

Description

@haiphucnguyen

Background

The current HybridContentRetriever.reciprocalRankFusion() applies a uniform RRF formula across all retrieved content:

score(doc) = Σ 1 / (k + rank_i)

This treats results from all knowledge sources equally, regardless of their type (local_folders, local_files, urls) or any source-specific signals such as recency, trust level, or proximity to the query context.

Problem

KnowledgeSourceConfig already models distinct source types via its sealed subclasses:

  • LocalFoldersKnowledgeSourceConfig — watched directories (potentially stale if indexing is delayed)
  • LocalFilesKnowledgeSourceConfig — individual static files
  • UrlKnowledgeSourceConfig — crawled web pages (configurable depth/page count)
    However, reciprocalRankFusion() in HybridContentRetriever.kt discards source metadata entirely. All Content objects are keyed only by their text segment, and RRF scores are computed without distinguishing which source type produced them.

Proposed Improvement

Introduce source-aware score boosting into the RRF algorithm. Concretely:

  • Attach the originating KnowledgeSourceConfig type (or a normalized source weight) to each Content result at retrieval time (e.g., via TextSegment metadata).
  • In reciprocalRankFusion(), apply a configurable multiplier per source type:
adjusted_score = rrf_score * source_weight(sourceType)

Allow source weights to be defined in AppConfig.rag (e.g., sourceWeights: { local_files: 1.2, local_folders: 1.0, urls: 0.8 }).

Activity

  1. added
    enhancement
    on Apr 27, 2026
  2. deleted a comment from on Apr 30, 2026
  3. rcpeken commented on Oct 28, 2026

    @rcpeken
    Contributor

    Hi @haiphucnguyen, as discussed in #784, I'd like to take this one. I read through the current retrieval code first, and a few things shape the plan:

    Source type isn't available on local segments yet. URL-crawled segments carry source_type=url (UrlIndexingCoordinator), and live web results carry type=web (WebSearchContentRetriever). Local files and folders only have file_path, so reciprocalRankFusion() can't tell them apart. I'd add source_type (local_files / local_folders) at indexing time in the local coordinators and in FileChangeHandler, so watcher re-indexes keep it, and normalize web results to source_type=web.

    Already-indexed content. Existing segments won't have source_type until they're re-indexed, so a missing value would fall back to weight 1.0. Nothing changes for current users until a re-index.

    Config. A RagConfig.sourceWeights: Map<String, Double>, with every type defaulting to 1.0 so the default ranking stays identical to today.

    Weight scale. With k=60, RRF scores are very compressed (rank 1 vs rank 10 differ by only ~15%), so a 1.2 multiplier effectively outranks about 10 positions of relevance. That's why I'd keep neutral 1.0 defaults rather than the 1.2 / 1.0 / 0.8 from the description, unless you'd prefer opinionated defaults.

    Tests. There are no tests for HybridContentRetriever yet. I'd add unit tests with fake retrievers: weights of 1.0 reproduce today's ordering exactly, a boost reorders as expected, and missing metadata falls back to 1.0.

    A few questions before I start:

    1. Should the weights be editable in Advanced settings next to rankFusionConstant, or config-file only for now?
    2. Neutral 1.0 defaults, or do you want opinionated defaults?
    3. Should live web search results be in scope, or only indexed knowledge sources?
  4. haiphucnguyen commented on Oct 28, 2026

    @haiphucnguyen
    CollaboratorAuthor

    Thank you @rcpeken for taking this challenge issue!

    I re-read this ticket, and I think it solves a very small set of issues, the reason because in the real life users would rank the knowledge not base on the source types (local files, folders, web). But the real use case the prioritize the knowledge base on the specific resources. For instance, user will create a new resource collection about the project information in several folders:

    Image

    then they want to make the RAG ranking on the source code first, then in the document folders or the wiki page. So ranking the knowledge should be configured for the specific resource collection, not the global settings and based on the source type only.

    One thing we can configure global (or not?) that web search, or should we make the web search configuration in the resource collection too (I lean to this option too). I think we should keep this ticket is the discussion before finalizing what we should do :).

    In the meaning time, you can read the code of Askimo RAG, indexing, and Content retriever. We absolutely use them after we discuss about how we rank the resources.

  5. rcpeken commented on Oct 28, 2026

    @rcpeken
    Contributor

    Thanks @haiphucnguyen, that makes much more sense: priority per reference material inside a collection, not per source type. I went through the RAG code with that in mind, and the design gets simpler:

    Where it lives. Each reference material is already a KnowledgeSourceConfig stored in the collection's JSON. Adding a priority field there (default NORMAL) needs no DB migration, and existing collections keep today's ranking. It could be set from the ⋮ menu of each reference material in your screenshot.

    No re-index needed. Every chunk carries an absolute file_path (or url for URL sources), so at query time we can map a chunk back to its reference material: longest path prefix for folders, exact match for files and URLs. createRetrieverForContainer already has container.knowledgeSources; we'd pass the full configs into the retriever instead of just the identifiers.

    Same pipeline for Projects. Projects and resource collections share createRetrieverForContainer, so this would work for both. Server-managed (TEAM_MANAGED) collections retrieve remotely, so I'd leave them out.

    How strong a priority should be. "Source code first, then docs" can mean two things:

    • a strict order, where every chunk from a high-priority source ranks above every chunk from a normal one. The risk is that a weakly related code chunk beats the doc that actually answers the question.
    • a soft boost, where priority raises the score but relevance still counts.

    I'd go with a soft boost exposed as levels (High / Normal / Low) rather than raw numbers. RRF scores are very compressed, so raw weights are hard for users to reason about, and I'd tune what each level means with tests.

    Web search. Agree with moving it to the collection. One question is how it relates to the current per-session chip: should the collection setting replace the chip, or act as the default that the chip can still override per session?

    So the open points for you:

    1. Soft boost with levels, or a strict order?
    2. Web search: collection setting replaces the session chip, or is just the default?
    3. OK to include Projects' knowledge sources in the same change?

    Meanwhile I'll keep reading the indexing and retrieval code.

  6. haiphucnguyen commented on Oct 28, 2026

    @haiphucnguyen
    CollaboratorAuthor

    Thank you @rcpeken for quick reply. Aggree all your analysis about adding the priority field (we should have the default value is 1). The below part is interesting :)

    How strong a priority should be. "Source code first, then docs" can mean two things:
    
    a strict order, where every chunk from a high-priority source ranks above every chunk from a normal one. The risk is that a weakly related code chunk beats the doc that actually answers the question.
    a soft boost, where priority raises the score but relevance still counts.
    I'd go with a soft boost exposed as levels (High / Normal / Low) rather than raw numbers. RRF scores are very compressed, so raw weights are hard for users to reason about, and I'd tune what each level means with tests.
    

    Agree we do not have the fixed formula such as source code first then documentation later. We should make the opinionated score like your proposal that is calculated from the source priority, occurence. Not in scope: we can increase the precise of the query in the future by including more factors such as previous results (in memory), or knowledge graph, etc.

    Web search. Agree with moving it to the collection. One question is how it relates to the current per-session chip: should the collection setting replace the chip, or act as the default that the chip can still override per session? chip setting to select then override the collection. The reason I like to add the web configure in the resource collection because we only allow users select tools on-demand (to save their tokens), and if users do a research project we may prefer to use web search in all queries instead of select the web search in each query. However, we can separate this feature in the future ticket so you can focus in ranking issue.

    Below are the answers to your question:

    Soft boost with levels, or a strict order?

    Soft boost with levels

    Web search: collection setting replaces the session chip, or is just the default?

    Let do in the following up ticket

    OK to include Projects' knowledge sources in the same change?

    in fact, the project and resource collection are pretty similar, and they use the same code for indexing, query, etc. I leave the decision up to you if you think the changes in resource collection is complex enough for one PR then we can do for Project in the next ticket.

  7. haiphucnguyen commented on Oct 28, 2026

    @haiphucnguyen
    CollaboratorAuthor

    Actually we can support the strict order via soft boost if we adjust the proper parameters and value when we calculate scoring. We should allow this option though it is not the default choice.

  8. rcpeken commented on Oct 29, 2026

    @rcpeken
    Contributor

    Thanks @haiphucnguyen! Here's the plan for this ticket, including the strict option:

    • Priority field. priority on KnowledgeSourceConfig as a numeric weight with default 1.0. The UI exposes it as High / Normal / Low levels mapped to fixed weights, so the stored value stays a plain number.
    • Balanced ranking (default). Weighted RRF: each chunk's contribution becomes priority / (k + rank), so occurrence across the vector, keyword and web lists still adds up, and priority shifts relevance without overriding it.
    • Strict ranking (option). Results are ordered by priority level first, then by the same RRF score within a level. That's equivalent to a very large boost, but explicit and easier to test. I'd make it a per-collection setting ("Ranking: Balanced / Strict"), since whether strict order makes sense depends on the collection's content. Does that location work for you, or would you prefer a global setting?
    • Matching chunks to sources. Longest path prefix for folders, exact match for files and URLs. Web results and unmatched chunks keep weight 1.0, so no re-index is needed.
    • Projects included. knowledgeSourcesPanel and the retrieval pipeline are shared, so leaving Projects out would actually take extra code to hide the option there.
    • Out of scope: web search per collection (follow-up ticket) and extra ranking signals like memory or a knowledge graph.

    I'll start on this once you confirm where the strict option should live.

  9. haiphucnguyen commented on Oct 29, 2026

    @haiphucnguyen
    CollaboratorAuthor

    Thanks @rcpeken for the very detail plan!

    From my understanding, we do not have Balanced ranking or Strict ranking, but we ranking base on the weight value of the resource config instance. The prioritize the search content in the below code

    val sortedKeys = scoreMap.entries
                .sortedByDescending { it.value }
                .map { it.key }

    to

    val sortedKeys = scoreMap.entries
        .sortedByDescending { it.value * weightFor(it.key) }
        .map { it.key }

    we can have the list of weight for resource configs, such as Low=0.5, Normal=1.0, High=2.0, "Always prioritize"=100 . If user choose Always prioritize then the result is dominated by that resource config.

    If we prefer to let prioritze the web search result (in next ticket), we can weight for the webResults the higher value to make sure it dominate the result.

    So in the resource collection screenshot, we add one more field is priority which has the values are Low=0.5, Normal=1.0, High=2.0, "Always prioritize"=100 (you can adjust the value, that is the parameter we use to calculate the weight score).

    Let me know if you have any question.

  10. added a commit that references this issue on Oct 30, 2026
    fded9b5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementrag

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions