Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions _query-dsl/specialized/script-score.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,5 +373,95 @@
```
{% include copy-curl.html %}

### Late interaction score

The `lateInteractionScore` function is a Painless script scoring function that calculates document relevance using token-level vector matching. It compares each query vector against all document vectors, finds the maximum similarity for each query vector, and sums these maximum scores to produce the final document score.

**Example score calculation**:
- Query vectors: `[[0.8, 0.1], [0.2, 0.9]]`
- Document vectors: `[[0.7, 0.2], [0.1, 0.8], [0.3, 0.4]]`
- Query vector 1 → finds best match among document vectors → score A
- Query vector 2 → finds best match among document vectors → score B
- Final score = A + B

This approach enables fine-grained semantic matching between queries and documents, making it particularly effective for reranking search results.

#### Index mapping requirements

The vector field must be mapped as either an `object` (recommended) or `float` type.

We recommend mapping the vector field as an `object` with `"enabled": false` because it stores raw vectors without parsing, improving performance:

```json
{
"mappings": {
"properties": {
"my_vector": {
"type": "object",
"enabled": false
}
}
}
}
```

Alternatively, you can map the vector field as a `float`:

```json
{
"mappings": {
"properties": {
"my_vector": {
"type": "float"
}
}
}
}
```

#### Example

The following example demonstrates using the `lateInteractionScore` function with cosine similarity to measure vector similarity based on direction rather than distance:

```json
GET my_index/_search
{
"query": {
"script_score": {
"query": { "match_all": {} },
"script": {
"source": "lateInteractionScore(params.query_vectors, 'my_vector', params._source, params.space_type)",
"params": {
"query_vectors": [[[1.0, 0.0]], [[0.0, 1.0]]],
"space_type": "cosinesimil"
}
}
}
}
}
```
{% include copy-curl.html %}

#### Parameters

The `lateInteractionFunction` supports the following parameters.

| Parameter | Data type | Required | Description |
| :--- | :--- | :--- | :--- |
| `query_vectors` | Array of arrays | Yes | Query vectors for similarity matching |
| `vector_field` | String | Yes | Name of the document field containing vectors |

Check failure on line 452 in _query-dsl/specialized/script-score.md

View workflow job for this annotation

GitHub Actions / style-job

[vale] reported by reviewdog 🐶 [OpenSearch.Spelling] Error: containin. If you are referencing a setting, variable, format, function, or repository, surround it with tic marks. Raw Output: {"message": "[OpenSearch.Spelling] Error: containin. If you are referencing a setting, variable, format, function, or repository, surround it with tic marks.", "location": {"path": "_query-dsl/specialized/script-score.md", "range": {"start": {"line": 452, "column": 62}}}, "severity": "ERROR"}
| `doc` | Map | Yes | Document source (use `params._source`) |
| `space_type` | String | No | Similarity metric. Default: `"l2"` |

The `space_type` parameter determines how similarity is calculated and accepts the following valid values.

| Space type | Description | Higher score means |
| :--- | :--- | :--- |
| `innerproduct` | Dot product | More similar vectors |
| `cosinesimil` | Cosine similarity | More similar direction |
| `l2` (default) | Euclidean distance | Closer vectors (inverted) |

For a complete example, see [Reranking by a field using an externally hosted late interaction model]({{site.url}}{{site.baseurl}}/search-plugins/search-relevance/rerank-by-field-late-interaction/).

If [`search.allow_expensive_queries`]({{site.url}}{{site.baseurl}}/query-dsl/index/#expensive-queries) is set to `false`, `script_score` queries are not executed.
{: .important}
Loading
Loading