14.10.1 Syntax

Trivial Expressions

Always evaluates to true:

true

Always evaluates to false:

false

Constants

Legal constants are integer, long and floating point numbers of single and double precision as well as strings literals and true and false. Long constants need to be suffixed with l or L. Floating point numbers are treated as double precision numbers by default. To force a certain precision you can use f or F for single precision and d or D for double precision floating point numbers. String literals are UTF-8 character sequences, surrounded by single or double quotation marks.

25
4294967296L
0.62f
0.33d
"Double quoted string"
'Single quoted string'

Vertex and Edge Identifiers

Depending on the filter type, different identifiers are valid.

Vertex Filter

Vertex filter expressions have only one keyword that addresses the vertex in the current context.

vertex denotes the vertex that is currently being evaluated by the filter expression.

vertex

Edge Filter

Edge filter expressions have several keywords that addresses the edge or its vertices in the current context.

edge denotes the edge that is currently being evaluated by the filter expression.

edge

dst denotes the destination vertex of the current edge. dst is only valid in the subgraph context.

dst

src denotes the source vertex of the current edge. src is only valid in the subgraph context.

src

Properties

Filter expressions can access the values of vertex and edge properties.

<id>.<property>

where:

  • <id>: is any vertex or edge identifier (that is, src, dst, vertex, edge).
  • <property>: is the name of a vertex or edge property.

    Note:

    This has to be the name of an edge property if the identifier is edge. Otherwise it has to be a vertex property.

If the property name is a reserved name in the filter expression syntax or contains spaces, it must be quoted in single or double quotes.

The following code accesses the 'cost' property of the source vertex.

src.cost

Temporal properties support values comparison (constants and property values) using special constructors. The default temporal formats are shown in the following table:

Table 14-5 Default Temporal Formats

Property Type Constructor
DATE date ('yyyy-MM-dd HH:mm:ss')
LOCAL_DATE date 'yyyy-MM-dd'
TIME time 'HH:mm:ss'
TIME_WITH_TIMEZONE time 'HH:mm:ss+/-XXX'
TIMESTAMP timestamp 'yyyy-MM-dd HH:mm:ss'
TIMESTAMP_WITH_TIMEZONE timestamp 'yyyy-MM-dd HH:mm:ss+/-XXX'

The following expression accesses the property 'timestamp_withTZ' of an edge and checks if it is equal to 3/27/2007 06:00+01:00.

edge.timestamp_withTZ = timestamp'2007-03-2706:00:00+01:00'

Note:

Properties of type date can only be checked for equality. date type usage is deprecated since version 2.5, instead use local date or timestamp types that support all operations.

Methods

Filter expressions support the following functions:

Degree Functions

  1. outDegree() returns the number of outgoing edges of the vertex identifier. degree() is a synonym for outDegree.
    int <id>.degree()
    int <id>.outDegree()

    The following example determines whether the out-degree of the source vertex is greater than three:

    src.degree() > 3
  2. inDegree() returns the number of incoming edges of the vertex identifier.
    int <id>.inDegree()

Label Functions

  1. hasLabel() checks if a vertex has a label.
    boolean <id>.hasLabel('<label>')

    The following example determines whether a vertex has the label "city":

    vertex.hasLabel('city')
  2. label() returns the label of an edge.
    string <id>.label()

    The following expression checks whether the label of an edge is "clicked_by":

    edge.label() = 'clicked_by'

Relational Expressions

To compare values (e.g., property values or constants), filter expressions provide the comparison operators listed below. Note: Both == and = are synonyms.

==
=
!=
<
<=
>
>=

The following example checks whether the "cost" property of the source vertex is lower than or equals to 1.23.

src.cost <= 1.23

Vertex ID Comparison

It is also possible to filter for vertices with a specific vertex ID.

<id> = <vertex_id>

The following example determines whether the source vertex of an edge has the vertex ID "San Francisco"

src = "San Francisco"

Regular Expressions

Strings can be matched using regular expressions.

<string expression> =~ '<regularexpression>'

The following example checks if the edge label starts with a lowercase letter and ends with a number:

edge.label() =~ '^[a-z].*[0-9]$'

Note:

The syntax followed for the pattern on the right-hand side, is Java REGEX.

Type Conversions

The following syntax allows converting the type of <expression> to <type>.

(<type>) <expression>

The following example converts the value of the 'cost' property of the source vertex to an integer value:

(int) src.cost

Boolean Expressions

Filter expressions can be composed to form other filter expressions. This can be done using the Boolean operators && (and), || (or) and ! (not).

Note:

Only boolean operands can be composed.
(! true) || false
edge.cost < INF && dst.visited = false
src.degree() < 10 || !(dst.visited)

Arithmetic Expressions

Any numeric expression can be combined using arithmetic expressions. The available arithmetic operators are: +, -, *, /, %.

Note:

These operators only work on numeric operands.
1 + 5
-vertex.degree()
edge.cost * 2 > 5
src.value * 2.5 = (dst.inDegree() + 5) / dst.outDegree()

Operator Precedence

Operator precedences are shown in the following list, from highest precedence to the lowest. An operator on a higher level is evaluated before an operator on a lower level.

  1. + (unary plus), - (unary minus)
  2. *,/, %
  3. +, -
  4. =,!=, <, >, <=, >=, =~
  5. NOT
  6. AND
  7. OR

Syntactic Sugar

both and any denote the source and destination vertex of the current edge. They can be used to express a condition that should be true for both or at least either one of the two vertices. These keywords are only valid in an edge filter expression. To use them in a vertex filter results in a runtime type-checking exception.

both
any

The filter expressions inside the following examples are equivalent:

both.property = 1
src.property = 1 && dst.property = 1
any.degree() > 1
src.degree() > 1 || dst.degree() > 1