示例来源: Neo4j in Action.

0 准备数据

0.1 node

(user1 { name: 'John Johnson', type: 'User', email: 'jsmith@example.org', age: 35})

with Label:

(user1:Users { name: 'John Johnson', type: 'User', email: 'jsmith@example.org', age: 35})

0.2 relationship

(user1) -[:IS_FRIEND_OF]-> (user2)

with property:

(user1) -[:HAS_SEEN {stars: 5}]-> (movie1)

0.3 sample

create (user1:Users { name: 'John Johnson', type: 'User', email: 'jsmith@example.org', age: 35}),
(user2:Users { name: 'Kate Smith', type: 'User', email: 'ksmith@example.org', age: 35}),
(user3:Users { name: 'Jack Jeffries', type: 'User', email: 'jjeffries@example.org', age: 34}),
(movie1:Movies { name: 'Fargo', type: 'Movie'}),
(movie2:Movies { name: 'Alien', type: 'Movie'}),
(movie3:Movies { name: 'Heat', type: 'Movie'}),
(user1) -[:IS_FRIEND_OF]-> (user2),
(user1) -[:IS_FRIEND_OF]-> (user3),
(user1) -[:HAS_SEEN {stars: 5}]-> (movie1),
(user2) -[:HAS_SEEN {stars: 3}]-> (movie3),
(user3) -[:HAS_SEEN {stars: 4}]-> (movie1),
(user3) -[:HAS_SEEN {stars: 5}]-> (movie2)

1 模式匹配

1.1 使用node和relationship标识符

start user=node(1)
match (user)-[r:HAS_SEEN]->(movie)
return movie; 

//匿名relationship
start user=node(1)
match (user)-[:HAS_SEEN]->(movie)
return movie;

//匿名node
start user=node(1)
match (user)-[r:HAS_SEEN]->()
return r;

1.2 复杂的模式匹配

// 3 nodes, 2 relationships
start john=node(1)
match john-[:IS_FRIEND_OF]->()-[:HAS_SEEN]->(movie)
return movie;

//同一查询中多个模式
start john=node(1)
match
    john-[:IS_FRIEND_OF]->()-[:HAS_SEEN]->(movie),
    john-[r:HAS_SEEN]->(movie)
return movie;

// where条件过滤
start john=node:users(name = "John Johnson")
match john-[:IS_FRIEND_OF]->(user)-[:HAS_SEEN]->(movie)
where NOT john-[:HAS_SEEN]->(movie)
return movie.name;

2 定位起始node

2.1 Id

start john=node(1)
return john;

2.2 Ids

start user=node(1, 3)
match user-[:HAS_SEEN]->movie
return distinct movie;

2.3 index

// index `users`
start john=node:users(name = "John Johnson")
return john;

// native Lucene query
start john=node:users("name:John Johnson")
return john;

// a complex Lucene query
start john=node:users("name:John* AND yearOfBirth<1980")
return john;

2.4 schema-based index

基于Label的索引只能用于查找整个属性值。

match (john:USER)
where john.name='John Johnson'
return john;

2.5 多个起始node

start john=node:users("name:John Johnson"),
    jack=node:users("name:Jack Jeffries")
match john-[:HAS_SEEN]->movie, jack-[:HAS_SEEN]->movie
return movie;

3 过滤数据

// node和relationship的属性值过滤
start john=node:users("name:John Johnson")
match john-[:IS_FRIEND_OF]-(friend)
where friend.yearOfBirth > 1980
return friend;

//使用正则表达式
start john=node:users("name:John Johnson")
match john-[:IS_FRIEND_OF]-(friend)
where friend.email =~ /.*@gmail.com/
return friend;

//使用Cypher内建函数
start john=node:users("name:John Johnson")
match john-[IS_FRIEND_OF]-friend
where has(friend.twitter)
return friend

4 获取结果

4.1 返回property

//node属性
start john= node:users(name = "John Johnson")
match john-[:IS_FRIEND_OF]->(user)-[:HAS_SEEN]->(movie)
where not john-[:HAS_SEEN]->(movie)
return movie.name;

//relationship属性
start john=node:users("name:John Johnson")
match john-[r:HAS_SEEN]-(movie)
return r.stars

4.2 返回relathinship

start john=node:users("name:John Johnson")
match john-[r:HAS_SEEN]-(movie)
return r;

4.3 返回path

