|
1 | 1 | ---
|
| 2 | +applies_to: |
| 3 | + stack: |
| 4 | + serverless: |
| 5 | +products: |
| 6 | + - id: elasticsearch |
2 | 7 | navigation_title: "Sparse vector"
|
3 | 8 | mapped_pages:
|
4 | 9 | - https://www.elastic.co/guide/en/elasticsearch/reference/current/sparse-vector.html
|
@@ -220,24 +225,81 @@ GET my-index-000001/_search
|
220 | 225 | }
|
221 | 226 | ```
|
222 | 227 |
|
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 |
226 | 229 |
|
| 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: |
227 | 231 |
|
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 |
231 | 235 |
|
| 236 | +This is different from primitive array fields (like `keyword`), which are replaced entirely during updates. |
232 | 237 |
|
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 |
236 | 239 |
|
| 240 | +Original document: |
237 | 241 |
|
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 | +``` |
241 | 295 |
|
| 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 | +::: |
242 | 299 |
|
| 300 | +## Important notes and limitations |
243 | 301 |
|
| 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