Query Language Reference

Vespa accepts unstructured human input and structured queries for application logic separately, then combines them into a single data structure for executing. Human input is parsed heuristically, while application queries are formulated in YQL.

A query URL looks like:

In other words, yql contains:
This matches all documents where the field named text contains the word blues.

Quote (") and backslash (\) characters in text values must be escaped by a backslash.


select is the list of summary fields requested (a field with the "summary" index attribute). Vespa will hide other fields in the matching documents.

The above explicitly requests the fields "price" and "isbn" (from all sources). To request all fields, use an asterisk as field selection:

from sources

from sources specifies which document sources to search. Example:

searches in music documents. Search in:
all sources select … from sources * where …
a set of sources select … from sources source1, source2 where …
a single source select … from source1 where …
In other words, sources is used for querying some/all sources. If only a single source is queries, the sources keyword is dropped.


The where clause is a tree of operators:


The following numeric operators are available: =, <, >, <=, >=, range(field, lower bound, upper bound)


Numbers must be in the signed 32-bit range, input 64-bit signed numbers using L as suffix.

The interval is by default a closed interval. If the lower bound is exclusive, set the annotation "bounds" to "leftOpen". If the upper bound is exclusive, set the same annotation to "rightOpen". If both bounds are exclusive, set the annotation to "open".

The number operations support an extra annotation, the integer "hitLimit". This is used for capped range search. An alternative to using negative and positive values for "hitLimit" is always using a positive number of hits (as a negative number of hits do no not make much sense) and combine this with either of the boolean annotations "ascending" and "descending" (but not both). Then "[{"hitLimit": 38, "descending": true}]" would be equivalent to setting it to -38, i.e. only populate with 38 hits and start from upper boundary, i.e. descending order.


The boolean operator is: =


The right hand side argument of the contains operator is either a string literal, or a function, like phrase.

contains is the basic building block for text matching. The kind of matching to be done depends on the field settings in the search definition.

The matched field must be an indexed field or attribute.

Fields inside structs are referenced using dot notation - e.g mystruct.mystructfield.

By default, the string will be tokenized to match the field(s) searched. Explicitly control tokenization by using annotations:


Note the use of parentheses to control precedence.


and accepts other and statements, or statements, userQuery, logically inverted statements - and contains statements as arguments:


or accepts other or statements, and statements, userQuery - and contains statements as arguments:


As Vespa does recall as opposed to filtering, the only excluding operator in Vespa is andnot. In YQL this is expressed as the right hand side, and only the right hand side, argument of the and operator may be a logically inverted expression, i.e. using the ! operator:


Phrases are expressed as a function

It can be necessary to pass along extra information about a search term, for instance when specifying a term should not be stemmed before matching. This is done by using YQL annotations:


near() matches if all argument terms occur close to each other in the same document. It supports the distance-annotation which controls how many words are allowed to separate the argument terms. The default value is 2.


onear() (ordered near) is like near(), but also requires the terms in the document having the same order as given in the function (i.e. it is a phrase allowing other words interleaved). With distance 1 onear() has the same semantics as phrase().


sameElement() is an operator that requires the terms to match within the same struct element in an array or a map field. Example:

struct person {
    field first_name    type string {}
    field last_name     type string {}
    field year_of_birth type int {}

field persons type array<person> {
    indexing: summary
    struct-field first_name    { indexing: attribute }
    struct-field last_name     { indexing: attribute }
    struct-field year_of_birth { indexing: attribute }
field identities type map<string, person> {
    indexing: summary
    struct-field key                 { indexing: attribute }
    struct-field value.first_name    { indexing: attribute }
    struct-field value.last_name     { indexing: attribute }
    struct-field value.year_of_birth { indexing: attribute }
With normal AND the query persons.first_name AND persons.last_name will normally not give you what you want. It will match if a document has a persons element with a matching first_name AND any element with a matching last_name. So you will get a lot of false positives since there is nothing limiting them to the same element. However, that is what sameElement ensures.
The above returns all documents containing Joe Smith born before 1940 in the persons array.

Searching in a map is similar to searching in an array of struct. The difference is that you have an extra synthetic struct with the field members key and value. The above example with the identities map looks like this:

The above returns all documents that have tagged Joe Smith born before 1940 as a 'father'. The importance here is using the indirection of key and value to address the keys and the values of the map.


If two terms in the same field should give exactly the same behavior when match, the equiv() operator behaves like a special case of "or".


In many cases, the OR operator will give the same results as an EQUIV. The matching logic is exactly the same, and an OR does not have the limitations that EQUIV does (below). The difference is in how matches are visible to ranking functions. All words that are children of an OR count for ranking. When using an EQUIV however, it looks like a single word:

  • Counts as only +1 for queryTermCount
  • Counts as 1 word for completeness measures
  • Proximity will not discriminate different words inside the EQUIV
  • Connectivity can be set between the entire EQUIV and the word before and after
  • Items inside the EQUIV are not directly visible to ranking features, so weight and connectivity on those will have no effect
Limitations on how equiv can be used in a query:
  • equiv may not appear inside a phrase
  • It may only contain TermItem and PhraseItem instances. Operators like and cannot be placed inside equiv
  • PhraseItems inside equiv will rank like as if they have size 1
Learn how to use equiv.


Used to search for urls indexed using the uri field type.


Various subfields are supported to search components of the URL, see the field type definition. The hostname subfield supports anchoring to the start and/or end of the hostname, controlled by the startAnchor and endAnchor boolean annotations. Anchoring to the end is on by default while anchoring to the start is not. Hence

will match vespa.ai and docs.vespa.ai and so on, while
will only match vespa.ai.

Regular expressions is supported using posix extended syntax with the limitation that it is case insensitive. Replace contains with matches to run a regex search. This example becomes a substring search:

This example matches both madonna, madona and with any number of ns:
Here you match any string starting with mad:

Note: Only attribute fields in documents that have mode="index" is supported. It is also not optimized. Having a prefix using the ^ will be faster than not having one.


userInput() is a robust way of mixing user input and a formal query. It allows controlling whether the user input is to be stemmed, lowercased, etc, but it also allows for controlling whether it should be treated as a raw string, whether it should simply be segmented or parsed as a query.

Here, the userInput() function will access the query property "animal", and parse the property value as an "ALL" query, resulting in the following expression:
Now, if we changed the value of "animal" without changing the rest of the expression:
The result would be:
Now, let's assume we want to combine multiple query properties and have a more complex expression as well:
The resulting YQL expression will be:
Now, consider we do not want the "teddy" field to be treated as its own query segment, it should only be segmented with the linguistic libraries to get recall. We can do this by adding a "grammar" annotation to the userInput() call:
Then, the linguistic library will split on space, and the resulting expression is:
Instead of a variable reference, the userInput() function also accepts raw strings as arguments, but this would obviously not be suited for parametrizing the query from a query profile. It is mostly intended as test feature.

userInput() control annotations:

grammar all raw, segment and all values accepted for the model.type argument in the search API. How to parse the user input. "raw" will treat the user input as a string to be matched without any processing, "segment" will do a first pass through the linguistic libraries, while the rest of the values will treat the string as a query to be parsed. If query parsing fails, an error message will be returned.
defaultIndex default Any searchable field in the system's search definition. Same as model.defaultIndex in the search API. If "grammar" is set to "raw" or "segment", this will be the field searched.
language Autodetect RFC 3066 language code Language setting for the linguistics treatment of this userInput() call, also see model.language in the search API reference.
allowEmpty false Boolean true or false. Whether to allow empty input for query parsing and search terms. If this is true, a NullItem instance is inserted in the proper place in the query tree. If "allowEmpty" is false, the query will fail if the user provided data can not be parsed or is empty.

In addition, other annotations, like stem or ranked, will take effect as normal.

The query parsing mechanism has currently certain limitations for propagating annotation, therefore, for any value of grammar other than raw or segment, only the following annotations will take effect:

  • ranked
  • filter
  • stem
  • normalizeCase
  • accentDrop
  • usePositionData


userQuery() evaluates to the parsed user query, i.e. the HTTP API parameter named query (including the filter part, if this is available). The function userQuery represents where the heuristically parsed query is to be inserted as a sub-tree into the YQL query. In other words, this is not a string substitution, the user query is first parsed with any of the supported grammars, then the resulting tree is inserted into the corresponding place in the YQL query tree:

query abc def -ghi
type all
yql select * from sources * where vendor contains "brick and mortar" AND price < 50 AND userQuery();
The above example will in other words evaluate to a query where the numeric field price must have a value lower than 50, vendor must match the term brick and mortar, and the default index must contain the two terms abc and def while not containing the term ghi. The spaces in the vendor term will not be used to split this into several new terms by YQL. The string specified by the search will be used. Query transformers may convert the string at a later stage, but it is not necessary to do anything "special" to create a search term containing arbitrary characters.


The first, and only the first, argument of the rank() function determines whether a document is a match, but all arguments are used for calculating rank score.



dotProduct calculates the dot product between the weighted set in the query and a weighted set field in the document as its rank score contribution:

The result is stored as a raw score.

A normal use case is a collection of weighted tokens produced by an algorithm, to match against a corpus containing weighted tokens produced by another algorithm in order to implement personalized content exploration.

Refer to advanced ranking for a discussion of usage and examples.

Field type Weighted set attribute with fast-search. Note: Also supported for regular attribute or index fields, but then with much weaker performance).
Query model Weighted set with {token, weight} pairs
Matching Documents where the weighted set field contains at least one of the tokens in the query.
Ranking Dot product score between the weights of the matched query tokens and field tokens. This score is available using rawScore or itemRawScore rank features.
Java Query Item DotProductItem

When using weightedSet to search a field, all tokens present in the searched field will be matched against the weighted set in the query. This means that using a weighted set to search a single-value attribute field will have similar semantics to using a normal term to search a weighted set field. The low-level matching information resulting from matching a document with a weighted set in the query will contain the weights of all the matched tokens in descending order. Each matched weight will be represented as a standard occurrence on position 0 in element 0.

weightedSet has similar semantics to equiv, as it acts as a single term in the query. However, the restriction dictating that it contains a collection of weighted tokens directly enables specific back-end optimizations that improves performance for large sets of tokens compared to using the generic equiv or or operators.

Refer to advanced ranking for a discussion of usage and examples.

Field type Singlevalue or multivalue attribute or index field. (Note: Most use cases operates on a single value field).
Query model Weighted set with {token, weight} pairs.
Matching Documents where the field contains at least one of the tokens in the query.
Ranking The operator will act as a single term in the back-end. The query term weight is the weight assigned to the operator itself and the match weight is the largest weight among matching tokens from the weighted set. This operator does not produce a raw score. Due to better ranking and performance we recommend using dotProduct instead.
Java Query Item WeightedSetItem

wand can be used to search for documents where weighted tokens in a field matches a subset of weighted tokens in the query. At the same time, it internally calculates the dot product between token weights in the query and the field. wand is guaranteed to return the top-k hits according to its internal dot product rank score. It is an operator that scales adaptively from or to and.

wand optimizes the performance of using multiple threads per search in the backend, and is also called Parallel Wand.

wand also allows numeric arguments, then the search argument is an array of arrays of length two. In each pair, the first number is the search term, the second its weight:

Both wand and weakAnd support the annotations scoreThreshold, which is an double giving the minimum rank score for hits to include, and targetNumHits which is the wanted number of hits. By default, set targetNumHits equal to the number of hits to return. If additional second phase ranking with rerank-count is used, do not set targetNumHits less than the configured rank-profile's rerank-count.
Refer to advanced ranking for a discussion of usage and examples.

Field type Weighted set attribute with fast-search. Note: Also supported for regular attribute or index fields, but then with much weaker performance).
Query model Weighted set with {token, weight} pairs.
Matching Documents where the weighted set field contains at least one of the tokens in the query and where the internal dot product score for this document, is larger than the worst among the current top-k best hits. This means that more than top-k documents are matched and returned for ranking. It also means that many documents are skipped, even they match several tokens in the query because the dot product score is too low. This skipping makes wand faster than dotProduct in some cases.
Ranking Dot product score between the weights of the matched query tokens and field tokens. This score is available using rawScore or itemRawScore rank features. Note that the top-k best hits are only guaranteed to be returned when using this internal score as the final ranking expression.
Java Query Item WandItem

weakAnd is some times called Vespa Wand. Unlike wand, it accepts arbitrary word matches (across arbitrary fields) as arguments. Only a limited number of documents are returned for ranking (default is 100), but it does not guarantee to return the best k hits. This function can be seen as an optimized or:

Both wand and weakAnd support the annotations scoreThreshold, which is an double giving the minimum rank score for hits to include, and targetNumHits which is the wanted number of hits:
Unlike wand, weakAnd can be used to search across several fields of various types, but it does NOT guarantee to return the top-k best number of hits. It can however be combined with any ranking expression. Keep in mind that this expression should correlate with its simple internal ranking score that uses query term weight and inverse document frequency for matching terms.

Refer to advanced ranking for a discussion of usage and examples.

Field type Multiple fields of all types (both attribute and index).
Query model Arbitrary number of query items searching across different fields.
Matching Documents that matches at least one of the tokens in the query and where the internal operator score for this document is larger than the worst among the current top-k best hits. As with wand, this means that typically more than top-k documents are matched and a lot of documents are skipped.
Ranking Internal ranking score based on query term weight and inverse document frequency for matching terms to find the top-k hits. This score is currently not available to the ranking framework. Matching terms are exposed to the ranking framework (same as when using and or or), so an arbitrary ranking expression can be used in combination with this operator. Note that the ranking expression used should correlate with this internal ranking score. nativeFieldMatch and nativeDotProduct are good starting points.
Java Query Item WeakAndItem

nonEmpty takes as its only argument an arbitrary search expression. It will then perform a set of checks on that expression. If all the checks pass, the result is the same expression, otherwise the query will fail. The checks are as follows:

  1. No empty search term
  2. No empty operators, like phrases without terms
  3. No null markers (NullItem) from e.g. failed query parsing
Note how "foo" is empty in this case, which will force the query to fail. If "foo" contained a searchable term, the query would not have failed.


predicate() specifies a predicate query - see predicate fields. It takes three arguments: the predicate field to search, a map of attributes, and a map of range attributes:

Due to a quirk in YQL-parsing, one cannot specify an empty map, use the number 0 instead.

order by

Sort using order by. Add asc or desc after the name of an attribute to set sort order - ascending order is default.

Sorting function, locale and strength are defined using the annotations "function", "locale" and "strength", as in:
Note: match-phase is enabled when sorting - refer to the sorting reference.

limit / offset

To specify a slice / limit the number of hits returned / do pagination, use limit and/or offset:

The above will return two hits (if there sufficiently many hits matching the query), skipping the 29 first documents.


Set query timeout in milliseconds using timeout:

Only literal numbers are valid, i.e. setting another unit is not supported.


Terms and phrases can be annotated to manipulate the behavior. Add an annotation using [], like:


Annotations supported by strings

These annotations are supported by the string arguments to functions like and phrase() and near() and also the string argument to the "contains" operator.

"nfkc": true|false NFKC normalization. Default on.
"implicitTransforms": true|false Implicit term transformations (field defaults), default on. If implicitTransforms is active, the settings for the field in the search definition will be honored in term transforms, e.g. if the field has stemming, this term will be stemmed. If implicitTransforms are turned off, the search backend will receive the term exactly as written in the initial YQL expression. This is in other words a top level switch to turn off all other stemming, accent removal, Unicode normalizations and so on.
"annotations": {
  "string": "string"
Custom term annotations. This is by default empty.
"origin": {
  "original": "string",
  "offset": int,
  "length": int
The (sub-)string which produced this term. Default unset.
"usePositionData": true|false Use position data for ranking algorithm. Default true. This is term position, not to be confused with geo searches
"stem": true|false Stem this term if it is the setting for this field, default on.
"normalizeCase": true|false Normalize casing of this term if it is the setting for this field, default on.
"accentDrop": true|false Remove accents from this term if it is the setting for this field, default on.
"andSegmenting": true|false Force phrase or AND operator if re-segmenting (e.g. in stemming) this term results in multiple terms. Default is choosing from language settings.
"prefix": true|false Do prefix matching for this word. Default false. ("Search for "word*".")
"suffix": true|false Do suffix matching for this word. Default false. ("Search for "*word".")
"substring": true|false Do substring matching for this word if available in the index. Default false. ("Search for "*word*".") Only supported for streaming search.

Annotations supported by strings and functions

These annotations are supported by strings and by the functions which are handled like leaf nodes internally in the query tree: phrase(), near(), onear(), range(), equiv(), weightedSet(), weakAnd() and wand().

"id": int Unique ID used for e.g. connectivity.
"connectivity": {
  "id": int,
  "weight": double
Map with the ID and weight of explicitly connectivity of this item.
"significance": double Significance value for ranking.
"annotations": {
  "string": "string"
Custom annotations. No special semantics inside the YQL layer.
"filter": true|false Regard this term as a "filter" term. Default false.
"ranked": true|false Include this term for ranking calculation. Default true.
"label": "string" Label for referring to this term during ranking.
"weight": int Term weight, used in some ranking calculations.

Annotations of sub-expressions

Consider the following query:

The "stem" annotation controls whether a given term should be stemmed if its field is configured as a stemmed field (default is "true"). The "AND" operator itself has no internal API for whether its operands should be stemmed or not, but we can still annotate as such, because when the value of a given annotation is determined, the expression tree is followed from the term in question and up through its ancestors. Traversing the tree stops when a value is found (or there is nothing more to traverse). In other words, none of the terms in this example will be stemmed.

How annotations behave may be easier to understand of expressing a boolean query in the style of an S-expression:

(AND term1 term2 (OR term3 term4) (OR term5 (AND term6 term7)))
The annotation scopes would then be as follows, i.e. annotations on which elements will be checked when determining the settings for a given term:
term1term1 itself, and the first AND
term2term2 itself, and the first AND
term3term3 itself, the first OR and the first AND
term4term4 itself, the first OR and the first AND
term5term5 itself, the second OR and the first AND
term6term6 itself, the second AND, the second OR and the first AND
term7term7 itself, the second AND, the second OR and the first AND

Query properties

Use YQL variable syntax to initialize words in phrases and as single terms. This removes the need for caring about quoting a term in YQL, as well as URL quoting. The term will be used exactly as it is in the URL. As an example, look at a query with a YQL argument, and the properties animal and syntaxExample:

This YQL expression will then access the query properties animal and syntaxExample and evaluate to:

YQL in query profiles

YQL requires quoting to be included in a URL. Since YQL is well suited to application logic, while not being intended for end users, a solution to this is storing the application's YQL queries into different query profiles. To add a default query profile, add search/query-profiles/default.xml to the application package:

<query-profile id="default">
  <field name="yql">select * from sources * where default contains "latest" or userQuery();</field>
This will add latest as an OR term to all queries not having an explicit query profile parameter. The important thing to note is how it is not necessary to URL-quote anything in the query profiles files. They operate independently of the HTTP parsing as such.

Query rewriting in Searchers

Searchers which modifies the textual YQL statement (not recommended) should be annotated with @Before("ExternalYql"). Searchers modifying query tree produced from an input YQL statement should annotate with @After("ExternalYql").


Group / aggregate results by adding a grouping expression after a | - read more.