Show pageOld revisionsBacklinksBack to top This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. # Cypher Query {{ https://i.imgur.com/sffPY48.jpg }} ## Case sensitivity Case sensitive - Node labels (:Person) - Relationship types (:ACTED_IN) - Property keys (name) Case insensitive - Cypher keywords (MATCH, return) ## Aggregates Aggregate queries in Cypher are a little bit different than in SQL as we don't need to specify a grouping key. We implicitly group by any non aggregate fields in the RETURN statement. <code> // implicitly groups by p.name MATCH (p:Person)-[:ACTED_IN]->(m:Movie) RETURN p.name, count(*) AS numberOfMovies </code> ### Other aggregate functions https://neo4j.com/docs/cypher-refcard/current/ ## Constraints and Indexes ### Unique Constraints We create unique constraints to: - ensure uniqueness - allow fast lookup of nodes which match label-property pairs. <code> CREATE CONSTRAINT ON (label:Label) ASSERT label.property IS UNIQUE </code> There are three types of unique constraints: - Unique node property constraint - Node property existence constraint - Relationship property existence constraint <code> CREATE CONSTRAINT ON (label:Lable) ASSERT EXISTS(label.name) </code> <code> CREATE CONSTRAINT ON ()-[rel:REL_TYPE]->() ASSERT EXISTS(rel.name) </code> ### Indexes We create indexes to: - allow fast lookup of nodes which match label-property pairs. <code> CREATE INDEX ON :Label(property) </code> The following predicates use indexes: - Equality - STARTS WITH - CONTAINS - ENDS WITH - Range searches - (Non-) existence checks ### The MERGE Clause <code> MERGE (p:Person {name:'Tom Hanks', oscar: true}) RETURN p </code> There is not a :Person node with name:'Tom Hanks' and oscar:true in the graph, but there is a :Person with name:'Tom Hanks'. What do you think will happen here? <code> MERGE (p:Person {name:'Tom Hanks'}) SET p.oscar = true RETURN p </code> ## Write queries ### The CREATE Clause <code> CREATE (m:Movie {title:'Mystic River', released:2003}) RETURN m </code> ### The SET Clause <code> MATCH (m:Movie {title:'Mystic River'}) SET m.tagline = 'We bury our sins here, Dave. We wash them clean.' RETURN m </code> ### The CREATE Clause <code cypher> MATCH (m:Movie {title:'Mystic River'}) MATCH (p:Person {name:'Kevin Bacon'}) CREATE (p)-[r:ACTED_IN {role:['Sean']}]->(m) RETRUN p,r,m </code> ### ON CREATE and ON MATCH <code> MERGE (p:Person {name: 'Your Name'}) ON CREATE SET p.created = timestamp(), p.updated= 0 On MATCH SET p.updated = p.updated + 1 RETURN p.created, p.updated </code> ### Reading data from CSV with Cypher <code> [USING PREIODIC COMMIT] // optional transaction batching LOAD CSV // load csv data WITH HEADERS // optionally use first header row as keys in 'row' map FROM "url" // file:// URL relative to $NEO4J_HOME/import or http:// AS row // return each row of the CSV as list of strings or map FIELDTERMINATOR ";" // alternative field delimiter ... rest of the Cypher statement ... </code> ### Tabular CSV records to Graph structure Type the following command into the query pane in the browser: <code> :play http://guides.neo4j.com/fundamentals/import.html </code> ## Developing Applications with Neo4j and Cypher ### Available APIs - Remote with Drivers - [[Bolt]] Binary Protocol - Officially Supported Drivers - Cypher transactional HTTP Endpoint - neo4j.com/developer/language-guides - Native Java API - User Defined Procedures - Execute Cypher - Core Java API open/cypher-query.txt Last modified: 2024/10/05 06:15by 127.0.0.1