Cypher API
此文档主要介绍了TuGraph-Cypher的详细使用说明
1.Operators
1.1.Summary
Operators支持进度一览:
类别 | 支持 | 待支持 |
---|---|---|
General operators | DISTINCT , . for property access | [] for dynamic property access |
Mathematical operators | + , - , * , / , % , ^ | |
Comparison operators | = , <> , < , > , <= , >= , IS NULL , IS NOT NULL | |
String-specific comparison operators | STARTS WITH , ENDS WITH , CONTAINS , REGEXP | |
Boolean operators | AND , OR , XOR , NOT | |
String operators | + for concatenation | |
List operators | + for concatenation, IN to check existence of an element in a list, [] for accessing element(s) |
1.2.General operators
- ✓ Using the DISTINCT operator
MATCH (p:person) RETURN DISTINCT p.born
- ❏ Accessing properties of a nested literal map using the
.
operator
WITH {person: {name: 'Anne', age: 25}} AS p
RETURN p.person.name
- ❏ Filtering on a dynamically-computed property key using the
[]
operator
CREATE (a:Restaurant {name: 'Hungry Jo', rating_hygiene: 10, rating_food: 7}),
(b:Restaurant {name: 'Buttercup Tea Rooms', rating_hygiene: 5, rating_food:6}),
(c1:Category {name: 'hygiene'}), (c2:Category {name: 'food'})
MATCH (restaurant:Restaurant), (category:Category)
WHERE restaurant["rating_" + category.name] > 6
RETURN DISTINCT restaurant.name
1.3.Mathematical operators
- ✓ Using the exponentiation operator
^
WITH 2 AS number, 3 AS exponent
RETURN number ^ exponent AS result
- ✓ Using the unary minus operator
-
WITH -3 AS a, 4 AS b
RETURN b - a AS result
1.4.Comparison operators
- ✓ Comparing two numbers
WITH 4 AS one, 3 AS two
RETURN one > two AS result
1.5.String-specific comparison operators
- ✓ Using STARTS WITH to filter names
WITH ['John', 'Mark', 'Jonathan', 'Bill'] AS somenames
UNWIND somenames AS names
WITH names AS candidate
WHERE candidate STARTS WITH 'Jo'
RETURN candidate
- ✓ Using REGEXP to filter names
WITH ['John', 'Mark', 'Jonathan', 'Bill'] AS somenames
UNWIND somenames AS names
WITH names AS candidate
WHERE candidate REGEXP 'Jo.*n'
RETURN candidate
1.6.Boolean operators
- ✓ Using boolean operators to filter numbers
WITH [2, 4, 7, 9, 12] AS numberlist
UNWIND numberlist AS number
WITH number
WHERE number = 4 OR (number > 6 AND number < 10)
RETURN number
1.7.String operators
String operators comprise:
- ✓ concatenating strings:
+
1.8.List operators
- ✓ Concatenating two lists using +
RETURN [1,2,3,4,5]+[6,7] AS myList
- ✓ Using IN to check if a number is in a list
WITH [2, 3, 4, 5] AS numberlist
UNWIND numberlist AS number
WITH number
WHERE number IN [2, 3, 8]
RETURN number
- ✓ Accessing elements in a list using the [] operator
WITH ['Anne', 'John', 'Bill', 'Diane', 'Eve'] AS names
RETURN names[1..3] AS result
2.Clauses
2.1.Summary
Clauses支持进度一览:
类别 | 语法 | 备注 |
---|---|---|
Reading clauses | MATCH | 支持 |
OPTIONAL MATCH | 支持 | |
MANDATORY MATCH | 待支持 | |
Projecting clauses | RETURN … [AS] | 支持 |
WITH … [AS] | 支持 | |
UNWIND … [AS] | 支持 | |
Reading sub-clauses | WHERE | 支持 |
ORDER BY [ASC[ENDING] / DESC[ENDING]] | 支持 | |
SKIP | 支持 | |
LIMIT | 支持 | |
Writing clauses | CREATE | 支持 |
DELETE | 支持 | |
DETACH DELETE | 支持 | |
SET | 支持 | |
REMOVE | 支持 | |
Reading/Writing clauses | MERGE | 支持 |
CALL […YIELD] | 支持 | |
Set operations | UNION | 待支持 |
UNION ALL | 支持 |
2.2.MATCH
-
Basic node finding
- ✓ Get all nodes
MATCH (n)
RETURN n- ✓ Get all nodes with a label
MATCH (movie:movie)
RETURN movie.title- ✓ Related nodes
MATCH (person {name: 'Laurence Fishburne'})-[]-(movie)
RETURN movie.title- ✓ Match with labels
MATCH (:person {name: 'Laurence Fishburne'})-[]-(movie:movie)
RETURN movie.title -
Relationship basics
- ✓ Outgoing relationships
MATCH (:person {name: 'Laurence Fishburne'})-[]->(movie)
RETURN movie.title- ✓ Directed relationships and variable
MATCH (:person {name: 'Laurence Fishburne'})-[r]->(movie)
RETURN type(r)- ✓ Match on relationship type
MATCH (matrix:movie {title: 'The Matrix'})<-[:acted_in]-(actor)
RETURN actor.name- ✓ Match on multiple relationship types
MATCH (matrix {title: 'The Matrix'})<-[:acted_in|:directed]-(person)
RETURN person.name- ✓ Match on relationship type and use a variable
MATCH (matrix {title: 'The Matrix'})<-[r:acted_in]-(actor)
RETURN r.role -
Relationships in depth
- ❏ Relationship types with uncommon characters
MATCH (n {name: 'Rob Reiner'})-[r:`TYPE WITH SPACE`]->()
RETURN type(r)- ✓ Multiple relationships
MATCH (laurence {name: 'Laurence Fishburne'})-[:acted_in]->(movie)<-[:directed]-(director)
RETURN movie.title, director.name- ✓ Variable-length relationships
MATCH (laurence {name: 'Laurence Fishburne'})-[:acted_in*1..3]-(movie:movie)
RETURN movie.title- ✓ Relationship variable in variable-length relationships
MATCH p = (laurence {name: 'Laurence Fishburne'})-[:acted_in*2]-(co_actor)
RETURN p- ❏ Match with properties on a variable-length path
MATCH p = (charlie:person)-[* {blocked:false}]-(martin:person)
WHERE charlie.name = 'Charlie Sheen' AND martin.name = 'Martin Sheen'
RETURN p- ✓ Zero-length paths
MATCH (matrix:movie {title: 'The Matrix'})-[*0..1]-(x)
RETURN x- ✓ Named paths
MATCH p = (michael {name: 'Michael Douglas'})-[]->() RETURN p
- ✓ Matching on a bound relationship
MATCH (a)-[r]->(b)
WHERE euid(r)="0_3937_0_0_0"
RETURN a,b -
Shortest path
- ✓ Single shortest path
MATCH (martin:person {name: 'Carrie-Anne Moss'}), (laurence:person {name: 'Laurence Fishburne'})
CALL algo.shortestPath(martin, laurence) YIELD nodeCount,totalCost,path RETURN nodeCount,totalCost,path- ✓ All shortest paths
MATCH (martin:person {name: 'Carrie-Anne Moss'}), (laurence:person {name: 'Laurence Fishburne'}) WITH martin, laurence
CALL algo.allShortestPaths(martin, laurence) YIELD nodeIds,relationshipIds,cost RETURN nodeIds,relationshipIds,cost -
Get node or relationship by id
- ✓ Node by id
MATCH (n)
WHERE id(n)= 0
RETURN n- ✓ Relationship by id
MATCH ()-[r]->()
WHERE euid(r) = "0_3937_0_0_0"
RETURN r- ✓ Multiple nodes by id
MATCH (n)
WHERE id(n) IN [0, 3, 5]
RETURN n
2.3.RETURN
- ✓ Return nodes
MATCH (n {name: 'Carrie-Anne Moss'}) RETURN n
- ✓ Return relationships
MATCH (n {name: 'Carrie-Anne Moss'})-[r:acted_in]->(c)
RETURN r
- ✓ Return property
MATCH (n {name: 'Carrie-Anne Moss'}) RETURN n.born
- ❏ Return all elements
MATCH p = (a {name: 'A'})-[r]->(b)
RETURN *
- ❏ Variable with uncommon characters
MATCH (`This isn\'t a common variable`)
WHERE `This isn\'t a common variable`.name = 'A'
RETURN `This isn\'t a common variable`.happy
- ✓ Aliasing a field
MATCH (a {name: 'Carrie-Anne Moss'})
RETURN a.born AS SomethingTotallyDifferent
- ✓ Optional properties
MATCH (n)
RETURN n.age
- ❏ Other expressions
MATCH (a {name: 'Carrie-Anne Moss'})
RETURN a.born > 1900, "I'm a literal", (a)-[]->()
(a)-[]->()
not supported.
- ✓ Unique results
MATCH (a {name: 'Carrie-Anne Moss'})-[]->(b)
RETURN DISTINCT b
2.4.WHERE
-
Basic usage
- ✓ Boolean operations
MATCH (n)
WHERE n.name = 'Laurence Fishburne' XOR (n.born > 1965 AND n.name = 'Carrie-Anne Moss')
RETURN n.name, n.born- ✓ Filter on node label
MATCH (n)
WHERE n:person
RETURN n.name, n.born- ✓ Filter on node property
MATCH (n)
WHERE n.born > 2000
RETURN n.name, n.born- ✓ Filter on relationship property
MATCH (n)-[k:acted_in]->(f)
WHERE k.role = "Trinity"
RETURN f.title- ❏ Filter on dynamically-computed property
WITH 'AGE' AS propname
MATCH (n)
WHERE n[toLower(propname)]< 30
RETURN n.name, n.age- ✓ Property existence checking
MATCH (n)
WHERE exists(n.born)
RETURN n.name, n.born -
String matching
- ✓ Match the beginning of a string
MATCH (n)
WHERE n.name STARTS WITH 'Pet'
RETURN n.name, n.born- ✓ Match the ending of a string
MATCH (n)
WHERE n.name ENDS WITH 'ter'
RETURN n.name, n.born- ✓ Match anywhere within a string
MATCH (n)
WHERE n.name CONTAINS 'ete'
RETURN n.name, n.born- ✓ String matching negation
MATCH (n)
WHERE NOT n.name ENDS WITH 's'
RETURN n.name, n.born -
Using path patterns in
WHERE
- ❏ Filter on patterns
MATCH (tobias {name: 'Tobias'}), (others)
WHERE others.name IN ['Andres', 'Peter'] AND (tobias)<-[]-(others)
RETURN others.name, others.age- ❏ Filter on patterns using NOT
MATCH (persons), (peter {name: 'Peter'})
WHERE NOT (persons)-[]->(peter)
RETURN persons.name, persons.age- ❏ Filter on patterns with properties
MATCH (n)
WHERE (n)-[:KNOWS]-({name: 'Tobias'})
RETURN n.name, n.age- ✓ Filter on relationship type
MATCH (n)-[r]->()
WHERE n.name='Laurence Fishburne' AND type(r) STARTS WITH 'ac'
RETURN type(r), r.role -
Lists
- ✓ IN operator
MATCH (a)
WHERE a.name IN ['Laurence Fishburne', 'Tobias']
RETURN a.name, a.born -
Missing properties and values
- ✓ Default to false if property is missing
MATCH (n)
WHERE n.belt = 'white'
RETURN n.name, n.age, n.belt- ✓ Default to true if property is missing
MATCH (n)
WHERE n.belt = 'white' OR n.belt IS NULL RETURN n.name, n.age, n.belt
ORDER BY n.name- ✓ Filter on null
MATCH (person)
WHERE person.name = 'Peter' AND person.belt IS NULL RETURN person.name, person.age,
person.belt