Skip to content

Commit 9cc0cef

Browse files
committed
[DOCS] Clarify Update API does not replace fields with sparse_vector field (elastic#135788)
* [DOCS] Clarify Update API does not replace fields with sparse_vector field Updated the documentation for `sparse_vector` fields to clarify merging behavior, replacement methods, and limitations. 🚗 Compiled all the admonitions into a limitations section * Fix links * Fix rank link to rank_features * idem * Fix formatting and wording * Add applies_to and products metadata
1 parent 0d78dc0 commit 9cc0cef

File tree

1 file changed

+74
-12
lines changed

1 file changed

+74
-12
lines changed

docs/reference/elasticsearch/mapping-reference/sparse-vector.md

Lines changed: 74 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
---
2+
applies_to:
3+
stack:
4+
serverless:
5+
products:
6+
- id: elasticsearch
27
navigation_title: "Sparse vector"
38
mapped_pages:
49
- https://www.elastic.co/guide/en/elasticsearch/reference/current/sparse-vector.html
@@ -150,24 +155,81 @@ GET my-index-000001/_search
150155
}
151156
```
152157

153-
::::{note}
154-
`sparse_vector` fields can not be included in indices that were **created** on {{es}} versions between 8.0 and 8.10
155-
::::
158+
## Updating `sparse_vector` fields
156159

160+
When using the [Update API](https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-update) with the `doc` parameter, `sparse_vector` fields behave like nested objects and are **merged** rather than replaced. This means:
157161

158-
::::{note}
159-
`sparse_vector` fields only support strictly positive values. Negative values will be rejected.
160-
::::
162+
- Existing tokens in the sparse vector are preserved
163+
- New tokens are added
164+
- Tokens present in both the existing and new data will have their values updated
161165

166+
This is different from primitive array fields (like `keyword`), which are replaced entirely during updates.
162167

163-
::::{note}
164-
`sparse_vector` fields do not support [analyzers](docs-content://manage-data/data-store/text-analysis.md), querying, sorting or aggregating. They may only be used within specialized queries. The recommended query to use on these fields are [`sparse_vector`](/reference/query-languages/query-dsl/query-dsl-sparse-vector-query.md) queries. They may also be used within legacy [`text_expansion`](/reference/query-languages/query-dsl/query-dsl-text-expansion-query.md) queries.
165-
::::
168+
### Example of merging behavior
166169

170+
Original document:
167171

168-
::::{note}
169-
`sparse_vector` fields only preserve 9 significant bits for the precision, which translates to a relative error of about 0.4%.
170-
::::
172+
```console
173+
PUT /my-index/_doc/1
174+
{
175+
"my_vector": {
176+
"token_a": 0.5,
177+
"token_b": 0.8
178+
}
179+
}
180+
```
181+
182+
Partial update:
183+
184+
```console
185+
POST /my-index/_update/1
186+
{
187+
"doc": {
188+
"my_vector": {
189+
"token_c": 0.3
190+
}
191+
}
192+
}
193+
```
194+
195+
Observe that tokens are merged, not replaced:
196+
197+
```json
198+
{
199+
"my_vector": {
200+
"token_a": 0.5,
201+
"token_b": 0.8,
202+
"token_c": 0.3
203+
}
204+
}
205+
```
206+
207+
### Replacing the entire `sparse_vector` field
208+
209+
To replace the entire contents of a `sparse_vector` field, use a [script](docs-content://explore-analyze/scripting/modules-scripting-using.md) in your update request:
210+
211+
```console
212+
POST /my-index/_update/1
213+
{
214+
"script": {
215+
"source": "ctx._source.my_vector = params.new_vector",
216+
"params": {
217+
"new_vector": {
218+
"token_x": 1.0,
219+
"token_y": 0.6
220+
}
221+
}
222+
}
223+
}
224+
```
171225

226+
:::{note}
227+
This same merging behavior also applies to [`rank_features` fields](/reference/elasticsearch/mapping-reference/rank-features.md), because they are also object-like structures.
228+
:::
172229

230+
## Important notes and limitations
173231

232+
- `sparse_vector` fields cannot be included in indices that were **created** on {{es}} versions between 8.0 and 8.10
233+
- `sparse_vector` fields only support strictly positive values. Negative values will be rejected.
234+
- `sparse_vector` fields do not support [analyzers](docs-content://manage-data/data-store/text-analysis.md), querying, sorting or aggregating. They may only be used within specialized queries. The recommended query to use on these fields are [`sparse_vector`](/reference/query-languages/query-dsl/query-dsl-sparse-vector-query.md) queries. They may also be used within legacy [`text_expansion`](/reference/query-languages/query-dsl/query-dsl-text-expansion-query.md) queries.
235+
- `sparse_vector` fields only preserve 9 significant bits for the precision, which translates to a relative error of about 0.4%.

0 commit comments

Comments
 (0)