Skip to main content

How Ranking Works

A ranking expression determines which documents are scored and how they’re ordered:

Expression Evaluation Process

  1. No ranking (rank=None): Documents are returned in index order (typically insertion order)
  2. With ranking expression:
    • Must contain at least one Knn expression
    • Documents must appear in at least one Knn’s top-k results to be considered
    • Documents must also appear in ALL Knn results where default=None
    • Documents missing from a Knn with a default value get that default score
    • Each Knn considers its top limit candidates (default: 16)
    • Documents are sorted by score (ascending - lower scores first)
    • Final results based on Search.limit()

Document Selection and Scoring

The Knn Class

The Knn class performs K-nearest neighbor search to find similar vectors. It’s the primary way to add vector similarity scoring to your searches.

Knn Parameters

Query Formats

Text Queries

Dense Vectors

Sparse Vectors

Embedding Fields

Chroma currently supports:
  1. Dense embeddings - Stored in the default embedding field ("#embedding" or K.EMBEDDING)
  2. Sparse embeddings - Can be stored in metadata under a consistent key

Arithmetic Operations

Supported operators:
  • + - Addition
  • - - Subtraction
  • * - Multiplication
  • / - Division
  • - (unary) - Negation
Combine ranking expressions using arithmetic operators. Operator precedence follows Python’s standard rules.

Mathematical Functions

Supported functions:
  • exp() - Exponential (e^x)
  • log() - Natural logarithm
  • abs() - Absolute value
  • min() - Minimum of two values
  • max() - Maximum of two values

Val for Constant Values

The Val class represents constant values in ranking expressions. Numbers are automatically converted to Val, but you can use it explicitly for clarity.

Combining Ranking Expressions

You can combine multiple Knn searches using arithmetic operations for custom scoring strategies.

Understanding Scores

  • Lower scores = better matches - Chroma uses distance-based scoring
  • Score range - Depends on your embedding model and distance metric
  • No ranking - When rank=None, results are returned in natural storage order
  • Distance vs similarity - Scores represent distance; for similarity, use 1 - score (for normalized embeddings)

Edge Cases and Important Behavior

Default Ranking

When no ranking is specified (rank=None), results are returned in index order (typically insertion order). This is useful when you only need filtering without scoring.

Combining Knn Expressions with default=None

Documents must appear in at least one Knn’s results to be candidates, AND must appear in ALL Knn results where default=None.

Vector Dimension Mismatch

Query vectors must match the dimension of the indexed embeddings. Mismatched dimensions will result in an error.

The return_rank Parameter

Set return_rank=True when using Knn with RRF to get rank positions (0, 1, 2…) instead of distances.

The limit Parameter

The limit parameter in Knn controls how many candidates are considered, not the final result count. Use Search.limit() to control the number of results returned.

Complete Example

Here’s a practical example combining different ranking features:

Tips and Best Practices

  • Normalize your vectors - Ensure consistent scoring by normalizing query vectors
  • Use appropriate limit values - Higher limits in Knn mean more accurate but slower results
  • Set return_rank=True for RRF - Essential when using Reciprocal Rank Fusion
  • Test score ranges - Understand your model’s typical score ranges for better thresholding
  • Combine strategies wisely - Linear combinations work well for similar score ranges

Next Steps