All Products
Search
Document Center

:Expressions

Last Updated:Mar 21, 2024

Grammar rules

logical_attribute_expression := attribute_expression logical_operator attribute_expression 
logical_operator := AND | OR
relational_attribute_expression := attribute_expression relational_operator attribute_expression 
relational_operator := '=' | '>' | '<' | '<=' | '>=' | '!='
arithmatic_attribute_expression := attribute_expression arithmatic_operator attribute_expression | atomic_attribute_expression
arithmatic_operator := '+' | '-'|'*'|'/' 
atomic_attribute_expression := attribute_name | number_literal 
attribute_name := IDENTIFIER
number_literal := NUMBER
  • The fields of the attribute_expression expression can be of the numeric, STRING, or multi-value type.

  • An expression that contains fields of the STRING type supports all relational operations but does not support arithmetic operations.

  • You can use only the equality operator (=) and inequality operator (!=) in an expression that contains fields of the multi-value type.

    • If you use the equality operator (=) in an expression that contains fields of the multi-value type to define the relationship between a multi-value field and a condition value in a function, the function returns the values in the multi-value field that include the condition value.

Operators

+    -   *  /    >    <    =   !=   >=    <=    AND    OR    ()    @
  • @ indicates bitwise AND.

  • AND indicates logical AND.

Expressions

  • Description: Expressions are used to encapsulate the evaluation logic during the query process.

  • Note: All expressions support operations only between fields of the same type.

Arithmetic expressions

  • An arithmetic expression evaluates to a value of the non-BOOLEAN type.

  • Simple variables: such as a.

  • Basic operations: such as a + b, a + b - c, and a + b/c.

  • Constants: such as 119.

Logical expressions

  • A logical expression evaluates to a value of the BOOLEAN type.

  • Logical operators: AND and OR.

    • Format: a AND b, or a OR b.

    • Both a and b can be expressions. Example: uid=1000 OR uid=2000.

  • Relational operators: =, !=, >, >=, <, and <=.

    • Format: a > b, a < b, a = b, a <= b, or a >= b.

Optimization suggestions:

Up to 10 filter conditions can be specified for a query. If more than 10 filter conditions are specified, server performance issues may occur.

You can use functions such as in, notin, in_string, and notin_string to combine homogeneous filter conditions.

Mathematical function expressions

  • Trigonometry functions: sin, cos, tan, cot, asin, acos, atab, sinh, cosh, and tanh

    • Description: These functions are consistent with those of C++ math library.

    • Example: sin(a). In this example, a is an expression that contains fields of the numeric type.

  • Type conversion functions: to_double and to_int

    • to_double(a):

    • Description: The expression a that contains fields of the DOUBLE type is returned.

    • Note: a can be an expression that contains only a string of the numeric type.

    • to_int(a):

    • Description: The expression a that contains fields of the LONG type is returned.

    • Note: a can be an expression that contains only a string of the numeric type.

  • Mathematical functions: abs, ln, log10, exp, sqrt, trunc, ceil, floor, round, and pow(a, b)

    • Description: These functions are consistent with those of C++ math library.

  • Conditional function: IF

    • Format: if(bool_expression, a, b).

    • Description: If bool_expression is true, a is returned. If bool_expression is false, b is returned.

    • Note:

    • The bool_expression expression must evaluate to a value of the BOOLEAN type.

    • The types of a and b must remain the same. The returned value of the entire function must be of the same type as a.

    • Example: if(a > 10, b, c). If a > 10, b is returned. In other cases, c is returned.

  • Conditional function: CASE

    • Format: case(bool_expression, a, bool_expression, b, c...).

    • Description: This function judges which bool_expression expression is true by order, and then returns the value of the expression behind the bool_expression expression that is true. If no bool_expression is true, the value of the last expression is returned.

    • Note:

    • The CASE function must have an odd number of arguments.

    • The values of a, b, and c must be of the same type.

    • Example: case(a > 20, b, a > 10, c, a > 5, d, e). In the preceding example, if a > 20, b is returned. If 10 < a <= 20, c is returned. If 5 < a <= 10, d is returned. In other cases, e is returned.

  • Range functions: in, notin, in_string, and notin_string

    • Format: in(a, b) or notin(a, b).

    • Note:

    • a can be an expression that contains fields of the single-value or multi-value type. The multi-value type indicates that true is returned if one of the values of the a expression meets the conditions.

    • b must be a list of values of an expression that contains constant fields. The constant fields are separated by vertical bars (|).

    • The constant fields of a and b must be of the same type.

    • The returned value of this expression is of the BOOLEAN type.

    • Examples: in(nid, "10|12|13") and notin(tag_id, "101|203|405").

    • The expressions in this format are much better in performance than the expressions in the nid=10 OR nid=20 OR nid=30 format.

    • Format: in_string(filed_name, group_condition, separator).

    • Note:

    • filed_name is an expression that contains fields of the STRING type.

    • group_condition is a list of values of an expression that contains fields of the STRING type.

    • separator is the specified delimiter.

    • The returned value of this expression is of the BOOLEAN type.

    • Examples: in_string(name, "abc|cde|fgh", "|") and notin_string(tag, "aaa,bbb,ccc", ",").

  • Range counting function: count_in

    • Format: count_in(a, b).

    • Note:

    • a can be an expression that contains fields of the single-value or multi-value type. The multi-value type indicates that the function starts to count if one of the values of the a expression meets the conditions.

    • b must be a list of values of an expression that contains constant fields. The constant fields are separated by vertical bars (|).

    • The constant fields of a and b must be of the same type.

    • The returned value of this expression is of the INTEGER type.

    • Example: count_in(nid, "10|13|12|13").

    • In the preceding example, if nid is 10, a value of 1 is returned.

    • If nid is 13, a value of 2 is returned.

    • If nid is 12 and 13, a value of 3 is returned. The first value 12 hits once, and the second value 13 hits twice.

    • If nid is 13, 12, and 13, a value of 5 is returned. The first value 13 hits twice, the second value 12 hits once, and the third value 13 hits twice.

Example

Gremlin syntax

  • Expressions can perform logical, relational, and arithmetic operations on the attributes of an entity.

  • The filter() step and the step that can be modified by by() can use expressions.

g("thinkerpop").V("1;2;3;4;5;6").hasLabel("thinkerpop_modern_person").filter("age<30")
==> {"label":"thinkerpop_modern_person","age":29,"name":"marko","pk":"1"}
==> {"label":"thinkerpop_modern_person","age":27,"name":"vadas","pk":"2"}