﻿---
title: Lexical, semantic and hybrid
description: Metrics only look at the ranked list, so they score every retrieval mode alike. judy adds one more fact: how each query was answered. You can then ask...
url: https://docs-v3-preview.elastic.dev/concepts/modes
---

# Lexical, semantic and hybrid
Metrics only look at the ranked list, so they score every retrieval mode alike. judy adds one more fact: *how* each query was
answered. You can then ask which mode does better, for which queries, and whether routing pays off.
**A router** decides, for each query, how to search it. A common router sends short keyword queries to lexical search, and longer natural-language queries to semantic or hybrid search. It might be an `if` in your application, a classifier, or a rule you are considering. In judy a router is a [builder](https://docs-v3-preview.elastic.dev/workflow/dynamic-queries) target: your code reads the query and returns the request.
```mermaid
flowchart LR
    Q["Query"] --> R{"Router"}
    R -->|"short"| L["Lexical<br/>match the words"]
    R -->|"long"| S["Semantic<br/>match the meaning"]
    R -->|"either"| H["Hybrid<br/>both, fused"]
    L --> Res["Results"]
    S --> Res
    H --> Res
```

The [glossary](https://docs-v3-preview.elastic.dev/concepts/glossary) defines *mode* (how a search matched) and *oracle* (the best score any router can reach).

## How judy knows the mode

judy works the mode out from the request it sent:

| Request                                                                                                 | Mode     |
|---------------------------------------------------------------------------------------------------------|----------|
| a `semantic`, `knn`, `sparse_vector`, `text_expansion` or `rank_vectors` query                          | semantic |
| a `match`, `multi_match`, `combined_fields`, `query_string` or `simple_query_string` on ordinary fields | lexical  |
| an `rrf` retriever (or a request that is both)                                                          | hybrid   |
| ES|QL `KNN(...)`, `TEXT_EMBEDDING(...)`, `SPARSE_VECTOR(...)`                                           | semantic |
| ES|QL `QSTR(...)`, `KQL(...)`, `MATCH(field, ...)`                                                      | lexical  |
| ES|QL `\| FUSE`                                                                                         | hybrid   |

A `match` on a `semantic_text` field looks lexical in the request, but it is a semantic query. Tell judy which fields are semantic
in `judy.yaml`:
```yaml
modes: {semantic_fields: [product_semantic]}
```

A [builder](https://docs-v3-preview.elastic.dev/workflow/dynamic-queries) can also state its mode (`"mode": "semantic"` next to the `dsl`), and that always wins
over inference.

## Automatic groups

Every scorecard and results file reports each metric per group, next to your own tags:
- `mode:lexical`, `mode:semantic`, `mode:hybrid`
- `words:1`, `words:2`, `words:3+`, the query length in words

```text
tag words:1
  ndcg@10    0.900
…
tag words:3+
  ndcg@10    0.566
```

On WANDS a one-word query scores 0.900 with lexical search, and a three-word query scores 0.566. You see where the search is
weak without asking.

## `judy modes`: is routing worth it?

"Only go semantic above two words" is a plausible router. `judy modes` runs targets that each retrieve one way, and
optionally a router, and compares them query by query. You see if routing beats always using the best mode:
```text
$ judy modes wands.judgements.yaml --target lexical --target semantic --target hybrid --router router
ndcg@10 over 479 queries

  lexical                  0.595  (lexical)
  semantic                 0.641  (semantic)
  hybrid                   0.666  (hybrid)
  oracle (best per query)  0.733
  router (router)          0.631

router vs oracle: -0.102 (perfect routing would add 0.102)
router vs best single target (hybrid): -0.036
routed by mode:
  lexical      131 queries  0.669
  semantic     348 queries  0.617

by query length (best target, and the oracle for that group)
  words:1     23 queries  lexical 0.906  semantic 0.819  hybrid 0.922*  router 0.906  oracle 0.929
  words:2    108 queries  lexical 0.619  semantic 0.681  hybrid 0.692*  router 0.619  oracle 0.779
  words:3+   348 queries  lexical 0.567  semantic 0.617  hybrid 0.642*  router 0.617  oracle 0.705

in plain words
  On its own, hybrid is the best way to search here (0.666 ndcg@10) and lexical the weakest (0.595).
    A router is the logic that picks lexical, semantic or hybrid for each query; the numbers above
    say whether one is worth having.
  If every query used whichever way scored best for it (the oracle, 0.733), the score would be
    +0.066 above always using hybrid. That is the most any rule for choosing could add.
  Your router (router) scores 0.631, 0.036 below always using hybrid: on this data routing does not
    pay off. Running hybrid for every query is simpler and scores higher.
  It leaves 0.102 of the oracle's potential on the table, so a better rule has room to win.
```

These numbers were measured after [`judy judge`](https://docs-v3-preview.elastic.dev/llm/judging) had added 178 labels, so they differ slightly from the
human-only 0.594 and 0.661.
- **Oracle.** The best fixed target for every query. It is the ceiling for any router built from those modes.
- **Router vs oracle.** How much a perfect router would add (here 0.102).
- **Router vs best single target.** Whether routing beats simply always running the best mode. Here it does not: hybrid wins by
  0.036.
- **Routed by mode.** How the router actually split the queries, and what each half scored.
- **By query length.** The best target in each bucket (marked `*`), next to the oracle for that bucket.

On this data hybrid beats every single mode in every length bucket. Semantic beats lexical from two words up. Lexical beats
semantic only on one-word queries. Your data will differ; the point is to find out in a minute.
Targets for `modes` are ordinary [targets](https://docs-v3-preview.elastic.dev/concepts/targets). Pass two or more that each retrieve one way (`--target` is
repeatable). Pass a router with `--router`, usually a [builder](https://docs-v3-preview.elastic.dev/workflow/dynamic-queries). A target that fails is reported by name and the
command exits 3, never as a score of zero.

## Holes in the judgements

Semantic retrieval surfaces documents nobody judged, and unjudged documents count as not relevant. A semantic or hybrid target can therefore look worse than it is. `judy compare` warns when the two sides judged different
shares of their top results. [`judy judge`](https://docs-v3-preview.elastic.dev/llm/judging) fills the gaps.