For the complete documentation index, see llms.txt. This page is also available as Markdown.

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"

A predicate does not return a boolean directly but a score - 1 if the condition holds, otherwise 0. At the end the score is compared to 0, and the predicate is true if it is higher. The logical operators keep the same priority as boolean ones.

This is not a curiosity: it is what makes the scoring operators below possible.

Date operators

>=, >, <=, <, = or ==, !=.

Dates can be written in these forms:

  • ISO 8601, with the time part optional: 2012-09-27, 2012-09-27T12:42:00

  • a timestamp in milliseconds: 1549365498507

  • date 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:

Unit
Meaning

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.

Operator
What it does

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.

What gets stored, and what therefore matches
Ingested
Stored
Matches
Does not match

Métamorphosé

métamorphosé

Métamorphosé, métamorphosé

metamorphose, métamorphos, métamorphosés

hameçon, ligne et bouchon

hameçon, ligne, et, bouchon

any of those, any case

hamecon

aujourd'hui

aujourd'hui

aujourd'hui

aujourd hui, aujourd, hui

100

100

100

1000, 10

100 900 km

100, 900, km

the whole string, or any word

100900, 100,900

km/h

km, h

km/h, km, h

kmh

100km/h

100km, h

100km/h, 100km, h, km/h

100, km

H&M

h, m

H&M, h, m

hm

£1,000 / 1,000 / 1,000+

1,000

all three

1000, 000, 1.000

1.000

1.000

1.000

1000, 000, 1,000

chou-fleur / chou- fleur / chou fleur

chou, fleur

all forms, or either word

chou_fleur, choufleur

chou_fleur

chou_fleur

chou_fleur

chou, fleur, choufleur, chou-fleur

recherche.aspx

recherche.aspx

recherche.aspx

recette, aspx

test@mics.com

test, mics.com

test@mics.com, test, mics.com

@, mics

Note the rules that surprise people: . and _ do not split, and - and / do.

All operators are case-sensitive. The value is a single word, with no transformation.

Operator
What it does

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".

IN performs better than multiple OR operators.

is_defined

Checks whether a field has a value. It applies to any indexed field and returns a boolean.

Field value
Returns

"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.

Directive
What it does

@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.

@ScoreField on its own achieves nothing a logical operator could not - it is meaningful only combined with @ScoreSum, @ScoreAvg, or @ScoreMax.

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.

Directive
What it returns
Applies to

@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.

Directive
What it does

@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.

  • @map does not count null values.

  • @map returns 50 buckets by default, up to a maximum of 50,000: SELECT { name @map(limit:200) } FROM UserEvent.

  • Bucket directives cannot be used with metrics on the same field.

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:

To extend the number of buckets returned by an aggregation directive, use the directive's own limit parameter - @map(limit:200) - not the LIMIT clause.

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.

The cache expiration delay is 12 hours.

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.

Last updated

Was this helpful?