GraphGists

Neo4j 简介

让我们创建第一个基本图!我们将创建一个类似于你在视频中看到的内容(或类似于你在此图像中看到的内容)。

3JiWZht

当然,如果你不是巴塞罗那球迷,你可以更改为任何你想要的内容。

CREATE  (m:Player{name:"Lionel Messi"}),	   	    (b:Team{name:"Barcelona"})
WITH m, b
CREATE (m)-[p:PlaysFor]->(b)
SET p.since=2001
//SET p.since=date("2001-02-01") // For some reason the GraphGists don't support dates
RETURN m, p, b

朋友网络

但是之前的是一个相当简单的例子,我们创建一个朋友网络看看还能做些什么…​

// Create nodes
CREATE (rob:Person{name:'Roberto'}), (isidro:Person{name:'Isidro'}),
      (tony:Person{name:'Antonio'}), (nora:Person{name:'Nora'}),
      (lily:Person{name:'Lilian'}), (freddy:Person{name:'Alfredo'}),
      (lucas:Person{name:'Lucas'}), (mau:Person{name:'Mauricio'}),
      (alb:Person{name:'Albina'}), (reg:Person{name:'Regina'}),
      (j:Person{name:'Joaquín'}), (julian:Person{name:'Julián'})
// Create relationships
CREATE
  (rob)-[:FriendsWith]->(isidro), (rob)-[:FriendsWith]->(tony), (rob)-[:FriendsWith]->(reg),
  (rob)-[:FriendsWith]->(mau), (rob)-[:FriendsWith]->(julian),
  (tony)-[:FriendsWith]->(reg), (tony)-[:FriendsWith]->(j),
  (alb)-[:FriendsWith]->(reg), (lily)-[:FriendsWith]->(isidro), (lily)-[:FriendsWith]->(j),
  (mau)-[:FriendsWith]->(lucas), (lucas)-[:FriendsWith]->(nora), (freddy)-[:FriendsWith]->(nora);

// Query the relationships
MATCH friendships=()-[:FriendsWith]-()
RETURN friendships

🤔

让我们找到谁是Lucas的朋友(注意我们不关心关系的方向,我们没有在关系中使用箭头

MATCH friends=(a:Person{name:'Lucas'})-[:FriendsWith]-(friend)
RETURN friends

Lucas的朋友的朋友的朋友呢?想象一下在 SQL 中做类似的事情…​

MATCH friends=(a:Person{name:'Lucas'})-[:FriendsWith*3]-(friend)
RETURN friend.name

或者更高级的东西,JoaquínLucas之间的最短路径是哪一条

MATCH (lucas:Person{name:'Lucas'}), (joaquin:Person{name:'Joaquín'}),
  p = shortestPath((lucas)-[*]-(joaquin))
RETURN p

现在轮到你了,尽情使用上面的图或创建你自己的图

由 Antonio Feregrino 创建 - Twitter | YouTube | LinkedIn