Operators

更新时间:
复制 MD 格式

AGE supports the following operators for filtering and matching in Cypher queries:

Category Operators Case-sensitive
String comparison STARTS WITH, ENDS WITH, CONTAINS Yes
Regular expression =~ Yes (by default)

String comparison operators

Use these operators in a WHERE clause to filter nodes by string properties. All three operators are case-sensitive.

Operator Description Returns
a STARTS WITH b true if string a begins with b Boolean
a ENDS WITH b true if string a ends with b Boolean
a CONTAINS b true if string b appears anywhere in a Boolean

Sample data

The following examples use this graph:

SELECT * FROM cypher('graph_name', $$
CREATE (:Person {name: 'John'}),
       (:Person {name: 'Jeff'}),
       (:Person {name: 'Joan'}),
       (:Person {name: 'Bill'})
$$) AS (result agtype);

STARTS WITH

Returns nodes whose property begins with the specified prefix.

SELECT * FROM cypher('graph_name', $$
 MATCH (v:Person)
 WHERE v.name STARTS WITH "J"
 RETURN v.name
$$) AS (names agtype);

Result:

 names
--------
 "John"
 "Jeff"
 "Joan"
(3 rows)

ENDS WITH

Returns nodes whose property ends with the specified suffix.

SELECT * FROM cypher('graph_name', $$
 MATCH (v:Person)
 WHERE v.name ENDS WITH "n"
 RETURN v.name
$$) AS (names agtype);

Result:

 names
--------
 "John"
 "Joan"
(2 rows)

CONTAINS

Returns nodes whose property contains the specified substring.

SELECT * FROM cypher('graph_name', $$
 MATCH (v:Person)
 WHERE v.name CONTAINS "o"
 RETURN v.name
$$) AS (names agtype);

Result:

 names
--------
 "John"
 "Joan"
(2 rows)

Regular expressions

AGE matches strings against POSIX regular expressions using the =~ operator. The =~ operator is case-sensitive by default.

To make a match case-insensitive, add the (?i) flag at the start of the pattern.

All examples below use the same sample graph defined in Sample data.

Basic string matching

Without special characters, =~ behaves like the = operator.

SELECT * FROM cypher('graph_name', $$
 MATCH (v:Person)
 WHERE v.name =~ 'John'
 RETURN v.name
$$) AS (names agtype);

Result:

 names
--------
 "John"
(1 row)

Case-insensitive matching

Add (?i) at the start of the pattern to make the match case-insensitive.

SELECT * FROM cypher('graph_name', $$
 MATCH (v:Person)
 WHERE v.name =~ '(?i)JoHn'
 RETURN v.name
$$) AS (names agtype);

Result:

 names
--------
 "John"
(1 row)

Wildcard .

. matches any single character.

SELECT * FROM cypher('graph_name', $$
 MATCH (v:Person)
 WHERE v.name =~ 'Jo.n'
 RETURN v.name
$$) AS (names agtype);

Result:

 names
--------
 "John"
 "Joan"
(2 rows)

Wildcard *

* matches zero or more occurrences of the preceding character.

SELECT * FROM cypher('graph_name', $$
 MATCH (v:Person)
 WHERE v.name =~ 'Johz*n'
 RETURN v.name
$$) AS (names agtype);

Result:

 names
--------
 "John"
(1 row)

Operator +

+ matches one or more occurrences of the preceding character.

SELECT * FROM cypher('graph_name', $$
 MATCH (v:Person)
 WHERE v.name =~ 'Bil+'
 RETURN v.name
$$) AS (names agtype);

Result:

 names
--------
 "Bill"
(1 row)

Combining . and *

Use .* together to match the rest of a string.

SELECT * FROM cypher('graph_name', $$
 MATCH (v:Person)
 WHERE v.name =~ 'J.*'
 RETURN v.name
$$) AS (names agtype);

Result:

 names
--------
 "John"
 "Jeff"
 "Joan"
(3 rows)

Operator priority

The following table lists all operators in order of precedence (1 = highest).

Priority Operator Description
1 . Property access
2 [] Map and list subscripting
() Function call
3 STARTS WITH Case-sensitive prefix matching
ENDS WITH Case-sensitive suffix matching
CONTAINS Case-sensitive substring matching
=~ Regular expression matching
4 - Unary minus
5 IN Checks whether an element exists in the list
IS NULL Checks whether the value is NULL
IS NOT NULL Checks whether the value is not NULL
6 ^ Exponentiation
7 * / % Multiplication, division, and remainder
8 + - Addition and subtraction
9 = <> Equality and inequality
< <= Less than and less than or equal to
> >= Greater than and greater than or equal to
10 NOT Logical NOT
11 AND Logical AND
12 OR Logical OR