DocumentationNeurondB Documentation

Cross-Encoder Reranking

Overview

Neural reranking models for improved relevance.

Rerank Results

Rerank search results with cross-encoder models. Cross-encoders process query-document pairs together for more accurate relevance scoring than bi-encoders.

Rerank search results

-- Rerank search results using cross-encoder
-- Returns: table with (idx, score) where idx is array index, score is relevance
SELECT 
    idx, 
    score 
FROM rerank_cross_encoder(
    'What is machine learning?',           -- query text
    ARRAY[                                  -- candidate document texts
        'Neural networks tutorial',
        'Deep learning basics',
        'AI history'
    ],
    'ms-marco-MiniLM-L-6-v2',              -- model name (optional, NULL for default)
    3                                       -- top K results to return
)
ORDER BY score DESC;

Function Signature:

rerank_cross_encoder( query TEXT,              -- Query text documents TEXT[],        -- Array of candidate document texts model_name TEXT,         -- Optional model name (NULL for default) top_k INTEGER           -- Number of top results to return ) RETURNS TABLE ( idx INTEGER,            -- Index in original documents array score REAL              -- Relevance score (higher = more relevant) )

Common Models:

  • ms-marco-MiniLM-L-6-v2: Fast, lightweight model (default)
  • cross-encoder/ms-marco-MiniLM-L-12-v2: Higher accuracy, slower
  • cross-encoder/ms-marco-electra-base: Best accuracy, slowest

Batch Reranking

Rerank multiple queries efficiently in a single batch operation. Batch processing is faster than individual calls.

Batch reranking

-- Batch reranking for multiple queries
SELECT 
    query_id, 
    idx, 
    score
FROM rerank_cross_encoder_batch(
    ARRAY['query 1', 'query 2'],           -- Array of queries
    ARRAY[                                  -- Array of document arrays (one per query)
        ARRAY['doc 1', 'doc 2'],
        ARRAY['doc 3', 'doc 4']
    ],
    'model_name',                           -- Optional model name
    5                                       -- top K per query
)
ORDER BY query_id, score DESC;

Function Signature:

rerank_cross_encoder_batch( queries TEXT[],         -- Array of query texts documents TEXT[][],     -- Array of document arrays (one per query) model_name TEXT,        -- Optional model name top_k INTEGER          -- Top K results per query ) RETURNS TABLE ( query_id INTEGER,      -- Index of query in queries array idx INTEGER,           -- Index in documents array for that query score REAL             -- Relevance score )

Flash Attention Reranking

For long contexts, use Flash Attention reranking which is optimized for efficiency:

Flash reranking

-- Flash Attention reranking (optimized for long contexts)
SELECT 
    idx,
    score
FROM rerank_flash(
    'machine learning',                    -- query text
    ARRAY[                                  -- candidate documents
        'machine learning algorithms',
        'deep learning models',
        'neural networks'
    ],
    NULL,                                   -- model name (NULL for default)
    3                                       -- top K
)
ORDER BY score DESC;

-- Long context reranking
SELECT 
    idx,
    score
FROM rerank_long_context(
    'query text',
    ARRAY['document 1', 'document 2', 'document 3'],
    8192,                                   -- max context length
    3                                       -- top K
)
ORDER BY score DESC;

Function Signatures:

rerank_flash( query TEXT, documents TEXT[], model_name TEXT, top_k INTEGER ) RETURNS TABLE (idx INTEGER, score REAL) rerank_long_context( query TEXT, documents TEXT[], max_length INTEGER,    -- Maximum context length top_k INTEGER ) RETURNS TABLE (idx INTEGER, score REAL)

Learn More

For detailed documentation on cross-encoder models, model selection, fine-tuning, and performance optimization, visit: Cross-Encoder Documentation

Related Topics