Skip to content

Commit 62190d1

Browse files
authored
[DOCS] Clarify Update API does not replace fields with sparse_vector field (#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 d6ae386 commit 62190d1

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
@@ -220,24 +225,81 @@ GET my-index-000001/_search
220225
}
221226
```
222227

223-
::::{note}
224-
`sparse_vector` fields can not be included in indices that were **created** on {{es}} versions between 8.0 and 8.10
225-
::::
228+
## Updating `sparse_vector` fields
226229

230+
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:
227231

228-
::::{note}
229-
`sparse_vector` fields only support strictly positive values. Negative values will be rejected.
230-
::::
232+
- Existing tokens in the sparse vector are preserved
233+
- New tokens are added
234+
- Tokens present in both the existing and new data will have their values updated
231235

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

233-
::::{note}
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-
::::
238+
### Example of merging behavior
236239

240+
Original document:
237241

238-
::::{note}
239-
`sparse_vector` fields only preserve 9 significant bits for the precision, which translates to a relative error of about 0.4%.
240-
::::
242+
```console
243+
PUT /my-index/_doc/1
244+
{
245+
"my_vector": {
246+
"token_a": 0.5,
247+
"token_b": 0.8
248+
}
249+
}
250+
```
251+
252+
Partial update:
253+
254+
```console
255+
POST /my-index/_update/1
256+
{
257+
"doc": {
258+
"my_vector": {
259+
"token_c": 0.3
260+
}
261+
}
262+
}
263+
```
264+
265+
Observe that tokens are merged, not replaced:
266+
267+
```json
268+
{
269+
"my_vector": {
270+
"token_a": 0.5,
271+
"token_b": 0.8,
272+
"token_c": 0.3
273+
}
274+
}
275+
```
276+
277+
### Replacing the entire `sparse_vector` field
278+
279+
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:
280+
281+
```console
282+
POST /my-index/_update/1
283+
{
284+
"script": {
285+
"source": "ctx._source.my_vector = params.new_vector",
286+
"params": {
287+
"new_vector": {
288+
"token_x": 1.0,
289+
"token_y": 0.6
290+
}
291+
}
292+
}
293+
}
294+
```
241295

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

300+
## Important notes and limitations
243301

302+
- `sparse_vector` fields cannot be included in indices that were **created** on {{es}} versions between 8.0 and 8.10
303+
- `sparse_vector` fields only support strictly positive values. Negative values will be rejected.
304+
- `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.
305+
- `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)