// path: `recPath`
start john=node:users(name = "John Johnson")
match recPath = john-[:IS_FRIEND_OF]->(user)-[:HAS_SEEN]->(movie)
where not john-[r:HAS_SEEN]->(movie)
return movie.name, recPath;

4.4 结果分页

start john=node:users("name:John Johnson")
match john-[:HAS_SEEN]->(movie)
return movie
order by movie.name // order
skip 20             // skip 2 pages
limit 10            // 10 in a page

下面涉及更新操作(5-7)。

5 创建新实体

//创建node
create newuser
{
    name: 'Grace Spencer',
    yearOfBirth: 1982,
    email: 'grace@mycompany.com'
}
return newuser;

//创建relationship
start john = node:users(name = "John Johnson"), grace = node(10)
create john-[r:IS_FRIEND_OF]->grace
return r;

// 同时创建node和relationship
start john = node:users(name = "John Johnson")
create john -[r:IS_FRIEND_OF]->
    (grace {
    name: 'Grace Spencer',
    yearOfBirth: 1982, email: 'grace@mycompany.com'
    })
return r, grace;

//使用`unique`仅创建模式中不存在的实体
start john = node:users(name = "John Johnson")
create unique john -[r:IS_FRIEND_OF]->
    (grace {
        name: 'Grace Spencer',
        yearOfBirth: 1982,
        email: 'grace@mycompany.com'
    })
return r, grace;

6 删除数据

//删除node,仅在node没有relationship时
start grace = node(10)
delete grace

//删除node和relationship
start grace = node(10)
match grace-[r]-()
delete grace, r

7 更新node和relationship属性

//更新node属性
start john=node:users(name = "John Johnson")
set john.yearOfBirth = 1973;

//更新多个node的属性
start user=node(1,2)
set user.group = 'ADMINISTRATOR'

//删除node属性,Neo4j不允许null属性值
    start n=node(1)
delete n.group;

下面是Cypher的高级用法(8-11)。

8 聚合

//按`user`聚合,即结果中非所有非聚合字段
start user=node(*)
match user-[:IS_FRIEND_OF]-()
return user, count(*)
order by count(*) desc;

其他数值聚合函数:SUM, AVG, MAX, MIN

start john=node:users(name = "John Johnson")
match john-[:IS_FRIEND_OF]-(friend)
where HAS(friend.yearOfBirth)
return avg(2014-friend.yearOfBirth);

9 函数

9.1 实体内部属性

ID(node), TYPE(relationship)

