Skip to content

Commit 292deae

Browse files
committed
Documentation for native MMR support
Signed-off-by: Bo Zhang <[email protected]>
1 parent 0e80549 commit 292deae

File tree

5 files changed

+191
-1
lines changed

5 files changed

+191
-1
lines changed

_search-plugins/search-pipelines/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ The following is a list of search pipeline terminology:
2020
* [_Search response processor_]({{site.url}}{{site.baseurl}}/search-plugins/search-pipelines/search-processors#search-response-processors): A component that intercepts a search response and search request (the query, results, and metadata passed in the request), performs an operation with or on the search response, and returns the search response.
2121
* [_Search phase results processor_]({{site.url}}{{site.baseurl}}/search-plugins/search-pipelines/search-processors#search-phase-results-processors): A component that runs between search phases at the coordinating node level. A search phase results processor intercepts the results retrieved from one search phase and transforms them before passing them to the next search phase.
2222
* [_Processor_]({{site.url}}{{site.baseurl}}/search-plugins/search-pipelines/search-processors/): Either a search request processor or a search response processor.
23+
* [_System Generated Processor_]({{site.url}}{{site.baseurl}}/search-plugins/search-pipelines/system-generated-search-processors/): System generated search processors.
2324
* _Search pipeline_: An ordered list of processors that is integrated into OpenSearch. The pipeline intercepts a query, performs processing on the query, sends it to OpenSearch, intercepts the results, performs processing on the results, and returns them to the calling application, as shown in the following diagram.
2425

2526
![Search processor diagram]({{site.url}}{{site.baseurl}}/images/search-pipelines.png)

_search-plugins/search-pipelines/search-pipeline-metrics.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
layout: default
33
title: Search pipeline metrics
4-
nav_order: 50
4+
nav_order: 60
55
has_children: false
66
parent: Search pipelines
77
---
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
layout: default
3+
title: System generated search processors
4+
nav_order: 50
5+
has_children: false
6+
parent: Search pipelines
7+
---
8+
9+
# System generated search processors
10+
11+
System generated search processors are search processors that can be systematically generated based on the search request.
12+
13+
To enable the processors, you must set the `cluster.search.enabled_system_generated_factories` setting to either `*` or explicitly include the required factories.
14+
15+
Example:
16+
```json
17+
PUT _cluster/settings
18+
{
19+
"persistent": {
20+
"cluster.search.enabled_system_generated_factories": [
21+
"mmr_over_sample_factory",
22+
"mmr_rerank_factory"
23+
]
24+
}
25+
}
26+
```
27+
{% include copy-curl.html %}
28+
29+
30+
31+
Search processors can be of the following types:
32+
33+
- [System generated search request processors](#system-generated-search-request-processors)
34+
- [System generated search response processors](#system-generated-search-response-processors)
35+
- [System generated search phase results processors](#system-generated-search-phase-results-processors)
36+
37+
# System generated search request processors
38+
39+
| Processor name | Processor factory name | Execution stage | Trigger condition | Description |
40+
|-------------------|---------------------------| ------------------- |-------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
41+
| `mmr_over_sample` | `mmr_over_sample_factory` | `POST_USER_DEFINED` | Triggered when a search request includes the mmr extension | Modifies the query size and `k` value of the k-NN query to oversample candidates for MMR re-ranking. This processor runs after any user-defined search request processors. |
42+
43+
The execution stage determines whether a system-generated processor runs before or after user-defined processors of the same type.
44+
45+
# System generated search response processors
46+
47+
| Processor name | Processor factory name | Execution stage | Trigger condition | Description |
48+
|----------------|------------------------|---------------------|------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------|
49+
| `mmr_rerank` | `mmr_rerank_factory` | `PRE_USER_DEFINED` | Triggered when a search request includes the mmr extension | Re-ranks the oversampled results using MMR and reduces them to the original query size. This processor runs before any user-defined search response processors. |
50+
51+
The execution stage determines whether a system-generated processor runs before or after user-defined processors of the same type.
52+
53+
# System generated search phase results processors
54+
55+
We don't have any for now.
56+
57+
# Limitation
58+
59+
## One processor per type and execution stage
60+
For each processor type and execution stage, OpenSearch currently supports only one system-generated processor for a search request. For example, only one search request processor can run at the `POST_USER_DEFINED` stage, and only one search response processor can run at the `PRE_USER_DEFINED` stage.

_vector-search/specialized-operations/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ cards:
1313
- heading: "Radial search"
1414
description: "Search all points in a vector space that reside within a specified maximum distance or minimum score threshold from a query point"
1515
link: "/vector-search/specialized-operations/radial-search-knn/"
16+
- heading: "Vector search with MMR"
17+
description: "Use vector search with maximal marginal relevance(mmr) re-rank."
18+
link: "/vector-search/specialized-operations/vector-search-mmr/"
1619
---
1720

1821
# Specialized vector search
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
---
2+
layout: default
3+
title: Vector search with MMR
4+
nav_order: 60
5+
parent: Specialized vector search
6+
has_children: false
7+
has_math: true
8+
---
9+
10+
# Vector search with MMR
11+
12+
The maximal marginal relevance (MMR) search helps balance relevance and diversity in search results. Instead of returning only the most similar documents, MMR selects results that are both relevant to the query and different from each other. This improves the coverage of the result set and reduces redundancy, which is especially useful in vector search scenarios.
13+
14+
MMR re-ranking balances two competing objectives:
15+
16+
- Relevance: How well a document matches the query.
17+
18+
- Diversity: How different a document is from the documents already selected.
19+
20+
The algorithm computes a score for each candidate document using the following principle:
21+
22+
```json
23+
MMR = (1 − λ) * relevance_score − λ * max(similarity_with_selected_docs)
24+
```
25+
26+
Where:
27+
28+
- λ is the diversity parameter (closer to 1 means higher diversity).
29+
30+
- relevance_score measures similarity between the query vector and the candidate document vector.
31+
32+
- similarity_with_selected_docs measures similarity between the candidate and already selected documents.
33+
34+
By adjusting the diversity parameter, you can control the tradeoff between highly relevant results and more diverse coverage in the result set.
35+
36+
# Prerequisites
37+
38+
To use MMR, you must enable [system-generated search processor factories](({{site.url}}{{site.baseurl}}/search-plugins/search-pipelines/system-generated-search-processors/)). Set the `cluster.search.enabled_system_generated_factories` setting (by default it is an empty list) to either `*` or explicitly include the required factories:
39+
40+
```json
41+
PUT _cluster/settings
42+
{
43+
"persistent": {
44+
"cluster.search.enabled_system_generated_factories": [
45+
"mmr_over_sample_factory",
46+
"mmr_rerank_factory"
47+
]
48+
}
49+
}
50+
```
51+
{% include copy-curl.html %}
52+
53+
# Parameters
54+
55+
The mmr extension in the search API supports the following parameters:
56+
57+
| Parameter | Data type | Required | Description |
58+
| ------------------------- | --------- | ----------------------------------------- |---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
59+
| `diversity` | float | No | Controls the weight of diversity in the re-ranking process. Valid values range from `0` to `1`. A value of `1` prioritizes maximum diversity, and `0` disables diversity. Default is `0.5`. |
60+
| `candidates` | integer | No | Specifies how many candidate documents to oversample before re-ranking. Default is `3 * query size`. |
61+
| `vector_field_path` | string | Optional, but required for remote indices | Path to the vector field used for MMR re-ranking. If not provided, OpenSearch resolves it automatically from the search request. |
62+
| `vector_field_data_type` | string | Optional, but required for remote indices | Data type of the vector field. Used to parse the field and calculate similarity. If not provided, OpenSearch resolves it from the index mapping. |
63+
| `vector_field_space_type` | string | Optional, but required for remote indices | Used to decide the similarity function for the vector field, such as cosine similarity or Euclidean distance. If not provided, OpenSearch resolves it from the index mapping. |
64+
65+
66+
# Example request
67+
68+
The following example shows how to use the mmr extension with a k-NN query:
69+
70+
```json
71+
POST /my-index/_search
72+
{
73+
"query": {
74+
"knn": {
75+
"my_vector_field": {
76+
"vector": [0.12, 0.54, 0.91],
77+
"k": 10
78+
}
79+
}
80+
},
81+
"ext": {
82+
"mmr": {
83+
"diversity": 0.7
84+
}
85+
}
86+
}
87+
88+
```
89+
{% include copy-curl.html %}
90+
91+
The following example shows how to use the mmr extension with a neural query:
92+
```json
93+
POST /my-index/_search
94+
{
95+
"query": {
96+
"neural": {
97+
"my_vector_field": {
98+
"query_text": "query text",
99+
"model_id": "<your model id>"
100+
}
101+
}
102+
},
103+
"ext": {
104+
"mmr": {
105+
"diversity": 0.6,
106+
"candidates": 50,
107+
"vector_field_path": "my_vector_field",
108+
"vector_field_data_type": "float",
109+
"vector_field_space_type": "l2"
110+
}
111+
}
112+
}
113+
```
114+
115+
When querying across multiple indices, ensure that the data type, and space type are aligned. Since that info decides the similarity function we use to calculate the similarity between docs.
116+
{: .note}
117+
118+
# Limitations
119+
120+
## MMR Query Type Restriction:
121+
MMR currently only supports knn or neural queries as the top-level query in a search request. If knn or neural is nested inside another query type (such as a bool query or hybrid query), MMR is not supported.
122+
123+
## Required Explicit Vector Field Details
124+
You must explicitly provide the vector field details—`vector_field_path, vector_field_data_type, and vector_field_space_type`—when querying remote indices.
125+
126+
Reason: Unlike a local index where OpenSearch can automatically resolve this metadata from the index mapping, the system cannot reliably fetch this information from the remote cluster. Providing these details ensures correct parsing of the vector data and accurate similarity calculations.

0 commit comments

Comments
 (0)