Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
114 lines (100 sloc) 3.37 KB
---
# Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
title: "Document Field Path Syntax"
---
<p>
The field path syntax is used several places in Vespa to traverse documents through
arrays, structs, maps and sets and generate a set of values matching the expression. Examples -
If the document contains the field <code>mymap</code>, and it has a
key <code>mykey</code>, the expression returns the value of the map for that key:
<pre>
mymap{mykey}
</pre>
Returns the value in index 3 of the <code>myarray</code> field, if set:
<pre>
myarray[3]
</pre>
Returns the value of the <code>value1</code> field in the struct
field <code>mystruct</code>, if set:
<pre>
mystruct.value1
</pre>
If mystructarray is an array field containing structs,
returns the values of value1 for each of those structs:
<pre>
mystructarray.value1
</pre>
The following syntax can be used for the different field types,
and can be combined recursively as required:
</p>
<h2 id="maps-weighted-sets">Maps/weighted Sets</h2>
<table class="table">
<thead></thead><tbody>
<tr>
<th style="width:250px">&lt;mapfield&gt;{&lt;keyvalue&gt;}</th>
<td> Retrieve the value of a specific key</td>
</tr><tr>
<th>&lt;mapfield&gt;{$&lt;variablename&gt;}</th>
<td>Retrieve all values, setting the <a href="#variables">variable</a>
to the key value for each</td>
</tr><tr>
<th>&lt;mapfield&gt;.key</th>
<td>Retrieve all key values</td>
</tr><tr>
<th>&lt;mapfield&gt;.value</th>
<td>Retrieve all values</td>
</tr><tr>
<th>&lt;mapfield&gt;</th>
<td>Retrieve all keys</td>
</tr>
</tbody>
</table>
<p>
In the case of weighted sets, the value referenced above is the weight of the item.
</p>
<h2 id="array">Array</h2>
<table class="table">
<thead></thead><tbody>
<tr>
<th style="width:250px">&lt;arrayfield&gt;[&lt;index&gt;]</th>
<td>Retrieve the value in a specific index</td>
</tr><tr>
<th>&lt;arrayfield&gt;[$&lt;variablename&gt;]</th>
<td>Retrieve all values in the array, setting the <a href="#variables">variable</a>
to the index of each</td>
</tr><tr>
<th>&lt;arrayfield&gt;</th>
<td>Retrieve all values in the array</td>
</tr>
</tbody>
</table>
<h2 id="struct">Struct</h2>
<table class="table">
<thead></thead><tbody>
<tr>
<th style="width:250px">&lt;structfield&gt;{.&lt;subfield&gt;}</th>
<td>Return the value of the struct field</td>
</tr><tr>
<th>&lt;structfield&gt;</th>
<td>Return the value of all subfields</td>
</tr>
</tbody>
</table>
<p>
Note that when specifying values of subscripts of maps, weighted sets and arrays,
only primitive types (numbers and strings) may be used.
</p>
<h2 id="variables">Variables</h2>
<p>
It can be useful to reference several field paths using a common variable.
For instance, if you have an array of structs,
you may want to use document selection on fields within the same array index together.
This could be done by an expression like:
<pre>
mydoctype.mystructarray{$x}.field1=="foo" AND mydoctype.mystructarray{$x}.field2=="bar"
</pre>
Variables either have a <code>key</code> value (for maps and weighted sets),
or an <code>index</code> value (for arrays).
Variables cannot be used across such contexts
(that is, a map key cannot be used to index into an array).
</p>