Operators
A variety of operators in SurrealQL allow for complex manipulation of data, and advanced logic.
| Operator | Description |
|---|---|
&&
AND
| Checks whether both of two values are truthy |
||
OR
| Checks whether either of two values is truthy |
!
| Reverses the truthiness of a value |
!!
| Determines the truthiness of a value |
??
| Check whether either of two values are truthy and not NULL |
?:
| Check whether either of two values are truthy |
=
IS
| Check whether two values are equal |
!=
IS NOT
| Check whether two values are not equal |
==
| Check whether two values are exactly equal |
?=
| Check whether any value in a set is equal to a value |
*=
| Check whether all values in a set are equal to a value |
~
| Compare two values for equality using fuzzy matching |
!~
| Compare two values for inequality using fuzzy matching |
?~
| Check whether any value in a set is equal to a value using fuzzy matching |
*~
| Check whether all values in a set are equal to a value using fuzzy matching |
<
| Check whether a value is less than another value |
<=
| Check whether a value is less than or equal to another value |
>
| Check whether a value is greater than another value |
>=
| Check whether a value is greater than or equal to another value |
+
| Add two values together |
-
| Subtract a value from another value |
*
×
| Multiply two values together |
/
÷
| Divide a value by another value |
**
| Raises a base value by another value |
CONTAINS
∋
| Checks whether a value contains another value |
CONTAINSNOT
∌
| Checks whether a value does not contain another value |
CONTAINSALL
⊇
| Checks whether a value contains all other values |
CONTAINSANY
⊃
| Checks whether a value contains any other value |
CONTAINSNONE
⊅
| Checks whether a value contains none of the following values |
INSIDE
IN
∈
| Checks whether a value is contained within another value |
NOTINSIDE
NOT IN
∉
| Checks whether a value is not contained within another value |
ALLINSIDE
⊆
| Checks whether all values are contained within other values |
ANYINSIDE
⊂
| Checks whether any value is contained within other values |
NONEINSIDE
⊄
| Checks whether no value is contained within other values |
OUTSIDE
| Checks whether a geometry type is outside of another geometry type |
INTERSECTS
| Checks whether a geometry type intersects another geometry type |
@@
@[ref]@
| Checks whether the terms are found in a full-text indexed field |
<|4|>
<|3,HAMMING| >
| Performs a K-Nearest Neighbors (KNN) search to find a specified number of records closest to a given data point, optionally using a defined distance metric. Supports customizing the number of results and choice of distance calculation method. |
&& or AND
The and operator checks whether both of two values are truthy.
|| or OR
The or operator checks whether either of two values are truthy.
!
The not operator reverses the truthiness of a value.
!!
The not not operator is simply an application of the ! operator twice. It can be used to determines the truthiness of a value.
??
The null coalescing operator checks whether either of two values are truthy and not NONE or NULL.
?:
The truthy coalescing operator checks whether either of two values are truthy.
= or IS
The equal operator checks whether two values are equal.
!= or IS NOT
The not equal operator checks whether two values are not equal.
==
The exact operator checks whether two values are exact. This operator also checks that each value has the same type.
?=
The any equal operator checks whether any value in an array equals another value.
*=
The all equal operator checks whether all values in an array equals another value.
~ ?~ !~ *~
These operators used to compare two values for equality using fuzzy matching. They have been removed since 3.0 to avoid implicitly preferring one algorithm over another, as the type of fuzzy matching to use will depend on each individual case.
Please use the string::similarity::* functions instead:
<
The less than operator checks whether a value is less than another value.
<=
The less than or equal operator checks whether a value is less than or equal to another value.
>
The greater than operator checks whether a value is less than another value.
>=
The greater than or equal operator checks whether a value is less than or equal to another value.
+
The add operator adds two values together.
-
The subtract operator subtracts a value from another value.
* or ×
The multiply operator multiplies a value by another value.
/ or ÷
The divide operator divides a value by another value.
**
The power operator raises a base value by another value.
CONTAINS or ∋
The contains operator checks whether a value contains another value.
CONTAINSNOT or ∌
The not contains operator checks whether a value does not contain another value.
CONTAINSALL or ⊇
The contains all operator checks whether a value contains all of multiple values.
CONTAINSANY or ⊃
The contains any operator checks whether a value contains any of multiple values.
INSIDE or ∈ or IN
The inside operator checks whether a value is contained within another value.
This operator can also be used to check for the existence of a key inside an object. To do so, precede IN with the field name as a string.
IN can also be used with a record ID as long as the ID is expanded to include the fields. Both of the following queries will return true.
NOTINSIDE or ∉ or NOT IN
The not inside operator checks whether a value is not contained within another value.
ALLINSIDE or ⊆
The all inside operator checks whether all of multiple values are contained within another value.
ANYINSIDE or ⊂
The any inside operator checks whether any of multiple values are contained within another value.
NONEINSIDE or ⊄
The none inside operator checks whether none of multiple values are contained within another value.
OUTSIDE
The outside operator checks whether a geometry value is outside another geometry value.
INTERSECTS
The intersects operator checks whether a geometry value intersects another geometry value.
MATCHES
The matches operator checks whether the terms are found in a full-text indexed field.
Using the matches operator with a reference checks whether the terms are found, highlights the searched terms, and computes the full-text score.
AND, OR, and numeric operators inside @@
In addition to the AND keyword, the OR matches operator can also be used as of 3.0.0-beta. This allows a single string to be compared against instead of needing to specify individual parts of the string.
KNN
K-Nearest Neighbors (KNN) is a fundamental algorithm used for classifying or regressing based on the closest data points in the feature space, with its performance and scalability critical in applications involving large datasets.
In practice, the efficiency and scalability of the KNN algorithm are crucial, especially when dealing with large datasets. Different implementations of KNN are tailored to optimize these aspects without compromising the accuracy of the results.
SurrealDB supports different K-Nearest Neighbors methods to perform KNN searches, each with unique requirements for syntax. Below are the details for each method, including how to format your query with examples:
Brute Force Method
Best for smaller datasets or when the highest accuracy is required.
K: The number of nearest neighbors to retrieve.
DISTANCE_METRIC: The metric used to calculate distances, such as EUCLIDEAN or MANHATTAN.
HNSW Method
Recommended for very large datasets where speed is essential and some loss of accuracy is acceptable.
K: The number of nearest neighbors.
EF: The size of the dynamic candidate list during the search, affecting the search's accuracy and speed.
Using the ANY/ALL operators for string indexes
An index defined on a string value can be used via the operators CONTAINSANY, ALLINSIDE, or ANYINSIDE. The operator CONTAINS, however, will not use a defined index as CONTAINS is used for substring matches between strings themselves as opposed to an index lookup.
Types of operators, order of operations and binding power
To determine which operator is executed first, a concept called "binding power" is used. Operators with greater binding power will operate directly on their neighbours before those with lower binding power. The following is a list of all operator types from greatest to lowest binding power.
| Operator name | Description |
|---|---|
Unary
|
The Unary operators are !, +, and -.
|
Nullish
|
The Nullish operators are ?: and ??.
|
Range
|
The Range operator is ...
|
Cast
|
The Cast operator is `, with type_name a stand in for the type to cast into. For example, or `.
|
Power
|
The only Power operator is **.
|
MulDiv
|
The MulDiv (multiplication and division) operators are *, /, ÷, and %.
|
AddSub
|
The AddSub (addition and subtraction) operators are + and -.
|
Relation
|
The Relation operators are `=, ∋, CONTAINS, ∌, CONTAINSNOT, ∈, INSIDE, ∉, NOTINSIDE, ⊇, CONTAINSALL, ⊃, CONTAINSANY, ⊅, CONTAINSNONE, ⊆, ALLINSIDE, ⊂, ANYINSIDE, ⊄, NONEINSIDE, OUTSIDE, INTERSECTS, NOT, and IN`.
|
Equality
|
The Equality operators are =, IS, ==, !=, *=, ?=, and @.
|
And
|
The And operators are && and AND.
|
Or
|
The Or operators are || and OR.
|
Examples of binding power
The following samples show examples of basic operations of varying binding power. The original example is followed by the same example with the parts with higher binding power in parentheses, then the final expression after the first bound portion is calculated, and finally the output.