1, summary
“Cypher” Is a descriptive class Sql Graph manipulation language . Equivalent to relational database Sql
, Its importance can be seen ! Its syntax is designed according to the characteristics of graph , Very convenient and flexible . No, Join, Is a major feature ! learn from good examples Cypher Is to learn well Neo4j Key to , It's also the core !
2,Neo4j Field type
Neo4j Field type
3,Neo4j basic operation
3.1 increase (create)
CREATE (erzi:Person {id:‘erzi’}), //erzi It's an alias (baba:Person {id:'baba'}),
(yeye:Person {id:'yeye',name:'zhangsan'}), (nainai:Person {id:'nainai'}),
(mama:Person {id:'mama'}), (bozi:Person {id:'bozi'}), (erzi)-[:fathor]->(baba),
(baba)-[:fathor]->(yeye), (baba)-[:mother]->(nainai), (erzi)-[:mother]->(mama),
(erzi)-[:girlFrend]->(bozi) explain : create (n:Person
{id:'20140101',name:' Wang Wu ',age:30,card:123456}) Equivalent to relationship Sql of : Create table Person(
id varchar2, name varchar2, age number, card number ); Insert into Person
values(‘20140101’,’ Wang Wu ’,30,123456);
3.2 check (match)
Match Query syntax Match amount to select MATCH (n:Person) RETURN n limit 25 Equivalent to : Select *
from Person limit 25 problem 1: How to add relationships to existing people ? Match (n:Person {id:'erzi'}),(f:Person
{id:'bozi'}) Merge (n)-[:fuqi]->(f) problem 2: Son and Bo Zhi are married ,relation How to modify ? Match (n:Person
),(f:Person) where n.id='erzi' and f.id='bozi' Merge (n)-[r:fuqi]->(f) #
more Neo4j A way of writing Match (n:Person{id:'erzi'}),(f:Person{id:'bozi'}) Merge
(n)-[r:fuqi]->(f) explain :merge Used to create relationships , If it already exists , Can be used as a query #
Query and baba This node is fathor Other nodes of the relationship , And return to view these two nodes match (n:Person {id:'baba'})-[:fathor]-(f)
return n,f # Query and baba Of this node fathor, And return to view these two nodes match (n:Person
{id:'baba'})-[:fathor]->(f) return n,f # Query and baba Of this node fathor, Return only baba Of this node fathor
match (n:Person {id:'baba'})-[:fathor]-(f) return f
3.3, change (set)
Cypher None in Update, use set replace # Update properties Match (n:Person {id:'baba'}) set n.name=' Zhang San '
return n # Property names are created automatically when writing data , nothing schme characteristic , This is the same no-sql library ; Support unstructured data ; Unstructured : Data in different rows can have different numbers of columns ;
Match (n:Person {id:'baba'}) set n.name=' Zhang San ',n.age=50 return n
explain :Cypher In language , Any grammar can have return
3.4 Delete (delete,remove)
DELETE and REMOVE Main differences : ① DELETE Operations are used to delete nodes and relation( For graph structure ). ②
REMOVE Action is used to delete a label label And properties ( For Relational Structures ). explain :Remove label Equivalent to drop
table; Both commands should be associated with MATCH Use with command . Match (n:Person {id:'baba'}) remove n.age return n
MATCH (s:Teacher)-[r:teach]->(d:Student) delete r,s,d // Delete the teachers and students associated with the relationship and label
MATCH (n:Test) remove n:Test // delete label # How to delete only one relation? Match
(a:Person),(b:Person) where a.id='erzi' and b.id='bozi' merge (a)-[r:FUQI]->(b)
DELETE r # Or more Neo4j Written as Match (a:Person{id:'erzi'}),(b:Person{id:'bozi'}) merge
(a)-[r:FUQI]->(b) DELETE r # Delete all records MATCH (n) OPTIONAL MATCH (n)-[r]-() DELETE
n,r
3.5 sort (order by)
# Same relationship sql MATCH (n:Person) RETURN n order by n.id,n.name desc LIMIT 25 # positive sequence
MATCH (n:Person) RETURN n order by n.id LIMIT 25
3.6 Restricted display (limit)
MATCH (n:Customer) RETURN n LIMIT 25
3.7 Before skipping n strip (skip)
MATCH (n:Person) RETURN n order by n.id desc skip 2 LIMIT 25
3.8 union and union all
# Same relationship sql # Union: Multi segment Match of return result Combine online into a result set , Duplicate lines will be automatically removed ; # Union
all: Same function union, But don't lose weight ; MATCH (n:Person) where n.age>20 RETURN n.id,n.age union all
MATCH (n:Person) where n.id='erzi' RETURN n.id,n.age
3.9 Null
# Where attribute is null, In fact, the same relationship sql grammar , Is to make the results “ and ” MATCH (n:Person) where n.age>20
RETURN n.id,n.age union all MATCH (n:Person) where n.id='erzi' and n.age is not
null RETURN n.id,n.age
3.10 in
# Brackets identify the range of a field MATCH (n:Person) where n.age>20 RETURN n.id,n.age union all
MATCH (n:Person) where n.id in ['erzi','bozi','baba'] RETURN n.id,n.age
3.11 built-in id
Each node or relation There is a system assigned id, from 0 Start increment , Globally unique ! Create (a:Person {id:’123’})
// there ID Is an attribute , And built-in ID It's two different things Through function id(node/relation) Available id value ; Opaque , like Oracle Inside rowid;
User definable id attribute , With built-in id irrelevant ;
3.12 Relation Directional
# Create When the relationship between nodes , Direction must be specified , Otherwise, an error will be reported # No direction can be specified during query , Any direction is OK MATCH
(n:Person)-[:FUQI]-(s:Person) RETURN distinct n,s
3.13 Indexes (index)
# The complexity before and after indexing is different O(n),O(1) create index on :Person(id); drop index on
:Person(id); Which fields are indexed ? According to query needs , Index fields with multiple queries . create index on :Person(name);
1, There is no need to give a name to the claim , You only need to set the index field ; 2, All queries through this field are indexed where in substring
relationship DB in : If the index field is set with a layer of functions , Basically don't go .
3.14 Implementation plan (explain)
# Used to track the process of displaying queries explain MATCH p=()-[r:ORDERS]->() RETURN p LIMIT 25 explain
3.15 Attribute unique constraint CONSTRAINT
# When indexing attributes , The same attribute value cannot correspond to two records , In fact, it is the uniqueness constraint requirement of attributes # adopt CONSTRAINT Property can be set , Cancel uniqueness requirement CREATE
CONSTRAINT ON (a:Person) ASSERT a.id IS UNIQUE Drop CONSTRAINT ON (a:Person)
ASSERT a.id IS UNIQUE
3.16 Common functions
# Use and check immediately Function function describe UPPER It is used to change all letters to uppercase letters . LOWER It is used to change all letters to lowercase . SUBSTRING
It is used to get a given String Substring of . REPLACE It is used to replace a substring of a string . Match (n:Person) return
SUBSTRING(n.id,2,0),n.id Aggregate function describe COUNT It returns by MATCH Number of lines returned by the command . MAX
It from MATCH A set of lines returned by the command returns the maximum value . MIN It returns by MATCH The smallest set of values returned by the command line . SUM It returns by MATCH The sum value of all lines returned by the command . AVG
It returns by MATCH The average of all lines returned by the command . # Neo4j nothing group by, Execute in the following manner Match (n:Person) return
count(*) Match (n:Person) return avg(n.age) Contains only age Not empty node
3.17 Query shortest path (shortestPath)
# Return all shortest paths allShortestPaths # [*..n]
Used to represent acquisition n Layer relationship , The distance between grandma and mom is <=3 Shortest path , The two points are connected # Its distance is 1, When there are multiple shortest paths , Random return only 1 individual match
p=shortestPath((n:Person {id:'mama'})-[*..3]-(b:Person {id:'nainai'})) return p
# The shorter the relationship link , The closer the relationship between the two nodes !
Technology
Daily Recommendation