﻿---
title: Relevance
description: A relevance snapshot keeps how a search did on a set of judged queries. A judgement set says which pages answer each query. judy runs the set and scores...
url: https://docs-v3-preview.elastic.dev/formats/relevance
---

# Relevance
```csharp
var report = await RelevanceRun.RunAsync(file.Path, target);

report.Should().PassItsGates();
report.Should().MatchRelevanceSnapshot();
```

A relevance snapshot keeps how a search did on a set of judged queries. A judgement set says which pages answer each query. [judy](https://judy.nullean.net/) runs the set and scores it. The snapshot stores the scorecard and, for each query, the rank of its first relevant result.

## Run a set

`RelevanceRun.RunAsync` takes a `*.judgements.yaml` file and a judy target. A `DelegateTarget` wraps your own search code, so the test needs no cluster settings in judy.
```csharp
var target = new DelegateTarget("app", async (request, ct) =>
{
    var ids = await MySearch.FindAsync(request.Query, request.Size, ct);
    return new SearchOutcome([.. ids.Select(id => new Hit(id, null, null, null))], null);
});
```

A relative path is relative to the test project. `[RelevanceFiles]` gives one test case per judgement set under `relevance/`, as `[HttpFiles]` does for `.http` files.

## Fail on a gate

A judgement set can hold gates: numbers a target must reach. `PassItsGates()` fails when one does not, and shows judy's scorecard, the gates and the queries that found nothing.
```yaml
gates:
  app: {success@3: 0.7, ndcg@10: 0.6}
```


## Snapshot the ranks

`MatchRelevanceSnapshot()` stores the run under `.snapshots/relevance/{set}.{target}.verified.yml`. A query is one line, keyed by its id, and its positions are named once by a `columns` header:
```yaml
columns: [first, ndcg@10]
results:
  data stream retention: [1, 1]
  salon chair: [2, 0.6309]
  elasticsearch get started: [~, 0, stale]
  broken query: [~, 0, failed]
```

`first` is the rank of the first relevant result and `~` means there is none. The second position is the main metric, the first `ndcg@k`, which is what the paired test reads. A third position appears only for a query whose judged pages are all missing from the index (`stale`) or whose search failed (`failed`). A difference reads `$.results["salon chair"][0]: 2 → 5`, and the note above it says which position is which.

| Detail            | Keeps                                                                                    |
|-------------------|------------------------------------------------------------------------------------------|
| `Scorecard`       | the scores, rounded to three decimals                                                    |
| `Ranks` (default) | the scores, and for each query the rank of its first relevant result and the main metric |
| `Hits`            | everything in `Ranks`, and the returned documents with their grades                      |

The request hash, the retrieval mode and the text of an error are left out. They follow the machine, not the search.

### Noise passes, a regression fails

A live index moves for reasons that are not a code change. A person should not accept each move to keep a build green. So the format decides:
- A difference **passes** when it is within noise. The received file is written, a note prints the move, and `snap review` accepts it.
- A drop in the main metric **fails** when judy's paired randomization test calls it real (p < 0.05). The main metric is the first `ndcg@k`.
- A **change to the set** always fails: queries added or removed. So does a failed search, a changed metric list, and any difference in a `Scorecard` snapshot, which has no per-query data.

The note reads like this:
```text
ndcg@10 0.712 → 0.698 (-0.014): 3 better, 5 worse, 12 same, p = 0.31
Tolerated: the move is within noise. The received file is written; run snap review to accept it.
```

An improvement passes the same way, so you can accept the new ranks when you are ready.

## Judged pages that are not in the index

A judgement can name a page that is gone. It scores as a miss. Give `RunOptions.Presence` a lookup, and judy marks such queries as stale in the report and in the snapshot, so a stale judgement and a search regression look different. See the [judy docs](https://judy.nullean.net/reference/formats/#stale-judgements).

## Differences you expect

The `.differences` rules of [expected differences](https://docs-v3-preview.elastic.dev/workflow/expected-differences) do not apply to a relevance snapshot. The tolerance rule above takes their place for a live index.