Skip to content

Commit ea7ec97

Browse files
authored
[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
1 parent aef5c27 commit ea7ec97

File tree

1 file changed

+67
-12
lines changed

1 file changed

+67
-12
lines changed

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

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -220,24 +220,79 @@ GET my-index-000001/_search
220220
}
221221
```
222222

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-
::::
223+
## Updating `sparse_vector` fields
226224

225+
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:
227226

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

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

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-
::::
233+
### Example of merging behavior
236234

235+
Original document:
237236

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-
::::
237+
```console
238+
PUT /my-index/_doc/1
239+
{
240+
"my_vector": {
241+
"token_a": 0.5,
242+
"token_b": 0.8
243+
}
244+
}
245+
```
246+
247+
Partial update:
248+
249+
```console
250+
POST /my-index/_update/1
251+
{
252+
"doc": {
253+
"my_vector": {
254+
"token_c": 0.3
255+
}
256+
}
257+
}
258+
```
259+
260+
Observe that tokens are merged, not replaced:
261+
262+
```json
263+
{
264+
"my_vector": {
265+
"token_a": 0.5,
266+
"token_b": 0.8,
267+
"token_c": 0.3
268+
}
269+
}
270+
```
271+
272+
### Replacing the entire `sparse_vector` field
273+
274+
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:
275+
276+
```console
277+
POST /my-index/_update/1
278+
{
279+
"script": {
280+
"source": "ctx._source.my_vector = params.new_vector",
281+
"params": {
282+
"new_vector": {
283+
"token_x": 1.0,
284+
"token_y": 0.6
285+
}
286+
}
287+
}
288+
}
289+
```
241290

291+
This same merging behavior also applies to [`rank_features` fields](/mapping-reference/rank-feature.md), as they are also object-like structures.
242292

293+
## Important notes and limitations
243294

295+
- `sparse_vector` fields can not be included in indices that were **created** on Elasticsearch versions between 8.0 and 8.10
296+
- `sparse_vector` fields only support strictly positive values. Negative values will be rejected.
297+
- `sparse_vector` fields do not support [analyzers](https://www.elastic.co/docs/manage-data/data-store/text-analysis), querying, sorting or aggregating. They may only be used within specialized queries. The recommended query to use on these fields are [`sparse_vector`](https://www.elastic.co/docs/reference/query-languages/query-dsl/query-dsl-sparse-vector-query) queries. They may also be used within legacy [`text_expansion`](https://www.elastic.co/docs/reference/query-languages/query-dsl/query-dsl-text-expansion-query) queries.
298+
- `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)