This document describes the JSON format used for sending document operations to Vespa. Field types are defined in the schema reference. This is a reference for:
Also refer to encoding troubleshooting.
string |
Feeding in an empty string ("") for a field will have the same effect as not feeding a value for that field, and the field will not be rendered in the document API and in document summaries. |
---|---|
int | |
long | |
bool |
true or false: |
byte | |
float | |
double | |
position |
A position is encoded as a lat/lng object: See Geo Search for details. |
predicate |
A predicate is a string: |
raw |
The content of a raw field is represented as a base64-encoded string: When used as summary field it will be rendered as a base64-encoded string. |
uri |
A URI is a string: |
array |
Arrays are represented as JSON arrays. An array of struct is represented as a JSON array of JSON objects matching the defined struct field: Feeding in an empty array ([]) for a field will have the same effect as not feeding a value for that field, and the field will not be rendered in the document API and in document summaries. |
weightedset |
Weighted sets are represented as maps where the value is the weight. Note, even if the key is not a string as such, it will be represented as a string in the JSON format. Feeding in an empty weightedset ({}) for a field will have the same effect as not feeding a value for that field, and the field will not be rendered in the document API and in document summaries. |
tensor |
Indexed tensors short form: An array where the values are ordered in the standard value order, where indexes of dimensions to the right are incremented before indexes to the left, where dimensions are ordered alphabetically (such that, e.g. with a tensor with dimensions x,y the "y" values for each value of "x" are adjacent): The cells array can optionally be nested in an object under the key "values". This is how tensor values are returned by default, along with another key "type" containing the tensor type. Short form for tensors with a single mapped dimension: A map with the dimension key as key and the value as value. The cells object can optionally be nested in an object under the key "cells". This is how tensor values are returned by default, along with another key "type" containing the tensor type.
Mixed tensors short form:
If the tensor has a single sparse dimension: A map where the key is the value of that dimension and the value
is a nested array containing the values of the dense subspace within that key. Example - single sparse dimension: Example - multiple sparse dimensions: This is how tensor values are returned by default, along with another key "type" containing the tensor type.
Cell values as binary data
For dense and mixed tensors it's possible to fill the cell values directly from binary data
sent in as a string of hexadecimal digits. The simplest possible case is if you have a
vector with This can be used to represent the value For other cell types, it's possible to take the bits of the floating-point value, interpreted directly as an unsigned integer of appropriate width (16, 32, or 64 bits) and use the hex dump (respectively 4, 8, or 16 hex digits per cell) in a string. For "float" cells (32-bit IEE754 floating-point) a simple snippet for converting a cell could look like this:
As an advanced combination example, if you have a tensor with type
Verbose: Tensor fields may be represented as an array of cells: This works for any tensor but is verbose, so shorter forms specific to various tensor types are also supported. Use the shortest form applicable to your tensor type for the best possible performance. The cells array can optionally be nested in an object under the key "cells". This is how tensor values are returned by default, along with another key "type" containing the tensor type. |
struct | |
map |
The JSON dictionary key must be a string, even if the map key type in the schema is not a string: Feeding in an empty map ({}) for a field will have the same effect as not feeding a value for that field, and the field will not be rendered in the document API and in document summaries. |
annotationreference |
Annotation-references do not have a JSON representation |
reference |
String with document ID referring to a parent document: |
In general, fields that have not received a value during feeding will be ignored when rendering the document. They are considered as empty fields. However, certain field types have some values which causes them to be considered empty. For instance, the empty string ("") is considered empty, as well as the empty array ([]). See the above table for more information for each type.
Refer to reads and writes for details - alternatives:
The "put" payload has a "put" operation and "fields" (/document/v1/ example):
"get" does not have a payload - the response has the same "field" object as in "put", and also "id" and "pathId" fields (/document/v1/ example):
The "remove" payload has a "remove" operation (/document/v1/ example):
The "update" payload has an "update" operation and "fields" (/document/v1/ example):
An optional condition can be added to operations to specify a test and set condition - see conditional writes. The value of the condition is a document selection, encoded as a string. Example: Increment the sales field only if it is already equal to 999 (/document/v1/ example):
If the condition is not met, a 412 response code is returned.
Updates to nonexistent documents are supported using create. (/document/v1/ example):
Since Vespa 8.178, create can also be used together with conditional Put operations (/document/v1/ example - review notes there before using):
assign
is used to replace the value of a field (or an element of a collection) with a new value.
When assigning, one can generally use the same syntax and structure
as when feeding that field's value in a put
operation.
field title type string { indexing: summary }
field tensorfield type tensor(x{},y{}) { indexing: attribute | summary }
This will fully replace the entire tensor stored in this field.
A full struct is replaced by assigning an object of struct key/value pairs.
struct person { field first_name type string {} field last_name type string {} } field contact type person { indexing: summary }
Individual struct fields are updated using field path syntax. Refer to the reference for restrictions using structs.
Individual map entries can be updated using field path syntax.
The following declaration defines a map
where the key
is an Integer
and the value is a person
struct.
struct person { field first_name type string {} field last_name type string {} } field contact type map<int, person> { indexing: summary }
Example updating part of an entry in the contact
map:
contact
is the name of the map field to be updated{0}
is the key that is going to be updatedfirst_name
is the struct field to be updated inside the person
structAssigning an element to a key in a map will insert the key/value mapping if it does not already exist, or overwrite it with the new value if it does exist. Refer to the reference for restrictions using maps.
field my_food_scores type map<string, string> { indexing: summary }
struct contact_info { field phone_number type string {} field email type string {} } field contacts type map<string, contact_info> { indexing: summary }
field ingredients type array<string> { indexing: summary }
Assign full array:
Assign existing elements in array:
Note that the index element 3 needs to exist. Alternative using match:
Individual array elements may be updated using field path or match syntax.
Refer to the reference for restrictions using array of structs.
struct person { field first_name type string {} field last_name type string {} } field people type array<person> { indexing: summary }
Note that the element index needs to exist. Use add to add a new element. Alternative syntax using match:
Adding new elements to a weighted set can be done using add, or
by assigning with field{key}
syntax. Example of the latter:
field int_weighted_set type weightedset<int> { indexing: summary } field string_weighted_set type weightedset<string> { indexing: summary }
Note that using the field{key}
syntax for weighted sets may be
less efficient than using add.
To clear a field, assign a null
value to it.
add
is used to add entries to arrays, weighted sets or to the mapped dimensions of tensors.
The added entries are appended to the end of the array in the order specified.
field tracks type array<string> { indexing: summary }
Add weighted set elements by using a JSON key/value syntax, where the value is the weight of the element.
Adding a key/weight mapping that already exists will overwrite the existing weight with the new one.
field int_weighted_set type weightedset<int> { indexing: summary } field string_weighted_set type weightedset<string> { indexing: summary }
Add cells to mapped or mixed tensors. Invalid for tensors with only indexed
dimensions. Adding a cell that already exists will overwrite the cell value with the new value.
The address must be fully specified, but cells with bound indexed dimensions not specified
will receive the default value of 0.0
.
See system test
tensor add update
for more examples.
field tensorfield type tensor(x{},y[3]) { indexing: attribute | summary }
In this example, cell {"x":"b","y":"2"}
will implicitly be set to 0.0.
So if you started with the following tensor:
{ {"x": "a", "y": "0"}: 0.2, {"x": "a", "y": "1"}: 0.3, {"x": "a", "y": "2"}: 0.5, }
You now end up with this tensor after the above add operation was applied:
{ {"x": "a", "y": "0"}: 0.2, {"x": "a", "y": "1"}: 0.3, {"x": "a", "y": "2"}: 0.5, {"x": "b", "y": "0"}: 2.0, {"x": "b", "y": "1"}: 3.0, {"x": "b", "y": "2"}: 0.0, }
Prefer the block short form for mixed tensors instead. This also avoids the problem where cells with indexed dimensions are not specified:
Remove elements from weighted sets, maps and tensors with remove
.
field string_weighted_set type weightedset<string> { indexing: summary }
field string_map type map<string, string> { indexing: summary }
Removes cells from mapped or mixed tensors. Invalid for tensors with only indexed dimensions. Only mapped dimensions should be specified for tensors with both mapped and indexed dimensions, as all indexed cells the mapped dimensions point to will be removed implicitly. See system test tensor remove update for more examples.
field tensorfield type tensor(x{},y[2]) { indexing: attribute | summary }
In this example, cells {x:b,y:0},{x:b,y:1},{x:c,y:0},{x:c,y:1}
will be removed.
It is also supported to specify only a subset of the mapped dimensions in the addresses.
In that case, all cells that match the label values of the specified dimensions are removed.
In the given example, all cells having label b
for dimension x
are removed.
field tensorfield type tensor(x{},y{},z[2]) { indexing: attribute | summary }
The four arithmetic operators increment
, decrement
,
multiply
and divide
are used to modify single
value numeric values without having to look up the current
value before applying the update. Example:
field sales type int { indexing: summary | attribute }
If an arithmetic operation is to be done for a specific key
in a weighted set or array, use the match
operation:
field track_popularity type weightedset<string> { indexing: summary | attribute }
In other words, for the weighted set "track_popularity",
match
the element "Lay Lady Lay", then increment
its weight by 1.
See the reference for how to auto-create keys.
If the updated field is an array, the element
value would be a positive integer.
Individual cells in tensors can be modified using the modify
update.
The cells are modified according to the given operation:
replace
- replaces a single cell valueadd
- adds a value to the existing cell valuemultiply
- multiples a value with the existing cell value
The addresses of cells must be fully specified. If the cell does not exist, the update for that cell will be ignored.
Use "create": true
(see example below) to create non-existing cells before the modify update is applied.
See system test
tensor modify update
for more examples.
field tensorfield type tensor(x[3]) { indexing: attribute | summary }
In this example, cell {"x":"1"}
is replaced with value 7.0 and {"x":"2"}
with value 8.0.
If operation add
or multiply
was used instead,
7.0 and 8.0 would be added or multiplied to the current values of cells {"x":"1"}
and {"x":"2"}
.
For tensors with a single mapped dimension the cells short form can also be used:
field tensorfield type tensor(x{}) { indexing: attribute | summary }
In this example, 5.0 is added to cell {"x":"b"}
and 6.0 is added to cell {"x":"c"}
.
With "create": true
non-existing cells in the input tensor are created before applying the modify update.
The default cell value is 0.0 for replace
and add
, and 1.0 for multiply
.
This means a non-existing cell ends up with the value specified in the operation.
For mixed tensors the block short form can also be used to modify entire dense subspaces:
field tensorfield type tensor(x{},y[3]) { indexing: attribute | summary }
Fieldpath is for accessing fields within composite structures - for structures that are not part of index or attribute, it is possible to access elements directly using fieldpaths. This is done by adding more information to the field value. For map structures, specify the key (see example).
mymap{mykey}
and then do operation on the element which is keyed by "mykey". Arrays can be accessed as well (see details).
myarray[3]
And this is also true for structs (see details). Note: Struct updates do not work for index mode:
mystruct.value1
This also works for nested structures,
e.g. a map
of map
to array
of struct
: