﻿---
title: Targets
description: A target is anything that answers a query with a ranked list of documents. You define targets in judy.yaml and pick one with --target name. There are...
url: https://docs-v3-preview.elastic.dev/concepts/targets
---

# Targets
A target is anything that answers a query with a ranked list of documents. You define targets in `judy.yaml` and pick one with
`--target name`. There are five kinds; each says how to ask.
```yaml
clusters:
  dev: {url: env:ES_URL, api_key: env:ES_API_KEY}    
targets:
  full:    {dsl:     {cluster: dev, index: docs, file: queries/full.json}}
  esql:    {esql:    {cluster: dev, file: queries/full.esql}}
  dynamic: {builder: {cluster: dev, index: docs, command: [node, build-query.js]}}
  api:     {http:    {url: "https://example.test/search?q={{query}}", hits: "$.results", id: url}}
  replay:  {run:     {file: earlier.run}}
docs: {id: url, title: title, text: [summary, body]}  
```

Relative file paths resolve against the folder of the configuration file. Write secrets as `env:NAME`, which reads an environment
variable when it is used, so the file can be committed.

## `dsl`: a Query DSL template

A JSON file with placeholders. judy renders it for each query and sends it to `{index}/_search`.

| Placeholder       | Replaced with                                          |
|-------------------|--------------------------------------------------------|
| `{{query}}`       | the query text (escaped for JSON)                      |
| `{{size}}`        | the number of results wanted (the set's `k`, or `--k`) |
| `{{params.name}}` | a tuning parameter (see below)                         |

```json
{"size": "{{size}}",
 "_source": ["product_id", "product_name"],
 "query": {"multi_match": {"query": "{{query}}", "fields": ["product_name^3", "product_description"]}}}
```

Settings: `cluster`, `index` and `file`. A template that sets its own `size` keeps it. Retrievers (`rrf`, `linear`) work like any
other body, so hybrid search is just a template.

## `esql`: an ES|QL query

An inline `query` or a `file`, with named parameters: `?query`, `?size` and `?name` for your own parameters. judy only sends the
parameters a query actually uses.
```text
FROM docs METADATA _score
| WHERE MATCH(title, ?query)
| SORT _score DESC
| LIMIT ?size
```

Each result column that matches the `docs:` mapping is read as the id, title or text; `_score` is kept when present.

## `builder`: your own code

Your own code, in any language, builds the request for each query. judy starts the command once per query. It gives the
command the query as a JSON line on standard input and as `JUDY_*` environment variables. It reads one JSON line back, so the
request can depend on anything your code knows. A shell script is enough. See [dynamic query building](https://docs-v3-preview.elastic.dev/workflow/dynamic-queries).
Settings:

| Setting           | Meaning                                                                                                                                                                                       |
|-------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `command`         | the program and its arguments, as a list                                                                                                                                                      |
| `cluster`         | the cluster to send the request to                                                                                                                                                            |
| `index`           | the default index, for answers that name none                                                                                                                                                 |
| `cwd`             | the working folder. It defaults to the configuration's folder, and a relative `./program` resolves against it                                                                                 |
| `timeout_seconds` | how long to wait for an answer. The default is 60                                                                                                                                             |
| `lifetime`        | `query` (the default) starts a new process for each query. `process` keeps one long-lived command that reads a line per query and echoes its `id`. Use it for builders that are slow to start |


## `http`: any search API

Calls a URL and reads hits from a JSON response.

| Setting                        | Meaning                                                               |
|--------------------------------|-----------------------------------------------------------------------|
| `url`                          | the URL; `{{query}}` and `{{size}}` are filled in (and URL-encoded)   |
| `method`                       | `GET`, or `POST` when there is a `body_file`                          |
| `headers`                      | a map; values may be `env:` references                                |
| `body_file`                    | a JSON body template with the same placeholders                       |
| `hits`                         | a JSONPath to the array of hits, `$` by default                       |
| `id`, `title`, `text`, `score` | the fields to read from each hit; they default to the `docs:` mapping |


## `run`: a TREC run file

Reads a file written by another system, with lines of `topic Q0 doc rank score tag`. Nothing is searched, so it needs no cluster
and the same file always gives the same scores. It is how you try judy offline, and how you score a system judy cannot call.

## Tuning parameters

A target can carry `params`, which fill `{{params.name}}` in a template or are passed to a builder:
```yaml
targets:
  full: {dsl: {cluster: dev, index: docs, file: queries/full.json}, params: {title_boost: 3}}
```

`--target full:title_boost=5` overrides a parameter for one run, and `judy compare full full:title_boost=5 --set …` compares the
two. A boost sweep is a loop of compares.

## Reading hits: the `docs` mapping

```yaml
docs:
  id: product_id          
  title: product_name     
  text: [product_description]  
  normalize: [lowercase, trailing-slash, strip-host]  
```

`id` is read from the document's `_source` (or a returned field), not from `_id`, so judgements can use any stable key.