最近一直在做圖資料庫的相關工作,對neo4j的查詢語言cypher使用較多,故在此總結記錄。cypher作為圖資料庫的查詢語言,感覺和關係型資料庫的查詢語言sql差不多吧。
1.如何找到乙個節點x,x以某種關係同時連線兩個不同節點a和b
match (a)-[r:relation]->(x)<-[r:relation]-(b) return x
2.如何找到節點a和b之間的最短路徑
(1)match p=shortestpath((a)-[r:relation]-(b)) return nodes(p)
(2)match(n:na),(m:nb)with n,m match p=shortestpath((n)-[r*…]-(m)) return p;
3.如何找到節點a和b之間以某種關係相連線的最短路徑
p=shortestpath((a)-[r:relationname]->(b)) return nodes(p)
4.找到資料庫**現的唯一節點標籤
match n return distinct labels(n)
5.找到資料庫**現的唯一關係型別
match n-[r]-() return distinct type(r)
6.找到資料庫中的唯一節點標籤和唯一關係型別
match n-[r]-() return distinct labels(n),type(r)
7.找到不與任何關係(或某種關係)向連的節點
start n = node() match n-[r:relationname]-() where r is null return n
8.找到某個帶有特定屬性的節點
start n=node() match n where has (n.someproperty) return n
9.找到與某種關係相連線的全部節點
start n= node() match n-[r:relationshipname]-() return distinct n
10.找到節點和它們的關係數,並以關係數目降序排列顯示
start n=node() match n-[r]-() return n,count(r) as rel_count order by rel_count desc
11.返回圖中所有節點的個數
start n = node() match n return count(n)
12.(1)刪除圖中關係:start n=node(*) match n-[r]-() delete r
(2)刪除圖中節點:start n =node(*) match n delete n
(3)刪除圖中所有東西:match (n) detach delete n
13.查詢某類節點下某屬性為特定值的節點
match (n:person)where n.name=」alice」 return n
14.with
cypher中的with關鍵字可以將前步查詢的結果作為後一步查詢的條件,這個在我的工作中可是幫了大忙哈哈。下面是兩個栗子。
(1)match(p:node_se)-[re:推理條件]->(q:node_se) where p.name=『fev1%pred』and p.value=』<30%』 with p,re,q match (q:node_se) <-[re2:推理條件]- (c:node_se)return p, re,q,re2,c
(2)match(p:node_patient)-[re:個人情況]->(q:node_se) where p.name=『qwe』 with p,re,q match (q:node_se) -[re2:推薦方案]-> (c:node_se) where q.name=『first』 with p, re,q,re2,c match (c:node_se)-[re3:方案細節]->(d:drugs) return p, re,q,re2,c,re3,d
15.查詢符合條件的某個節點的id
match(p) where p.name = 『***』 and p.value = 『***』 return id(p)
16.直接連線關係節點進行多層查詢
match(na:bank)-[re1]->(nb:company)-[re2]->(nc:people) return na,re1,nb,re2,nc
17.可以將查詢結果賦給變數,然後返回
match data=(na:bank)-[re1]->(nb:company)-[re2]->(nc:company) return data
18.變長路徑檢索
變長路徑的表示方式是:[*n…m],n和m表示路徑長度的最小值和最大值。
(a)-[ *2]->(b):表示路徑長度為2,起始節點是a,終止節點是b;
(a)-[ *3…5]->(b):表示路徑長度的最小值是3,最大值是5,起始節點是a,終止節點是b;
(a)-[ *…5]->(b):表示路徑長度的最大值是5,起始節點是a,終止節點是b;
(a)-[ *3…]->(b):表示路徑長度的最小值是3,起始節點是a,終止節點是b;
(a)-[ *]->(b):表示不限制路徑長度,起始節點是a,終止節點是b;
19.cypher對查詢的結果進行去重
慄:match(p:node_se)-[re]->(q)where re.name
<> 『and』 return distinct(re.name)
(注:栗子中的<>為cypher中的操作符之一,表示『不等於』)
20.更新節點的 labels
neo4j中的乙個節點可以有多個 label,返回所有節點的label:match (n) return labels(n)
修改節點的 label,可以先新加 label,再刪除舊的label
match (n:label_old) set n:label_new remove n:label_old
match(n:label_new) return labels(n)
21.更新節點的屬性
match(n:) set n.new_property = n.old_property remove n.old_proerty
先總結這些吧,感覺cypher還挺有意思的,專案中經常琢磨著要怎麼寫感覺像玩樂高一樣。
以上。
Neo4j 空間查詢
1.neo4j自帶函式進行簡單計算 neo4j自帶distance point1,point2 函式,返回乙個浮點數,表示同一座標參照系中兩點之間的測地距離,可以使用round函式來進行四捨五入。目前neo4j3.3版本不支援return直接返回point型別,3.4版本可以。節點型別 節點格式 詳...
neo4j查詢優化
1 使用match時,避免使用多個標籤。搜尋使用了2個標籤,同樣的事情竟然訪問了資料庫801次。這是因為cypher執行了額外的 haslabel 過濾器。可以通過使用專門的關係型別來解決這樣的問題,比如為他們新增published和drafted關係,然後用關係來找到指定使用者的published...
neo4j使用總結
埠配置 外掛程式配置 dbms.jvm.additional dunsupported.dbms.udc.source tarball org.neo4j.server.thirdparty jaxrs classes org.neo4j.tableau.wdc tableau wdc dbms.s...