|
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
|
@@ -150,24 +155,81 @@ GET my-index-000001/_search
|
150 | 155 | }
|
151 | 156 | ```
|
152 | 157 |
|
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 |
156 | 159 |
|
| 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: |
157 | 161 |
|
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 |
161 | 165 |
|
| 166 | +This is different from primitive array fields (like `keyword`), which are replaced entirely during updates. |
162 | 167 |
|
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 |
166 | 169 |
|
| 170 | +Original document: |
167 | 171 |
|
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 | +``` |
171 | 225 |
|
| 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 | +::: |
172 | 229 |
|
| 230 | +## Important notes and limitations |
173 | 231 |
|
| 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