Skip to content

Commit 2bcb93a

Browse files
mingshlbrianf-awskolchfa-awsnatebower
authored
Add lateInteractionScore Function (#11168)
* draft Signed-off-by: Mingshi Liu <[email protected]> * add details to lateInteractionScore function page Signed-off-by: Mingshi Liu <[email protected]> * add tutorials using late interaction model Signed-off-by: Mingshi Liu <[email protected]> * Update _search-plugins/search-relevance/rerank-by-field-late-interaction.md Co-authored-by: Brian Flores <[email protected]> Signed-off-by: Mingshi Liu <[email protected]> * Update _search-plugins/search-relevance/rerank-by-field-late-interaction.md Co-authored-by: Brian Flores <[email protected]> Signed-off-by: Mingshi Liu <[email protected]> * Doc review Signed-off-by: Fanit Kolchina <[email protected]> * Remove the late interaction file Signed-off-by: Fanit Kolchina <[email protected]> * Reformatted the requests Signed-off-by: Fanit Kolchina <[email protected]> * Apply suggestions from code review Signed-off-by: Nathan Bower <[email protected]> * Apply suggestions from code review Signed-off-by: kolchfa-aws <[email protected]> * use screenshot as sample image and attach sample search response Signed-off-by: Mingshi Liu <[email protected]> * update search term Signed-off-by: Mingshi Liu <[email protected]> * move session Signed-off-by: Mingshi Liu <[email protected]> * Enclose response in a details block Signed-off-by: Fanit Kolchina <[email protected]> --------- Signed-off-by: Mingshi Liu <[email protected]> Signed-off-by: Fanit Kolchina <[email protected]> Signed-off-by: Nathan Bower <[email protected]> Signed-off-by: kolchfa-aws <[email protected]> Co-authored-by: Brian Flores <[email protected]> Co-authored-by: Fanit Kolchina <[email protected]> Co-authored-by: Nathan Bower <[email protected]> Co-authored-by: kolchfa-aws <[email protected]>
1 parent a9f1233 commit 2bcb93a

File tree

3 files changed

+688
-5
lines changed

3 files changed

+688
-5
lines changed

_query-dsl/specialized/script-score.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,5 +373,95 @@ GET /demo_index_v1/_search
373373
```
374374
{% include copy-curl.html %}
375375

376+
### Late interaction score
377+
378+
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.
379+
380+
**Example score calculation**:
381+
- Query vectors: `[[0.8, 0.1], [0.2, 0.9]]`
382+
- Document vectors: `[[0.7, 0.2], [0.1, 0.8], [0.3, 0.4]]`
383+
- Query vector 1 → finds best match among document vectors → score A
384+
- Query vector 2 → finds best match among document vectors → score B
385+
- Final score = A + B
386+
387+
This approach enables fine-grained semantic matching between queries and documents, making it particularly effective for reranking search results.
388+
389+
#### Index mapping requirements
390+
391+
The vector field must be mapped as either an `object` (recommended) or `float` type.
392+
393+
We recommend mapping the vector field as an `object` with `"enabled": false` because it stores raw vectors without parsing, improving performance:
394+
395+
```json
396+
{
397+
"mappings": {
398+
"properties": {
399+
"my_vector": {
400+
"type": "object",
401+
"enabled": false
402+
}
403+
}
404+
}
405+
}
406+
```
407+
408+
Alternatively, you can map the vector field as a `float`:
409+
410+
```json
411+
{
412+
"mappings": {
413+
"properties": {
414+
"my_vector": {
415+
"type": "float"
416+
}
417+
}
418+
}
419+
}
420+
```
421+
422+
#### Example
423+
424+
The following example demonstrates using the `lateInteractionScore` function with cosine similarity to measure vector similarity based on direction rather than distance:
425+
426+
```json
427+
GET my_index/_search
428+
{
429+
"query": {
430+
"script_score": {
431+
"query": { "match_all": {} },
432+
"script": {
433+
"source": "lateInteractionScore(params.query_vectors, 'my_vector', params._source, params.space_type)",
434+
"params": {
435+
"query_vectors": [[[1.0, 0.0]], [[0.0, 1.0]]],
436+
"space_type": "cosinesimil"
437+
}
438+
}
439+
}
440+
}
441+
}
442+
```
443+
{% include copy-curl.html %}
444+
445+
#### Parameters
446+
447+
The `lateInteractionFunction` supports the following parameters.
448+
449+
| Parameter | Data type | Required | Description |
450+
| :--- | :--- | :--- | :--- |
451+
| `query_vectors` | Array of arrays | Yes | Query vectors for similarity matching |
452+
| `vector_field` | String | Yes | Name of the document field containing vectors |
453+
| `doc` | Map | Yes | Document source (use `params._source`) |
454+
| `space_type` | String | No | Similarity metric. Default: `"l2"` |
455+
456+
The `space_type` parameter determines how similarity is calculated and accepts the following valid values.
457+
458+
| Space type | Description | Higher score means |
459+
| :--- | :--- | :--- |
460+
| `innerproduct` | Dot product | More similar vectors |
461+
| `cosinesimil` | Cosine similarity | More similar direction |
462+
| `l2` (default) | Euclidean distance | Closer vectors (inverted) |
463+
464+
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/).
465+
376466
If [`search.allow_expensive_queries`]({{site.url}}{{site.baseurl}}/query-dsl/index/#expensive-queries) is set to `false`, `script_score` queries are not executed.
377467
{: .important}

0 commit comments

Comments
 (0)