Skip to content

[BUG] Hybrid search ORDER BY RANK RRF throws IndexOutOfBoundsException — coalescedAndSortedResults Mono subscribed twice without .cache() #49823

Description

@MarioPadilla

Describe the bug

Cross-partition hybrid search queries (ORDER BY RANK RRF(VectorDistance(...), FullTextScore(...))) intermittently throw IndexOutOfBoundsException at HybridSearchDocumentQueryExecutionContext.lambda$computeRRFScores$3, e.g.:

java.lang.IndexOutOfBoundsException: Index 363 out of bounds for length 363
    at java.util.ArrayList.get(ArrayList.java:428)
    at com.azure.cosmos.implementation.query.HybridSearchDocumentQueryExecutionContext.lambda$computeRRFScores$3(HybridSearchDocumentQueryExecutionContext.java:343)

This is distinct from #46267 (already fixed via CosmosFullTextScoreScope) — it still reproduces with CosmosFullTextScoreScope.LOCAL set, and is unrelated to full-text statistics scope.

Root cause

In hybridSearch(...), coalescedAndSortedResults (a Mono<List<HybridSearchQueryResult<Document>>>) is built once but subscribed to twice independently, without .cache():

Mono<List<HybridSearchQueryResult<Document>>> coalescedAndSortedResults = coalesceAndSortResults(componentQueryResults);
Mono<List<List<ScoreTuple>>> componentScoresList = retrieveComponentScores(coalescedAndSortedResults, componentWeights); // subscription #1
Mono<List<List<Integer>>> ranks = computeRanks(componentScoresList);
return computeRRFScores(ranks, coalescedAndSortedResults, componentWeights) // subscription #2, via zipWith

Since the upstream source merges results across many partitions over real network I/O, each independent subscription re-executes the whole cross-partition merge and can non-deterministically produce a different-sized result list. computeRRFScores then indexes into a ranks list sized from the other subscription, causing the exception. Confirmed still present on main (checked against HybridSearchDocumentQueryExecutionContext.java on the default branch as of 2026-07-15).

Suggested fix

Add .cache() to coalescedAndSortedResults so both consumers observe the same materialized result list:

Mono<List<HybridSearchQueryResult<Document>>> coalescedAndSortedResults = coalesceAndSortResults(componentQueryResults).cache();

To Reproduce

Cross-partition hybrid search with k large enough (empirically ~≥90-100 against our container) to increase the window for the two subscriptions' network timing to diverge. Not reliably reproducible with a small local dataset — occurs intermittently in production against a multi-partition container under normal query load.

Environment

  • azure-cosmos 4.80.0
  • Java 25
  • Spring Boot 4.0.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    ClientThis issue points to a problem in the data-plane of the library.CosmosService AttentionWorkflow: This issue is responsible by Azure service team.customer-reportedIssues that are reported by GitHub users external to the Azure organization.needs-team-attentionWorkflow: This issue needs attention from Azure service team or SDK teamquestionThe issue doesn't require a change to the product in order to be resolved. Most issues start as that

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions