DEV Community

Talha Munir ๐Ÿ‡ต๐Ÿ‡ธ
Talha Munir ๐Ÿ‡ต๐Ÿ‡ธ

Posted on

Operators in Apache age

Operators in Age

In the article we will talk about different operators in apache age:

String Specific Comparison Operators

Data Setup

Setting up sample data

SELECT * FROM cypher('tutorial', $$ CREATE (:Person {name: 'Talha'}), (:Person {name: 'Usman'}), (:Person {name: 'Irfan'}) $$) AS (result agtype); 
Enter fullscreen mode Exit fullscreen mode

Starts With

The operator performs case sensitive prefix searching on selected strings.

SELECT * FROM cypher('tutorial', $$ MATCH (v:Person) WHERE v.name STARTS WITH "T" RETURN v.name $$) AS (names agtype); 
Enter fullscreen mode Exit fullscreen mode

The above query returns the names of the persons starting with T.

End With

Performs case sensitive suffix searching on selected strings.
Query:

SELECT * FROM cypher('tutorial', $$ MATCH (v:Person) WHERE v.name ENDS WITH "a" RETURN v.name $$) AS (names agtype); 
Enter fullscreen mode Exit fullscreen mode

The above query will return the names of the people ended with a which in this case is 1.

Regular expressions:

Age also supports the use of POSIX regular expressions using the =~ operator.

Basic String Matching:

The =~ operator acts like the = operator when no special characters are provided.

SELECT * FROM cypher('tutorial', $$ MATCH (v:Person) WHERE v.name =~ 'Usman' RETURN v.name $$) AS (names agtype); 
Enter fullscreen mode Exit fullscreen mode

Case insensitive Search:

Adding ?! at the beginning of the string will make case insensitive comparisons.
Query:
The query below performs case insensitive search

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

. Wildcard

. operator acts as a wildcard to match any single character.
Query:
The below query will search for any character in our data

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

The * wildcard

  • wildcard see if it matches the zero o more of the previous character. Query:
SELECT * FROM cypher('graph_name', $$ MATCH (v:Person) WHERE v.name =~ 'Talo*a' RETURN v.name $$) AS (names agtype); 
Enter fullscreen mode Exit fullscreen mode

The above query will see whether the character behind * matches then prints the name, else print it by eliminating the o.

The + operator:

The + operator matches to 1 or more the previous character.
Query:

SELECT * FROM cypher('tutorial', $$ MATCH (v:Person) WHERE v.name =~ 'Usm+' RETURN v.name $$) AS (names agtype); 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)