Neo4j学习2:概念、数据展示、CQL使用
文章目录
概念
概念
节点:关系、属性的数据
节点、关系都含有各自的属性
关系连接节点
属性是键值对
节点用圆圈表示、关系用方向连接节点
关系有单向、双向
关系包含开始节点、结束节点
标签:简单理解就是Java的类名
数据展示
修改图中的显示字段
默认: 使用 节点 或者 关系 中的 name 属性 进行展示 在图结果 中
CQL语法使用(Cypher Query Language)
作用: 类似关系型数据库的SQL,用于查询图中的节点、关系、统计等数据
官方文档介绍:https://neo4j.com/docs/cypher-manual/current/clauses/
特点
声明性模型匹配语言
遵循部分SQL语法,只要你用过SQL,CQL的语法就能熟悉
常用语法关键字汇总
| CQL命令 | 作用 |
|---|---|
| CREATE | 创建节点,关系和属性 |
| MATCH | 检索有关节点,关系和属性数据 |
| RETURN | 返回查询结果 |
| WHERE | 提供条件过滤检索数据 |
| DELETE | 删除节点和关系 |
| REMOVE | 删除节点和关系的属性 |
| ORDER BY | 排序检索数据 |
| SET | 添加或更新标签 |
| SKIP LIMIT | 分页, 从skip位置开始输出,最多输出limit个元素 |
| DISTINCT | 去重 |
| () | 节点,使用小括号进行表示节点内容信息 |
| [] | 关系,使用中括号进行表示关系内容信息 |
| -> | 关系与节点的关系方向,箭头表示 关系跟节点的顺序方向 |
节点创建create
//语法// 实例名 == 就是官方说的 节点名// 类名 == 就是官方说的 节点标签名// 1. 新节点创建 == 多个类名使用冒号拼接 create (实例名:类名 {实例成员变量名1:实例成员变量值1, 实例成员变量名2:实例成员变量值2})// 示例 create (test:Person:Movie:Beauty{ name:'linrc'})// 2. 节点以及节点关系创建 create (实例名1:类名 {实例成员变量名1:实例成员变量值1, 实例成员变量名2:实例成员变量值2})-[关系实例名:关系类名 {关系实例成员变量名1:关系实例成员变量值1, 关系实例成员变量名2:关系实例成员变量值2}]-(实例名2:类名 {实例成员变量名1:实例成员变量值1, 实例成员变量名2:实例成员变量值2})//示例 create (person1:Person{name:'linrc1'})-[:Colleagues{company:'电信总公司'}]->(person2:Person{name:'linrc2'})// 3. 已有节点 进行关系创建 match (实例名:类名 {实例成员变量名1:实例成员变量值1, 实例成员变量名2:实例成员变量值2}),(实例名2:类名 {实例成员变量名1:实例成员变量值1, 实例成员变量名2:实例成员变量值2}) create (实例名)-[关系实例名:关系类名 {关系实例成员变量名1:关系实例成员变量值1, 关系实例成员变量名2:关系实例成员变量值2}]-(实例名2)//示例 match (person1:Person{name:'linrc1'}),(person2:Person{name:'linrc2'}) create (person1)-[friend:Friend{Intimacy:'99.99%'}]->(person2)//官方示例数据 CREATE (TheMatrix:Movie{title:'TheMatrix', released:1999, tagline:'WelcometotheRealWorld'}) CREATE (Keanu:Person{name:'KeanuReeves', born:1964}) CREATE (Carrie:Person{name:'Carrie-AnneMoss', born:1967}) CREATE (Laurence:Person{name:'LaurenceFishburne', born:1961}) CREATE (Hugo:Person{name:'HugoWeaving', born:1960}) CREATE (LillyW:Person{name:'LillyWachowski', born:1967}) CREATE (LanaW:Person{name:'LanaWachowski', born:1965}) CREATE (JoelS:Person{name:'JoelSilver', born:1952}) CREATE (Keanu)-[:ACTED_IN {roles:['Neo']}]->(TheMatrix),(Carrie)-[:ACTED_IN {roles:['Trinity']}]->(TheMatrix),(Laurence)-[:ACTED_IN {roles:['Morpheus']}]->(TheMatrix),(Hugo)-[:ACTED_IN {roles:['AgentSmith']}]->(TheMatrix),(LillyW)-[:DIRECTED]->(TheMatrix),(LanaW)-[:DIRECTED]->(TheMatrix),(JoelS)-[:PRODUCED]->(TheMatrix) CREATE (Emil:Person{name:"Emil Eifrem", born:1978}) CREATE (Emil)-[:ACTED_IN {roles:["Emil"]}]->(TheMatrix) CREATE (TheMatrixReloaded:Movie{title:'TheMatrixReloaded', released:2003, tagline:'Free your mind'}) CREATE (Keanu)-[:ACTED_IN {roles:['Neo']}]->(TheMatrixReloaded),(Carrie)-[:ACTED_IN {roles:['Trinity']}]->(TheMatrixReloaded),(Laurence)-[:ACTED_IN {roles:['Morpheus']}]->(TheMatrixReloaded),(Hugo)-[:ACTED_IN {roles:['AgentSmith']}]->(TheMatrixReloaded),(LillyW)-[:DIRECTED]->(TheMatrixReloaded),(LanaW)-[:DIRECTED]->(TheMatrixReloaded),(JoelS)-[:PRODUCED]->(TheMatrixReloaded) CREATE (TheMatrixRevolutions:Movie{title:'TheMatrixRevolutions', released:2003, tagline:'Everything that has a beginning has an end'}) CREATE (Keanu)-[:ACTED_IN {roles:['Neo']}]->(TheMatrixRevolutions),(Carrie)-[:ACTED_IN {roles:['Trinity']}]->(TheMatrixRevolutions),(Laurence)-[:ACTED_IN {roles:['Morpheus']}]->(TheMatrixRevolutions),(Hugo)-[:ACTED_IN {roles:['AgentSmith']}]->(TheMatrixRevolutions),(LillyW)-[:DIRECTED]->(TheMatrixRevolutions),(LanaW)-[:DIRECTED]->(TheMatrixRevolutions),(JoelS)-[:PRODUCED]->(TheMatrixRevolutions) CREATE (TheDevilsAdvocate:Movie{title:"The Devil's Advocate", released:1997, tagline:'Evil has its winning ways'}) CREATE (Charlize:Person{name:'CharlizeTheron', born:1975}) CREATE (Al:Person{name:'AlPacino', born:1940}) CREATE (Taylor:Person{name:'TaylorHackford', born:1944}) CREATE (Keanu)-[:ACTED_IN {roles:['KevinLomax']}]->(TheDevilsAdvocate),(Charlize)-[:ACTED_IN {roles:['MaryAnnLomax']}]->(TheDevilsAdvocate),(Al)-[:ACTED_IN {roles:['JohnMilton']}]->(TheDevilsAdvocate),(Taylor)-[:DIRECTED]->(TheDevilsAdvocate) CREATE (AFewGoodMen:Movie{title:"A Few Good Men", released:1992, tagline:"In the heart of the nation's capital, in a courthouse of the U.S. government, one man will stop at nothing to keep his honor, and one will stop at nothing to find the truth."}) CREATE (TomC:Person{name:'TomCruise', born:1962}) CREATE (JackN:Person{name:'JackNicholson', born:1937}) CREATE (DemiM:Person{name:'DemiMoore', born:1962}) CREATE (KevinB:Person{name:'KevinBacon', born:1958}) CREATE (KieferS:Person{name:'KieferSutherland', born:1966}) CREATE (NoahW:Person{name:'NoahWyle', born:1971}) CREATE (CubaG:Person{name:'CubaGoodingJr.', born:1968}) CREATE (KevinP:Person{name:'KevinPollak', born:1957}) CREATE (JTW:Person{name:'J.T. Walsh', born:1943}) CREATE (JamesM:Person{name:'JamesMarshall', born:1967}) CREATE (ChristopherG:Person{name:'ChristopherGuest', born:1948}) CREATE (RobR:Person{name:'RobReiner', born:1947}) CREATE (AaronS:Person{name:'AaronSorkin', born:1961}) CREATE (TomC)-[:ACTED_IN {roles:['Lt. DanielKaffee']}]->(AFewGoodMen),(JackN)-[:ACTED_IN {roles:['Col. NathanR. Jessup']}]->(AFewGoodMen),(DemiM)-[:ACTED_IN {roles:['Lt. Cdr. JoAnneGalloway']}]->(AFewGoodMen),(KevinB)-[:ACTED_IN {roles:['Capt. JackRoss']}]->(AFewGoodMen),(KieferS)-[:ACTED_IN {roles:['Lt. JonathanKendrick']}]->(AFewGoodMen),(NoahW)-[:ACTED_IN {roles:['Cpl. JeffreyBarnes']}]->(AFewGoodMen),(CubaG)-[:ACTED_IN {roles:['Cpl. CarlHammaker']}]->(AFewGoodMen),(KevinP)-[:ACTED_IN {roles:['Lt. SamWeinberg']}]->(AFewGoodMen),(JTW)-[:ACTED_IN {roles:['Lt. Col. MatthewAndrewMarkinson']}]->(AFewGoodMen),(JamesM)-[:ACTED_IN {roles:['Pfc. LoudenDowney']}]->(AFewGoodMen),(ChristopherG)-[:ACTED_IN {roles:['Dr. Stone']}]->(AFewGoodMen),(AaronS)-[:ACTED_IN {roles:['Man in Bar']}]->(AFewGoodMen),(RobR)-[:DIRECTED]->(AFewGoodMen),(AaronS)-[:WROTE]->(AFewGoodMen) CREATE (TopGun:Movie{title:"Top Gun", released:1986, tagline:'I feel the need, the need for speed.'}) CREATE (KellyM:Person{name:'KellyMcGillis', born:1957}) CREATE (ValK:Person{name:'ValKilmer', born:1959}) CREATE (AnthonyE:Person{name:'AnthonyEdwards', born:1962}) CREATE (TomS:Person{name:'TomSkerritt', born:1933}) CREATE (MegR:Person{name:'MegRyan', born:1961}) CREATE (TonyS:Person{name:'TonyScott', born:1944}) CREATE (JimC:Person{name:'JimCash', born:1941}) CREATE (TomC)-[:ACTED_IN {roles:['Maverick']}]->(TopGun),(KellyM)-[:ACTED_IN {roles:['Charlie']}]->(TopGun),(ValK)-[:ACTED_IN {roles:['Iceman']}]->(TopGun),(AnthonyE)-[:ACTED_IN {roles:['Goose']}]->(TopGun),(TomS)-[:ACTED_IN {roles:['Viper']}]->(TopGun),(MegR)-[:ACTED_IN {roles:['Carole']}]->(TopGun),(TonyS)-[:DIRECTED]->(TopGun),(JimC)-[:WROTE]->(TopGun) CREATE (JerryMaguire:Movie{title:'JerryMaguire', released:2000, tagline:'The rest of his life begins now.'}) CREATE (ReneeZ:Person{name:'ReneeZellweger', born:1969}) CREATE (KellyP:Person{name:'KellyPreston', born:1962}) CREATE (JerryO:Person{name:"Jerry O'Connell", born:1974}) CREATE (JayM:Person{name:'JayMohr', born:1970}) CREATE (BonnieH:Person{name:'BonnieHunt', born:1961}) CREATE (ReginaK:Person{name:'ReginaKing', born:1971}) CREATE (JonathanL:Person{name:'JonathanLipnicki', born:1996}) CREATE (CameronC:Person{name:'CameronCrowe', born:1957}) CREATE (TomC)-[:ACTED_IN {roles:['JerryMaguire']}]->(JerryMaguire),(CubaG)-[:ACTED_IN {roles:['RodTidwell']}]->(JerryMaguire),(ReneeZ)-[:ACTED_IN {roles:['DorothyBoyd']}]->(JerryMaguire),(KellyP)-[:ACTED_IN {roles:['AveryBishop']}]->(JerryMaguire),(JerryO)-[:ACTED_IN {roles:['FrankCushman']}]->(JerryMaguire),(JayM)-[:ACTED_IN {roles:['BobSugar']}]->(JerryMaguire),(BonnieH)-[:ACTED_IN {roles:['LaurelBoyd']}]->(JerryMaguire),(ReginaK)-[:ACTED_IN {roles:['MarceeTidwell']}]->(JerryMaguire),(JonathanL)-[:ACTED_IN {roles:['RayBoyd']}]->(JerryMaguire),(CameronC)-[:DIRECTED]->(JerryMaguire),(CameronC)-[:PRODUCED]->(JerryMaguire),(CameronC)-[:WROTE]->(JerryMaguire) CREATE (StandByMe:Movie{title:"Stand By Me", released:1986, tagline:"For some, it's the last real taste of innocence, and the first real taste of life. But for everyone, it's the time that memories are made of."}) CREATE (RiverP:Person{name:'RiverPhoenix', born:1970}) CREATE (CoreyF:Person{name:'CoreyFeldman', born:1971}) CREATE (WilW:Person{name:'WilWheaton', born:1972}) CREATE (JohnC:Person{name:'JohnCusack', born:1966}) CREATE (MarshallB:Person{name:'MarshallBell', born:1942}) CREATE (WilW)-[:ACTED_IN {roles:['GordieLachance']}]->(StandByMe),(RiverP)-[:ACTED_IN {roles:['ChrisChambers']}]->(StandByMe),(JerryO)-[:ACTED_IN {roles:['VernTessio']}]->(StandByMe),(CoreyF)-[:ACTED_IN {roles:['TeddyDuchamp']}]->(StandByMe),(JohnC)-[:ACTED_IN {roles:['DennyLachance']}]->(StandByMe),(KieferS)-[:ACTED_IN {roles:['AceMerrill']}]->(StandByMe),(MarshallB)-[:ACTED_IN {roles:['Mr. Lachance']}]->(StandByMe),(RobR)-[:DIRECTED]->(StandByMe) CREATE (AsGoodAsItGets:Movie{title:'AsGood as ItGets', released:1997, tagline:'A comedy from the heart that goes for the throat.'}) CREATE (HelenH:Person{name:'HelenHunt', born:1963}) CREATE (GregK:Person{name:'GregKinnear', born:1963}) CREATE (JamesB:Person{name:'JamesL. Brooks', born:1940}) CREATE (JackN)-[:ACTED_IN {roles:['MelvinUdall']}]->(AsGoodAsItGets),(HelenH)-[:ACTED_IN {roles:['CarolConnelly']}]->(AsGoodAsItGets),(GregK)-[:ACTED_IN {roles:['SimonBishop']}]->(AsGoodAsItGets),(CubaG)-[:ACTED_IN {roles:['FrankSachs']}]->(AsGoodAsItGets),(JamesB)-[:DIRECTED]->(AsGoodAsItGets) CREATE (WhatDreamsMayCome:Movie{title:'WhatDreamsMayCome', released:1998, tagline:'After life there is more. The end is just the beginning.'}) CREATE (AnnabellaS:Person{name:'AnnabellaSciorra', born:1960}) CREATE (MaxS:Person{name:'Max von Sydow', born:1929}) CREATE (WernerH:Person{name:'WernerHerzog', born:1942}) CREATE (Robin:Person{name:'RobinWilliams', born:1951}) CREATE (VincentW:Person{name:'VincentWard', born:1956}) CREATE (Robin)-[:ACTED_IN {roles:['ChrisNielsen']}]->(WhatDreamsMayCome),(CubaG)-[:ACTED_IN {roles:['AlbertLewis']}]->(WhatDreamsMayCome),(AnnabellaS)-[:ACTED_IN {roles:['AnnieCollins-Nielsen']}]->(WhatDreamsMayCome),(MaxS)-[:ACTED_IN {roles:['TheTracker']}]->(WhatDreamsMayCome),(WernerH)-[:ACTED_IN {roles:['TheFace']}]->(WhatDreamsMayCome),(VincentW)-[:DIRECTED]->(WhatDreamsMayCome) CREATE (SnowFallingonCedars:Movie{title:'SnowFalling on Cedars', released:1999, tagline:'First loves last. Forever.'}) CREATE (EthanH:Person{name:'EthanHawke', born:1970}) CREATE (RickY:Person{name:'RickYune', born:1971}) CREATE (JamesC:Person{name:'JamesCromwell', born:1940}) CREATE (ScottH:Person{name:'ScottHicks', born:1953}) CREATE (EthanH)-[:ACTED_IN {roles:['IshmaelChambers']}]->(SnowFallingonCedars),(RickY)-[:ACTED_IN {roles:['KazuoMiyamoto']}]->(SnowFallingonCedars),(MaxS)-[:ACTED_IN {roles:['NelsGudmundsson']}]->(SnowFallingonCedars),(JamesC)-[:ACTED_IN {roles:['JudgeFielding']}]->(SnowFallingonCedars),(ScottH)-[:DIRECTED]->(SnowFallingonCedars) CREATE (YouveGotMail:Movie{title:"You've Got Mail", released:1998, tagline:'At odds in life... in love on-line.'}) CREATE (ParkerP:Person{name:'ParkerPosey', born:1968}) CREATE (DaveC:Person{name:'DaveChappelle', born:1973}) CREATE (SteveZ:Person{name:'SteveZahn', born:1967}) CREATE (TomH:Person{name:'TomHanks', born:1956}) CREATE (NoraE:Person{name:'NoraEphron', born:1941}) CREATE (TomH)-[:ACTED_IN {roles:['JoeFox']}]->(YouveGotMail),(MegR)-[:ACTED_IN {roles:['KathleenKelly']}]->(YouveGotMail),(GregK)-[:ACTED_IN {roles:['FrankNavasky']}]->(YouveGotMail),(ParkerP)-[:ACTED_IN {roles:['PatriciaEden']}]->(YouveGotMail),(DaveC)-[:ACTED_IN {roles:['KevinJackson']}]->(YouveGotMail),(SteveZ)-[:ACTED_IN {roles:['GeorgePappas']}]->(YouveGotMail),(NoraE)-[:DIRECTED]->(YouveGotMail) CREATE (SleeplessInSeattle:Movie{title:'Sleepless in Seattle', released:1993, tagline:'Whatif someone you never met, someone you never saw, someone you never knew was the only someone for you?'}) CREATE (RitaW:Person{name:'RitaWilson', born:1956}) CREATE (BillPull:Person{name:'BillPullman', born:1953}) CREATE (VictorG:Person{name:'VictorGarber', born:1949}) CREATE (RosieO:Person{name:"Rosie O'Donnell", born:1962}) CREATE (TomH)-[:ACTED_IN {roles:['SamBaldwin']}]->(SleeplessInSeattle),(MegR)-[:ACTED_IN {roles:['AnnieReed']}]->(SleeplessInSeattle),(RitaW)-[:ACTED_IN {roles:['Suzy']}]->(SleeplessInSeattle),(BillPull)-[:ACTED_IN {roles:['Walter']}]->(SleeplessInSeattle),(VictorG)-[:ACTED_IN {roles:['Greg']}]->(SleeplessInSeattle),(RosieO)-[:ACTED_IN {roles:['Becky']}]->(SleeplessInSeattle),(NoraE)-[:DIRECTED]->(SleeplessInSeattle) CREATE (JoeVersustheVolcano:Movie{title:'JoeVersus the Volcano', released:1990, tagline:'A story of love, lava and burning desire.'}) CREATE (JohnS:Person{name:'JohnPatrickStanley', born:1950}) CREATE (Nathan:Person{name:'NathanLane', born:1956}) CREATE (TomH)-[:ACTED_IN {roles:['JoeBanks']}]->(JoeVersustheVolcano),(MegR)-[:ACTED_IN {roles:['DeDe', 'AngelicaGraynamore', 'PatriciaGraynamore']}]->(JoeVersustheVolcano),(Nathan)-[:ACTED_IN {roles:['Baw']}]->(JoeVersustheVolcano),(JohnS)-[:DIRECTED]->(JoeVersustheVolcano) CREATE (WhenHarryMetSally:Movie{title:'WhenHarryMetSally', released:1998, tagline:'Can two friends sleep together and still love each other in the morning?'}) CREATE (BillyC:Person{name:'BillyCrystal', born:1948}) CREATE (CarrieF:Person{name:'CarrieFisher', born:1956}) CREATE (BrunoK:Person{name:'BrunoKirby', born:1949}) CREATE (BillyC)-[:ACTED_IN {roles:['HarryBurns']}]->(WhenHarryMetSally),(MegR)-[:ACTED_IN {roles:['SallyAlbright']}]->(WhenHarryMetSally),(CarrieF)-[:ACTED_IN {roles:['Marie']}]->(WhenHarryMetSally),(BrunoK)-[:ACTED_IN {roles:['Jess']}]->(WhenHarryMetSally),(RobR)-[:DIRECTED]->(WhenHarryMetSally),(RobR)-[:PRODUCED]->(WhenHarryMetSally),(NoraE)-[:PRODUCED]->(WhenHarryMetSally),(NoraE)-[:WROTE]->(WhenHarryMetSally) CREATE (ThatThingYouDo:Movie{title:'ThatThingYouDo', released:1996, tagline:'In every life there comes a time when that thing you dream becomes that thing you do'}) CREATE (LivT:Person{name:'LivTyler', born:1977}) CREATE (TomH)-[:ACTED_IN {roles:['Mr. White']}]->(ThatThingYouDo),(LivT)-[:ACTED_IN {roles:['FayeDolan']}]->(ThatThingYouDo),(Charlize)-[:ACTED_IN {roles:['Tina']}]->(ThatThingYouDo),(TomH)-[:DIRECTED]->(ThatThingYouDo) CREATE (TheReplacements:Movie{title:'TheReplacements', released:2000, tagline:'Pain heals,Chicks dig scars...Glory lasts forever'}) CREATE (Brooke:Person{name:'BrookeLangton', born:1970}) CREATE (Gene:Person{name:'GeneHackman', born:1930}) CREATE (Orlando:Person{name:'OrlandoJones', born:1968}) CREATE (Howard:Person{name:'HowardDeutch', born:1950}) CREATE (Keanu)-[:ACTED_IN {roles:['ShaneFalco']}]->(TheReplacements),(Brooke)-[:ACTED_IN {roles:['AnnabelleFarrell']}]->(TheReplacements),(Gene)-[:ACTED_IN {roles:['JimmyMcGinty']}]->(TheReplacements),(Orlando)-[:ACTED_IN {roles:['CliffordFranklin']}]->(TheReplacements),(Howard)-[:DIRECTED]->(TheReplacements) CREATE (RescueDawn:Movie{title:'RescueDawn', released:2006, tagline:"Based on the extraordinary true story of one man's fight for freedom"}) CREATE (ChristianB:Person{name:'ChristianBale', born:1974}) CREATE (ZachG:Person{name:'ZachGrenier', born:1954}) CREATE (MarshallB)-[:ACTED_IN {roles:['Admiral']}]->(RescueDawn),(ChristianB)-[:ACTED_IN {roles:['DieterDengler']}]->(RescueDawn),(ZachG)-[:ACTED_IN {roles:['SquadLeader']}]->(RescueDawn),(SteveZ)-[:ACTED_IN {roles:['Duane']}]->(RescueDawn),(WernerH)-[:DIRECTED]->(RescueDawn) CREATE (TheBirdcage:Movie{title:'TheBirdcage', released:1996, tagline:'Come as you are'}) CREATE (MikeN:Person{name:'MikeNichols', born:1931}) CREATE (Robin)-[:ACTED_IN {roles:['ArmandGoldman']}]->(TheBirdcage),(Nathan)-[:ACTED_IN {roles:['AlbertGoldman']}]->(TheBirdcage),(Gene)-[:ACTED_IN {roles:['Sen. KevinKeeley']}]->(TheBirdcage),(MikeN)-[:DIRECTED]->(TheBirdcage) CREATE (Unforgiven:Movie{title:'Unforgiven', released:1992, tagline:"It's a hell of a thing, killing a man"}) CREATE (RichardH:Person{name:'RichardHarris', born:1930}) CREATE (ClintE:Person{name:'ClintEastwood', born:1930}) CREATE (RichardH)-[:ACTED_IN {roles:['EnglishBob']}]->(Unforgiven),(ClintE)-[:ACTED_IN {roles:['BillMunny']}]->(Unforgiven),(Gene)-[:ACTED_IN {roles:['LittleBillDaggett']}]->(Unforgiven),(ClintE)-[:DIRECTED]->(Unforgiven) CREATE (JohnnyMnemonic:Movie{title:'JohnnyMnemonic', released:1995, tagline:'The hottest data on earth. In the coolest head in town'}) CREATE (Takeshi:Person{name:'TakeshiKitano', born:1947}) CREATE (Dina:Person{name:'DinaMeyer', born:1968}) CREATE (IceT:Person{name:'Ice-T', born:1958}) CREATE (RobertL:Person{name:'RobertLongo', born:1953}) CREATE (Keanu)-[:ACTED_IN {roles:['JohnnyMnemonic']}]->(JohnnyMnemonic),(Takeshi)-[:ACTED_IN {roles:['Takahashi']}]->(JohnnyMnemonic),(Dina)-[:ACTED_IN {roles:['Jane']}]->(JohnnyMnemonic),(IceT)-[:ACTED_IN {roles:['J-Bone']}]->(JohnnyMnemonic),(RobertL)-[:DIRECTED]->(JohnnyMnemonic) CREATE (CloudAtlas:Movie{title:'CloudAtlas', released:2012, tagline:'Everything is connected'}) CREATE (HalleB:Person{name:'HalleBerry', born:1966}) CREATE (JimB:Person{name:'JimBroadbent', born:1949}) CREATE (TomT:Person{name:'TomTykwer', born:1965}) CREATE (DavidMitchell:Person{name:'DavidMitchell', born:1969}) CREATE (StefanArndt:Person{name:'StefanArndt', born:1961}) CREATE (TomH)-[:ACTED_IN {roles:['Zachry', 'Dr. HenryGoose', 'IsaacSachs', 'DermotHoggins']}]->(CloudAtlas),(Hugo)-[:ACTED_IN {roles:['BillSmoke', 'HaskellMoore', 'TadeuszKesselring', 'NurseNoakes', 'BoardmanMephi', 'OldGeorgie']}]->(CloudAtlas),(HalleB)-[:ACTED_IN {roles:['LuisaRey', 'JocastaAyrs', 'Ovid', 'Meronym']}]->(CloudAtlas),(JimB)-[:ACTED_IN {roles:['VyvyanAyrs', 'CaptainMolyneux', 'TimothyCavendish']}]->(CloudAtlas),(TomT)-[:DIRECTED]->(CloudAtlas),(LillyW)-[:DIRECTED]->(CloudAtlas),(LanaW)-[:DIRECTED]->(CloudAtlas),(DavidMitchell)-[:WROTE]->(CloudAtlas),(StefanArndt)-[:PRODUCED]->(CloudAtlas) CREATE (TheDaVinciCode:Movie{title:'TheDaVinciCode', released:2006, tagline:'BreakTheCodes'}) CREATE (IanM:Person{name:'IanMcKellen', born:1939}) CREATE (AudreyT:Person{name:'AudreyTautou', born:1976}) CREATE (PaulB:Person{name:'PaulBettany', born:1971}) CREATE (RonH:Person{name:'RonHoward', born:1954}) CREATE (TomH)-[:ACTED_IN {roles:['Dr. RobertLangdon']}]->(TheDaVinciCode),(IanM)-[:ACTED_IN {roles:['SirLeightTeabing']}]->(TheDaVinciCode),(AudreyT)-[:ACTED_IN {roles:['SophieNeveu']}]->(TheDaVinciCode),(PaulB)-[:ACTED_IN {roles:['Silas']}]->(TheDaVinciCode),(RonH)-[:DIRECTED]->(TheDaVinciCode) CREATE (VforVendetta:Movie{title:'VforVendetta', released:2006, tagline:'Freedom!Forever!'}) CREATE (NatalieP:Person{name:'NataliePortman', born:1981}) CREATE (StephenR:Person{name:'StephenRea', born:1946}) CREATE (JohnH:Person{name:'JohnHurt', born:1940}) CREATE (BenM:Person{name: 'BenMiles', born:1967}) CREATE (Hugo)-[:ACTED_IN {roles:['V']}]->(VforVendetta),(NatalieP)-[:ACTED_IN {roles:['EveyHammond']}]->(VforVendetta),(StephenR)-[:ACTED_IN {roles:['EricFinch']}]->(VforVendetta),(JohnH)-[:ACTED_IN {roles:['HighChancellorAdamSutler']}]->(VforVendetta),(BenM)-[:ACTED_IN {roles:['Dascomb']}]->(VforVendetta),(JamesM)-[:DIRECTED]->(VforVendetta),(LillyW)-[:PRODUCED]->(VforVendetta),(LanaW)-[:PRODUCED]->(VforVendetta),(JoelS)-[:PRODUCED]->(VforVendetta),(LillyW)-[:WROTE]->(VforVendetta),(LanaW)-[:WROTE]->(VforVendetta) CREATE (SpeedRacer:Movie{title:'SpeedRacer', released:2008, tagline:'Speed has no limits'}) CREATE (EmileH:Person{name:'EmileHirsch', born:1985}) CREATE (JohnG:Person{name:'JohnGoodman', born:1960}) CREATE (SusanS:Person{name:'SusanSarandon', born:1946}) CREATE (MatthewF:Person{name:'MatthewFox', born:1966}) CREATE (ChristinaR:Person{name:'ChristinaRicci', born:1980}) CREATE (Rain:Person{name:'Rain', born:1982}) CREATE (EmileH)-[:ACTED_IN {roles:['SpeedRacer']}]->(SpeedRacer),(JohnG)-[:ACTED_IN {roles:['Pops']}]->(SpeedRacer),(SusanS)-[:ACTED_IN {roles:['Mom']}]->(SpeedRacer),(MatthewF)-[:ACTED_IN {roles:['RacerX']}]->(SpeedRacer),(ChristinaR)-[:ACTED_IN {roles:['Trixie']}]->(SpeedRacer),(Rain)-[:ACTED_IN {roles:['TaejoTogokahn']}]->(SpeedRacer),(BenM)-[:ACTED_IN {roles:['CassJones']}]->(SpeedRacer),(LillyW)-[:DIRECTED]->(SpeedRacer),(LanaW)-[:DIRECTED]->(SpeedRacer),(LillyW)-[:WROTE]->(SpeedRacer),(LanaW)-[:WROTE]->(SpeedRacer),(JoelS)-[:PRODUCED]->(SpeedRacer) CREATE (NinjaAssassin:Movie{title:'NinjaAssassin', released:2009, tagline:'Preparetoenter a secret world of assassins'}) CREATE (NaomieH:Person{name:'NaomieHarris'}) CREATE (Rain)-[:ACTED_IN {roles:['Raizo']}]->(NinjaAssassin),(NaomieH)-[:ACTED_IN {roles:['MikaCoretti']}]->(NinjaAssassin),(RickY)-[:ACTED_IN {roles:['Takeshi']}]->(NinjaAssassin),(BenM)-[:ACTED_IN {roles:['RyanMaslow']}]->(NinjaAssassin),(JamesM)-[:DIRECTED]->(NinjaAssassin),(LillyW)-[:PRODUCED]->(NinjaAssassin),(LanaW)-[:PRODUCED]->(NinjaAssassin),(JoelS)-[:PRODUCED]->(NinjaAssassin) CREATE (TheGreenMile:Movie{title:'TheGreenMile', released:1999, tagline:"Walk a mile you'll never forget."}) CREATE (MichaelD:Person{name:'MichaelClarkeDuncan', born:1957}) CREATE (DavidM:Person{name:'DavidMorse', born:1953}) CREATE (SamR:Person{name:'SamRockwell', born:1968}) CREATE (GaryS:Person{name:'GarySinise', born:1955}) CREATE (PatriciaC:Person{name:'PatriciaClarkson', born:1959}) CREATE (FrankD:Person{name:'FrankDarabont', born:1959}) CREATE (TomH)-[:ACTED_IN {roles:['PaulEdgecomb']}]->(TheGreenMile),(MichaelD)-[:ACTED_IN {roles:['JohnCoffey']}]->(TheGreenMile),(DavidM)-[:ACTED_IN {roles:['Brutus"Brutal"Howell']}]->(TheGreenMile),(BonnieH)-[:ACTED_IN {roles:['JanEdgecomb']}]->(TheGreenMile),(JamesC)-[:ACTED_IN {roles:['WardenHalMoores']}]->(TheGreenMile),(SamR)-[:ACTED_IN {roles:['"Wild Bill"Wharton']}]->(TheGreenMile),(GaryS)-[:ACTED_IN {roles:['BurtHammersmith']}]->(TheGreenMile),(PatriciaC)-[:ACTED_IN {roles:['MelindaMoores']}]->(TheGreenMile),(FrankD)-[:DIRECTED]->(TheGreenMile) CREATE (FrostNixon:Movie{title:'Frost/Nixon', released:2008, tagline:'400 million people were waiting for the truth.'}) CREATE (FrankL:Person{name:'FrankLangella', born:1938}) CREATE (MichaelS:Person{name:'MichaelSheen', born:1969}) CREATE (OliverP:Person{name:'OliverPlatt', born:1960}) CREATE (FrankL)-[:ACTED_IN {roles:['RichardNixon']}]->(FrostNixon),(MichaelS)-[:ACTED_IN {roles:['DavidFrost']}]->(FrostNixon),(KevinB)-[:ACTED_IN {roles:['JackBrennan']}]->(FrostNixon),(OliverP)-[:ACTED_IN {roles:['BobZelnick']}]->(FrostNixon),(SamR)-[:ACTED_IN {roles:['JamesReston,Jr.']}]->(FrostNixon),(RonH)-[:DIRECTED]->(FrostNixon) CREATE (Hoffa:Movie{title:'Hoffa', released:1992, tagline:"He didn't want law. He wanted justice."}) CREATE (DannyD:Person{name:'DannyDeVito', born:1944}) CREATE (JohnR:Person{name:'JohnC. Reilly', born:1965}) CREATE (JackN)-[:ACTED_IN {roles:['Hoffa']}]->(Hoffa),(DannyD)-[:ACTED_IN {roles:['Robert"Bobby"Ciaro']}]->(Hoffa),(JTW)-[:ACTED_IN {roles:['FrankFitzsimmons']}]->(Hoffa),(JohnR)-[:ACTED_IN {roles:['Peter"Pete"Connelly']}]->(Hoffa),(DannyD)-[:DIRECTED]->(Hoffa) CREATE (Apollo13:Movie{title:'Apollo13', released:1995, tagline:'Houston, we have a problem.'}) CREATE (EdH:Person{name:'EdHarris', born:1950}) CREATE (BillPax:Person{name:'BillPaxton', born:1955}) CREATE (TomH)-[:ACTED_IN {roles:['JimLovell']}]->(Apollo13),(KevinB)-[:ACTED_IN {roles:['JackSwigert']}]->(Apollo13),(EdH)-[:ACTED_IN {roles:['GeneKranz']}]->(Apollo13),(BillPax)-[:ACTED_IN {roles:['FredHaise']}]->(Apollo13),(GaryS)-[:ACTED_IN {roles:['KenMattingly']}]->(Apollo13),(RonH)-[:DIRECTED]->(Apollo13) CREATE (Twister:Movie{title:'Twister', released:1996, tagline:"Don't Breathe. Don't Look Back."}) CREATE (PhilipH:Person{name:'PhilipSeymourHoffman', born:1967}) CREATE (JanB:Person{name:'Jan de Bont', born:1943}) CREATE (BillPax)-[:ACTED_IN {roles:['BillHarding']}]->(Twister),(HelenH)-[:ACTED_IN {roles:['Dr. JoHarding']}]->(Twister),(ZachG)-[:ACTED_IN {roles:['Eddie']}]->(Twister),(PhilipH)-[:ACTED_IN {roles:['Dustin"Dusty"Davis']}]->(Twister),(JanB)-[:DIRECTED]->(Twister) CREATE (CastAway:Movie{title:'CastAway', released:2000, tagline:'At the edge of the world, his journey begins.'}) CREATE (RobertZ:Person{name:'RobertZemeckis', born:1951}) CREATE (TomH)-[:ACTED_IN {roles:['ChuckNoland']}]->(CastAway),(HelenH)-[:ACTED_IN {roles:['KellyFrears']}]->(CastAway),(RobertZ)-[:DIRECTED]->(CastAway) CREATE (OneFlewOvertheCuckoosNest:Movie{title:"One Flew Over the Cuckoo's Nest", released:1975, tagline:"If he's crazy, what does that make you?"}) CREATE (MilosF:Person{name:'MilosForman', born:1932}) CREATE (JackN)-[:ACTED_IN {roles:['RandleMcMurphy']}]->(OneFlewOvertheCuckoosNest),(DannyD)-[:ACTED_IN {roles:['Martini']}]->(OneFlewOvertheCuckoosNest),(MilosF)-[:DIRECTED]->(OneFlewOvertheCuckoosNest) CREATE (SomethingsGottaGive:Movie{title:"Something's Gotta Give", released:2003}) CREATE (DianeK:Person{name:'DianeKeaton', born:1946}) CREATE (NancyM:Person{name:'NancyMeyers', born:1949}) CREATE (JackN)-[:ACTED_IN {roles:['HarrySanborn']}]->(SomethingsGottaGive),(DianeK)-[:ACTED_IN {roles:['EricaBarry']}]->(SomethingsGottaGive),(Keanu)-[:ACTED_IN {roles:['JulianMercer']}]->(SomethingsGottaGive),(NancyM)-[:DIRECTED]->(SomethingsGottaGive),(NancyM)-[:PRODUCED]->(SomethingsGottaGive),(NancyM)-[:WROTE]->(SomethingsGottaGive) CREATE (BicentennialMan:Movie{title:'BicentennialMan', released:1999, tagline:"One robot's 200 year journey to become an ordinary man."}) CREATE (ChrisC:Person{name:'ChrisColumbus', born:1958}) CREATE (Robin)-[:ACTED_IN {roles:['AndrewMarin']}]->(BicentennialMan),(OliverP)-[:ACTED_IN {roles:['RupertBurns']}]->(BicentennialMan),(ChrisC)-[:DIRECTED]->(BicentennialMan) CREATE (CharlieWilsonsWar:Movie{title:"Charlie Wilson's War", released:2007, tagline:"A stiff drink. A little mascara. A lot of nerve. Who said they couldn't bring down the Soviet empire."}) CREATE (JuliaR:Person{name:'JuliaRoberts', born:1967}) CREATE (TomH)-[:ACTED_IN {roles:['Rep. CharlieWilson']}]->(CharlieWilsonsWar),(JuliaR)-[:ACTED_IN {roles:['JoanneHerring']}]->(CharlieWilsonsWar),(PhilipH)-[:ACTED_IN {roles:['GustAvrakotos']}]->(CharlieWilsonsWar),(MikeN)-[:DIRECTED]->(CharlieWilsonsWar) CREATE (ThePolarExpress:Movie{title:'ThePolarExpress', released:2004, tagline:'ThisHolidaySeason...Believe'}) CREATE (TomH)-[:ACTED_IN {roles:['HeroBoy', 'Father', 'Conductor', 'Hobo', 'Scrooge', 'SantaClaus']}]->(ThePolarExpress),(RobertZ)-[:DIRECTED]->(ThePolarExpress) CREATE (ALeagueofTheirOwn:Movie{title:'ALeague of TheirOwn', released:1992, tagline:'Once in a lifetime you get a chance todo something different.'}) CREATE (Madonna:Person{name:'Madonna', born:1954}) CREATE (GeenaD:Person{name:'GeenaDavis', born:1956}) CREATE (LoriP:Person{name:'LoriPetty', born:1963}) CREATE (PennyM:Person{name:'PennyMarshall', born:1943}) CREATE (TomH)-[:ACTED_IN {roles:['JimmyDugan']}]->(ALeagueofTheirOwn),(GeenaD)-[:ACTED_IN {roles:['DottieHinson']}]->(ALeagueofTheirOwn),(LoriP)-[:ACTED_IN {roles:['KitKeller']}]->(ALeagueofTheirOwn),(RosieO)-[:ACTED_IN {roles:['DorisMurphy']}]->(ALeagueofTheirOwn),(Madonna)-[:ACTED_IN {roles:['"All the Way"MaeMordabito']}]->(ALeagueofTheirOwn),(BillPax)-[:ACTED_IN {roles:['BobHinson']}]->(ALeagueofTheirOwn),(PennyM)-[:DIRECTED]->(ALeagueofTheirOwn) CREATE (PaulBlythe:Person{name:'PaulBlythe'}) CREATE (AngelaScope:Person{name:'AngelaScope'}) CREATE (JessicaThompson:Person{name:'JessicaThompson'}) CREATE (JamesThompson:Person{name:'JamesThompson'}) CREATE (JamesThompson)-[:FOLLOWS]->(JessicaThompson),(AngelaScope)-[:FOLLOWS]->(JessicaThompson),(PaulBlythe)-[:FOLLOWS]->(AngelaScope) CREATE (JessicaThompson)-[:REVIEWED {summary:'An amazing journey', rating:95}]->(CloudAtlas),(JessicaThompson)-[:REVIEWED {summary:'Silly, but fun', rating:65}]->(TheReplacements),(JamesThompson)-[:REVIEWED {summary:'The coolest football movie ever', rating:100}]->(TheReplacements),(AngelaScope)-[:REVIEWED {summary:'Pretty funny at times', rating:62}]->(TheReplacements),(JessicaThompson)-[:REVIEWED {summary:'Dark, but compelling', rating:85}]->(Unforgiven),(JessicaThompson)-[:REVIEWED {summary:"Slapstick redeemed only by the Robin Williams and Gene Hackman's stellar performances", rating:45}]->(TheBirdcage),(JessicaThompson)-[:REVIEWED {summary:'A solid romp', rating:68}]->(TheDaVinciCode),(JamesThompson)-[:REVIEWED {summary:'Fun, but a little far fetched', rating:65}]->(TheDaVinciCode),(JessicaThompson)-[:REVIEWED {summary:'You had me at Jerry', rating:92}]->(JerryMaguire) WITH TomH as a MATCH (a)-[:ACTED_IN]->(m)<-[:DIRECTED]-(d)RETURN a,m,d LIMIT10;节点检索match
参考文档:https://neo4j.com/docs/cypher-manual/current/clauses/match/
// 返回所有节点信息 match (n)return n;// 返回节点数量 match (n)returncount(n);// 返回有WROTE 关系的 两条记录MATCH p=()-[r:WROTE]->() RETURN p LIMIT 2条件过滤where
//语法 MATCH 待筛选出来的信息 WHERE 需要的结果条件 return 实例名 //示例数据 match (n) where n.name CONTAINS 'linrc'return n 删除节点以及关系DELETE
//语法 match 待删除的东西 DELETE 关系、节点、或者节点关系对变量 // 示例数据//创建节点 create (person1:Person{name:'linrc3'})-[:TestColleagues{company:'测试电信总公司'}]->(person2:Person{name:'linrc4'})// 检索 含有 TestColleagues关系 且 节点名字有 linrc字符 的 match (n)-[:TestColleagues]-(m) where n.name contains 'linrc' or m.name contains 'linrc'returnDISTINCT n,m match (n) where n.name contains 'linrc3' or n.name contains 'linrc4'return n // 报错 == 删除节点前必须删除关系 == 否则执行失败 match (n) where n.name contains 'linrc3' or n.name contains 'linrc4' delete n // 正常:仅删除关系,但节点还在 match ()-[t:TestColleagues]-() delete t // 正常:节点、关系都删除 match p=()-[t:TestColleagues]-() delete p 删除节点或者关系的成员属性REMOVE
//语法 match 待删除属性的节点或者关系 REMOVE 需要删除的节点属性或者关系属性 //示例数据 create (person1:Person{name:'linrc5',year:18})-[:TestTestColleagues{company:'测试测试电信总公司',remark:'备注'}]->(person2:Person{name:'linrc6',year:19}); match (n) where n.name contains 'linrc5' or n.name contains 'linrc6'return n; match (n)-[:TestTestColleagues]-(m) where n.name contains 'linrc' or m.name contains 'linrc'returnDISTINCT n,m // 删除 节点 的year属性 以及 关系的remark属性 match (n)-[t:TestTestColleagues]-(m) where n.name contains 'linrc' or m.name contains 'linrc' remove n.year,t.remark 设置节点或者关系的属性SET
// 语法 match 待修改属性的节点或者关系 where 上面匹配的节点或者关系进一步过滤 set 需要修改的节点属性或者关系属性 //示例数据 create (person1:Person{name:'linrc7',year:18})-[:TestTestColleagues{company:'测试测试电信总公司',remark:'备注'}]->(person2:Person{name:'linrc8',year:19}); match (n) where n.name in ['linrc7','linrc8']return n; match (n) where n.name in ['linrc7'] set n.year =21return n 排序order by
//语法 match 待修改属性的节点或者关系 where 上面匹配的节点或者关系进一步过滤 return 输出的结果 order by 针对输出的结果进行排序 // 示例 create (person1:Person{name:'linrc9',year:18})-[:TestTestColleagues{company:'测试测试电信总公司',remark:'备注'}]->(person2:Person{name: 'linrc10',year:19}); match (n) where n.name contains 'linrc' and n.year is not nullreturn n order by n.year desc 分页limit
//语法 match 待修改属性的节点或者关系 where 上面匹配的节点或者关系进一步过滤 return 输出的结果 order by 针对输出的结果进行排序 skip 开始从第N个元素输出(0表第一个元素) limit 此次结果输出最多记录数 // 示例 create (person1:Person{name:'linrc9',year:18})-[:TestTestColleagues{company:'测试测试电信总公司',remark:'备注'}]->(person2:Person{name: 'linrc10',year:19}); match (n) where n.name contains 'linrc' and n.year is not nullreturn n order by n.year desc skip 0 limit 1常用内置函数
文档:https://neo4j.com/docs/cypher-manual/current/functions/
//聚合函数 count:总数 max:最大值 min:最小值 sum:求和 avg:平均值 collect:抽取某个节点或者关系属性形成一个 容器列表 //字符串函数 UPPER:所有字母大写 LOWER:所有字母小写 SUBSTRING:抽取字符串中的某位置的子字符串 REPLACE:字符串中某些字符替换成为其他字符 trim:去除字符串中首尾的空格 btrim:去除字符串中首尾的指定字符 ltrim:去除字符串中首部的指定字符 rtrim:去除字符串中尾部的指定字符 left:抽取字符串从左侧开始数的N个字符 right:抽取字符串从右侧开始数的N个字符 reverse:字符串翻转 split:基于某个字符串进行字符切割 //关系函数 id:节点或者关系的ID type:这段关系是什么(类名) startnode:关系的开始节点 endnode:关系的结束节点 //容器列表函数