//relationship的类型
start n=node:users(name='John Johnson)
match n-[rel]-()
return TYPE(rel), count(*);

9.2 集合函数

HAS(graphEntity.propertyName)
NODES(path):将path转换为node集合
ALL(x in collection where predicate(x))
NONE(x in collection where predicate(x))
ANY(x in collection where predicate(x))
SINGLE(x in collection where predicate(x))

start john=node:users(name = "John Johnson"),
    kate= node:users(name = "Kate Smith"),
match p=john-[:IS_FRIEND_OF*1..3]-(kate)
where ALL(
    user in NODES(p)
    where HAS(user.facebookId)
    )
return p;

10 用WITH子句管道化

// simulate SQL `HAVING` clause
start n=node(1)
match n-[rel]-()
with TYPE(rel) as type, count(*) as count
where count > 1
return type, count;

11 Cypher兼容性

//指定Neo4j的版本
CYPHER 1.8 start n=node(1)
match n-[rel]-()
with TYPE(rel) as type, count(*) as count
where count > 1
return type, count;

Neo4j Cypher运行示例的更多相关文章

  1. Neo4j Cypher语法(三)

    目录 5 函数 5.1 谓词函数 5.2 标量函数 5.3 聚合函数 5.4 列表函数 5.5 数学函数 5.6 字符串函数 5.7 Udf与用户自定义函数 6 模式 6.1 索引 6.2 限制 7 ...

  2. neo4j安装与示例

    Neo4j有两种访问模式:服务器模式和嵌入模式参考,下面主要讲windows下这两种模式的配置与访问示例 1 Windows下Neo4j服务器模式安装与示例 安装: 1.下载Neo4j,我下载的版本是 ...

  3. Servlet与Tomcat运行示例

    Servlet与Tomcat运行示例 本文将写一个servlet,然后将其部署到Tomcat的全过程.本文参考<深入拆解Tomcat_Jetty>内容. 一.基于web.xml开发步骤 下 ...

  4. Windows上配置Mask R-CNN及运行示例demo.ipynb

    最近做项目需要用到Mask R-CNN,于是花了几天时间配置.简单跑通代码,踩了很多坑,写下来分享给大家. 首先贴上官方Mask R-CNN的Github地址:https://github.com/m ...

  5. Kurento安装与入门02——运行示例前的准备

    官方一共提供了13个示例,这些示例运行的方式大同小异,一般会提供JAVA.Browser JavaScript.Node.js三种版本,这里仅演示java版本的示例.这些示例要求系统内已经正确安装了K ...

  6. Neo4j Cypher语法(二)

    目录 4 子句 4.1 CREATE 4.2 MATCH 4.3 Match 4.4 Create match return连用来返回一个关系基础 4.5 Optional_match 4.6 Wit ...

  7. Neo4j Cypher查询语言详解

    Cypher介绍 "Cypher"是一个描述性的图形查询语言,允许不必编写图形结构的遍历代码对图形存储有表现力和效率的查询.Cypher还在继续发展和成熟,这也就意味着有可能会出现 ...

  8. Neo4j Cypher语法(一)

    目录 Cypher手册详解 1 背景 2 唯一性 3 语法 3.1 命名规则 3.2 表达式 3.3 变量与保留关键字 3.4 参数 3.5 操作符 3.6 模式 3.7 列表 Cypher手册详解 ...

  9. Hadoop版Helloworld之wordcount运行示例

    1.编写一个统计单词数量的java程序,并命名为wordcount.java,代码如下: import java.io.IOException; import java.util.StringToke ...

随机推荐

  1. XAF How to: 实现一个WCF Application Server 并配置它的客户端应用

    本主题描述了如何实现一个 WCF 中间层应用程序服务器及如何配置 XAF客户端连接到此服务器. 注意 本主题演示可以由解决方案向导自动生成的代码.执行操作时,如果你想要在现有的 XAF 解决方案中实现 ...

  2. UART总线(异步)

    UART用一条传输线将数据一位位地顺序传送,以字符为传输单位通信中两个字符间的时间间隔多少是不固定的, 然而在同一个字符中的两个相邻位间的时间间隔是固定的 数据传送速率用波特率来表示, 指单位时间内载 ...

  3. Codeforces Round #263 (Div. 1)

    B 树形dp 组合的思想. Z队长的思路. dp[i][1]表示以i为跟结点的子树向上贡献1个的方案,dp[i][0]表示以i为跟结点的子树向上贡献0个的方案. 如果当前为叶子节点,dp[i][0] ...

  4. JavaScipt 源码解析 Sizzle选择器

    jQuery的定位就是一个DOM的操作库,那么可想而知选择器是一个至关重要的模块.Sizzle,作为一个独立全新的选择器引擎,出现在jQuery 1.3版本之后,并被John Resig作为一个开源的 ...

  5. 并发编程 06—— CompletionService :Executor 和 BlockingQueue

    Java并发编程实践 目录 并发编程 01—— ThreadLocal 并发编程 02—— ConcurrentHashMap 并发编程 03—— 阻塞队列和生产者-消费者模式 并发编程 04—— 闭 ...

  6. HashSet其实就那么一回事儿之源码浅析

    上篇文章<HashMap其实就那么一回事儿之源码浅析>介绍了hashMap,  本次将带大家看看HashSet, HashSet其实就是基于HashMap实现, 因此,熟悉了HashMap ...

  7. Props属性

    大多数组件在创建时就可以使用各种参数来进行定制.用于定制的这些参数就称为props(属性). import React, { Component } from 'react'; import { Ap ...

  8. jersey处理支付宝异步回调通知的问题:java.lang.IllegalArgumentException: Error parsing media type 'application/x-www-form-urlencoded; text/html; charset=UTF-8'

    tcpflow以流为单位分析请求内容,非常适合服务器端接口类服务查问题 这次遇到的问题跟支付宝支付后的回调post结果有关 淘宝的代码例子: public void doPost(HttpServle ...

  9. Foundation框架—集合

    Foundation框架—集合 一.NSArray和NSMutableArray (一)NSArray不可变数组 (1)NSArray的基本介绍 NSArray是OC中使用的数组,是面向对象的,以面向 ...

  10. iOS开发UI篇—程序启动原理和UIApplication

    iOS开发UI篇—程序启动原理和UIApplication   一.UIApplication 1.简单介绍 (1)UIApplication对象是应用程序的象征,一个UIApplication对象就 ...