Skip to content

Enhance semantic search scoring to account for multiple matching chunks #778

Closed

Description

@malibio

Problem

Currently, semantic search groups embeddings by node and returns math::max(similarity) as the relevance score. This means a document is scored by its single best-matching chunk only.

Current query logic (surreal_store.rs:4666-4679):

SELECT
    node,
    math::max(vector::similarity::cosine(vector, $query_vector)) AS similarity
FROM embedding
WHERE stale = false AND vector <|{knn_limit}|> $query_vector
GROUP BY node

This doesn't capture breadth of relevance - a document with 5 chunks all scoring 0.8 is arguably more relevant than a document with 1 chunk scoring 0.85.

Proposed Enhancement

Improve the scoring algorithm to consider multiple matching chunks. Options:

  1. Count matches: Include number of chunks above threshold in scoring
  2. Average top-N: Average the top 3 chunk scores instead of just max
  3. Weighted composite score: max_score * (1 + log(matching_chunks))
  4. Sum with decay: Sum of chunk scores with diminishing weight for lower-ranked chunks

Example

Document Chunk Scores Current Score Proposed Score (option 3)
Doc A [0.85] 0.85 0.85 * 1.0 = 0.85
Doc B [0.80, 0.78, 0.75, 0.72, 0.70] 0.80 0.80 * 1.70 = 1.36

Doc B has broader relevance and should rank higher.

Implementation Notes

  • The change would be in SurrealStore::search_embeddings()
  • May need subquery to count/aggregate chunks before final grouping
  • Should maintain backward compatibility (threshold and limit params unchanged)
  • Consider adding optional parameter to choose scoring strategy

Acceptance Criteria

  • Semantic search scoring accounts for multiple relevant chunks
  • Documents with broader relevance rank higher than single-chunk matches
  • Performance remains acceptable (benchmark before/after)
  • Optional: expose scoring strategy as parameter

Activity

  1. added
    enhancement
    on Dec 22, 2025
  2. malibio commented on Dec 23, 2025

    @malibio
    CollaboratorAuthor

    Additional Enhancement: BM25 Hybrid Search

    Beyond multi-chunk scoring, we should consider hybrid search combining semantic similarity with BM25 keyword matching.

    Problem with Pure Semantic Search

    A query like "testing concurrency investigation" only scores 0.65 against a document titled "Test Suite Concurrency Investigation" - despite the near-exact title match. This happens because:

    • The title gets diluted by all the body content in chunked embeddings
    • Semantic search doesn't give extra weight to exact keyword matches

    Proposed Solution: Hybrid BM25 + Semantic

    final_score = (alpha * semantic_score) + ((1 - alpha) * bm25_score)
    

    Where:

    • semantic_score: Current vector cosine similarity
    • bm25_score: SurrealDB full-text search score (exact/fuzzy keyword matching)
    • alpha: Configurable weighting (e.g., 0.7 semantic, 0.3 BM25)

    Implementation Options

    1. SurrealDB native full-text search

      • DEFINE ANALYZER with tokenizers/filters
      • DEFINE INDEX ... SEARCH ANALYZER
      • search::score() function for BM25 ranking
    2. Title boosting (simpler alternative)

      • Prepend title multiple times when aggregating content for embedding
      • Quick fix but less flexible

    Benefits

    • Exact keyword matches in titles score much higher
    • Still captures semantically related documents without keyword overlap
    • User-tunable weighting via alpha parameter

    References

  3. malibio commented on Dec 23, 2025

    @malibio
    CollaboratorAuthor

    Test baseline: Frontend 3431 passed, Rust 674 passed

  4. self-assigned this
    on Dec 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

aienhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions