Cypher的使用

:play cypher 可以打开Cypher的帮助文档

Cypher

Neo4j’s graph query language

Neo4j’s Cypher language is purpose built for working with graph data.

  • uses patterns to describe graph data 使用模式描述图形数据
  • familiar SQL-like clauses 熟悉的类似SQL的查询语句
  • declarative, describing what to find, not how to find it 说明性的,描述要查找的内容,而不是如何查找

CREATE

Create a node
Let’s use Cypher to generate a small social graph.

CREATE (ee:Person { name: "Emil", from: "Sweden", klout: 99 })
  • CREATE clause to create data CREATE语句去添加数据
  • () parenthesis to indicate a node 括号表示一个节点
  • ee:Person a variable ‘ee’ and label ‘Person’ for the new node
  • brackets to add properties to the node 大括号里面的内容为节点添加属性

MATCH

Finding nodes

Now find the node representing Emil:

MATCH (ee:Person) WHERE ee.name = "Emil" RETURN ee;
  • MATCH clause to specify a pattern of nodes and relationships
  • (ee:Person) a single node pattern with label ‘Person’ which will assign matches to the variable ‘ee’
  • WHERE clause to constrain the results
  • ee.name = "Emil" compares name property to the value “Emil”
  • RETURN clause used to request particular results

CREATE more

Nodes and relationships

CREATE clauses can create many nodes and relationships at once.

MATCH (ee:Person) WHERE ee.name = "Emil"
CREATE (js:Person { name: "Johan", from: "Sweden", learn: "surfing" }),
(ir:Person { name: "Ian", from: "England", title: "author" }),
(rvb:Person { name: "Rik", from: "Belgium", pet: "Orval" }),
(ally:Person { name: "Allison", from: "California", hobby: "surfing" }),
(ee)-[:KNOWS {since: 2001}]->(js),(ee)-[:KNOWS {rating: 5}]->(ir),
(js)-[:KNOWS]->(ir),(js)-[:KNOWS]->(rvb),
(ir)-[:KNOWS]->(js),(ir)-[:KNOWS]->(ally),
(rvb)-[:KNOWS]->(ally)

Pattern matching

Describe what to find in the graph

For instance, a pattern can be used to find Emil’s friends:

MATCH (ee:Person)-[:KNOWS]-(friends)
WHERE ee.name = "Emil" RETURN ee, friends
  • MATCH clause to describe the pattern from known Nodes to found Nodes
  • (ee) starts the pattern with a Person (qualified by WHERE)
  • -[:KNOWS]- matches “KNOWS” relationships (in either direction)
  • (friends) will be bound to Emil’s friends

Recommend

Using patterns

Pattern matching can be used to make recommendations. Johan is learning to surf, so he may want to find a new friend who already does:

MATCH (js:Person)-[:KNOWS]-()-[:KNOWS]-(surfer)
WHERE js.name = "Johan" AND surfer.hobby = "surfing"
RETURN DISTINCT surfer
  • () empty parenthesis to ignore these nodes
  • DISTINCT because more than one path will match the pattern
  • surfer will contain Allison, a friend of a friend who surfs

Analyze

Using the visual query plan

Understand how your query works by prepending EXPLAIN or PROFILE:

PROFILE MATCH (js:Person)-[:KNOWS]-()-[:KNOWS]-(surfer)
WHERE js.name = "Johan" AND surfer.hobby = "surfing"
RETURN DISTINCT surfer

Live Cypher warnings

Identify query problems in real time

As you type, the query editor notifies you about deprecated features and potentially expensive queries.
在这里插入图片描述

Next steps

Start your application using Cypher to create and query graph data. Use the REST API to monitor the database. In special cases, consider a plugin.

Keep getting started

Intro - a guided tour
Concepts - GraphDB 101
The Movie Graph - create the movie graph
Northwind Graph - from RDBMS to graph

Reference

Full Northwind import example
Cypher Refcard
The Cypher chapter of the Neo4j Developer Manual

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值