OTQL documentation
The OTQL language reference: operators, scoring, aggregations, joins, limits, and caching.
Your goal
Find the operator or directive you need, and use it correctly.
Before you read this
This page is the reference. What OTQL is for and how a query is structured is in OTQL; worked queries are in OTQL examples.
WHERE - object tree expressions
The WHERE clause is a list of predicates separated by the logical operators AND, OR, and NOT. Parentheses group predicates.
SELECT (...) FROM (...) WHERE (PredicateA AND PredicateB) OR PredicateC
SELECT (...) FROM (...) WHERE price > 50.0 AND last_modified_date > "now-10d"Date operators
>=, >, <=, <, = or ==, !=.
Dates can be written in these forms:
ISO 8601, with the time part optional:
2012-09-27,2012-09-27T12:42:00a timestamp in milliseconds:
1549365498507date math, defining a relative date
Date math
A relative date compared to an anchor. The anchor is now, or a date in ISO 8601 or timestamp form followed by ||. Then one or more expressions: +1h adds an hour, -1d subtracts a day, /d rounds down to the nearest day.
Assuming now is 2001-01-01 12:00:00:
y
Years
M
Months
w
Weeks
d
Days
h or H
Hours
m
Minutes
s
Seconds
String operators
Only indexed String fields are eligible, and the behaviour depends on the data_type declared in the schema.
All operators are case-insensitive. Values are stored as a set of transformed words.
match(fieldName, comparisonValue, operator = "or")
True if words in fieldName match words in comparisonValue. operator defaults to "or" - true if at least one word matches; "and" requires all words of comparisonValue to be found.
starts_with(fieldName, comparisonValue)
True if a word of the text in fieldName starts with one of the words in comparisonValue.
The same transformation applied to the stored data is applied to the comparison value.
All operators are case-sensitive. The value is a single word, with no transformation.
starts_with(fieldName, comparisonValue)
True if the exact value in fieldName starts with the exact comparison value.
= or ==
True if the exact values are equal.
IN
A shortcut for filtering on several values of the same field.
Equivalent to channel_id = "2456" OR channel_id = "5489" OR channel_id = "1426".
is_defined
Checks whether a field has a value. It applies to any indexed field and returns a boolean.
"example"
True
["example"]
True
[null]
True
""
True
[]
True
null
False
undefined
False
No field at all
False
Scoring operators
Every pair of braces { } containing a sub-query implicitly calculates a score for that sub-query. By default the score is the number of items matching the sub-query.
This is how you express frequency conditions.
@ScoreSum(min: Float)
Returns the score only if the nested sub-query has a score greater than or equal to min.
@ScoreAvg(min: Float)
Averages the sub-query's matching scores, and returns true if the average is at least min.
@ScoreMax(min: Float)
Takes the maximum of the sub-query's matching scores, and returns true if it is at least min.
Changing how scores are calculated
Leaves of your object tree often carry numeric fields - Int or Float. @ScoreField(name: String) uses such a field's value as the score of the sub-query instead of a count, which is what turns a frequency condition into a monetary one.
SELECT operations
Selecting fields. Any field in the schema can be selected.
Aggregation operations
Aggregations are initiated by a directive in the SELECT clause. They take the WHERE filter into account, but are not compatible with the @filter directive used in SELECT.
@count
Metric directives
They calculate one value per bucket, or a single value over everything if no bucket directive is used.
@avg
Average value
Numeric values
@min
Minimum value
Numeric values
@max
Maximum value
Numeric values
@sum
Sum of values
Numeric values
@cardinality
Count of distinct values
Any indexed field
Bucket directives
They separate values into buckets.
@map
One bucket per field value.
@histogram(interval: N)
An aggregated count on a numeric field, in intervals of N.
@date_histogram(interval: "...")
An aggregated count by period. Allowed intervals: 1M for a month, or XXd for a number of days.
Aliases
An alias names a field expression in the output - necessary as soon as you aggregate the same field twice.
JOIN
Adds predicates from another object, after the FROM/WHERE clauses. Several joins can appear in the same query.
LIMIT
The Query Tool returns 10 elements by default; LIMIT overrides that.
LIMIT is also ignored when using @count or @cardinality:
Executing a query
The body is the OTQL query as a string.
The response carries the result plus useful metadata: took, timed_out, result_type, precision, sampling_ratio, rows, and cache_hit.
A 503 with SERVICE_UNAVAILABLE means retry later.
Query cache
With use_cache=true, the system returns the query from the cache when available. Check the cache_hit property of the response to know whether it did.
Without the parameter, or with use_cache=false, the cache is skipped: the query executes and its result is not stored. You cannot use this to force a cache update.
Optimisation
The engine tries to optimise queries automatically before running them - for instance rewriting multiple OR operators as an IN when that is better. Writing IN yourself is still clearer, and guaranteed.
Related pages
Last updated
Was this helpful?