Operators

String-Specific Comparison Operators

Data Preparation

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

STARTS WITH

Performs a case-sensitive prefix search on strings.

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

CONTAINS

Performs a case-sensitive substring search on strings.

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

ENDS WITH

Performs a case-sensitive suffix search on strings.

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

Regular Expressions

AGE supports POSIX regular expressions using the =~ operator, as documented in the PostgreSQL POSIX Regular Expression documentation. By default, =~ is case-sensitive.

Basic Exact String Match

When no special regex characters are present, =~ behaves identically to =.

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 Search

Prepend (?i) to the pattern to perform a case-insensitive match.

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

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

* Quantifier

The * quantifier 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

+ Quantifier

The + quantifier 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 *

The . and * can be combined to match the remainder 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 Precedence

The following table lists operator precedence in AGE, from highest to lowest:

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