neo4j 查询cypher话语

钟柱 2017-01-22

neo4j 查询cypher语句
最近使用nosql数据库neo4j,针对其综合查询学习使用了cypher语句。

分享总结:

1.根据索引为nePort,针对id模糊查询全部节点。根据id排序,limit只去前10条记录
start n = node:nePort('id:*') 
match (x)-[:NE_PORT]->(n)
return n,x
order by n.id limit 10


2.这个查询相对于1查询,中多了,match匹配节点间关系,先根据索引查询获取,起点n,在匹配关系,【:关系名称】,a->b 表示从a到b.
该查询,是获取存在一个节点关系志向n,且关系为NE_PORT,这个起点x
start n = node:nePort('id:*') 
match (x)-[:NE_PORT]->n
return n.type,n.id,n.speedId,n.code,n.name,n.optical,n.used,n.serial,x.id
order by n.id skip 0 limit 10;

3模糊查询节点id属性中包含11的,的节点。
START ne=node:ne('id=*11*')  return ne

4.匹配下级所有[:VC*]
START ne=node(4106557) MACTH ne-[:NE_PORT]->port-[:VC*]-vc where vc.vtag=0 RETURN vc; 

5.只匹配到,2层关系为VC的节点。
START ne=node(4106557) MATCH ne-[:NE_PORT]->port-[:VC*1..2]->vc WITH vc WHERE vc.vtag? = 0 RETURN vc;

6.条件,模糊查询。查询 label 含中山的网元 
start n=node(3) match n-[:NE]-ne where ne.label =~ '.*中山.*' return ne.id, ne.label, ne.name;


查询语法(Cyphe Query Language)
neo4j自己基于图论的搜索算法,实现了一套查询语言解析,提供了一些常见的聚合函数(max,sum,min,count等)。

Ø  START:在图中的开始点,通过元素的ID或所以查找获得。

Ø  MATCH:图形的匹配模式,束缚于开始点。

Ø  WHERE:过滤条件。

Ø  RETURN:返回所需要的。

语法例子:



Join查询:
start n=(1) match (n)-[:BLOCKS]->(x) return x

Where条件:
start n=(2, 1) where (n.age < 30 and n.name = "Tobias") or not(n.name = "Tobias") return n

聚合函数:
start n=(2,3,4) return avg(n.property)

Order:
start n=(1,2,3) return n order by n.name DESC

分页:
start n=(1,2,3,4,5) return n order by n.name skip 1 limit 2
START n=node(*) return n skip 1 limit 2
统计:
START n=node(*) RETURN count(*);
START n=node(*) RETURN "Hello Graph with "+count(*)+" Nodes!" as welcome;

start n = node:nePort('id:*') return n skip " + pageNo + " limit " + pageSize;

如下是在JAVA中使用参数的几个例子:
节点ID参数
1
2
3
Map<String, Object> params = new HashMap<String, Object>();
params.put( "id", 0 );
ExecutionResult result = 
engine.execute("start n=node({id}) return n.name", params );

节点对象参数
1
2
3
Map<String, Object> params = new HashMap<String, Object>();
params.put( "node", andreasNode );
ExecutionResult result = 
engine.execute("start n=node({node}) return n.name", params );

多节点ID参数
1
2
3
Map<String, Object> params = new HashMap<String, Object>();
params.put( "id", Arrays.asList( 0, 1, 2 ) );
ExecutionResult result = 
engine.execute("start n=node({id}) return n.name", params );
字符串参数
1
2
3
4
Map<String, Object> params = new HashMap<String, Object>();
params.put( "name", "Johan" );
ExecutionResult result =
engine.execute("start n=node(0,1,2) where n.name = {name} return n", params );
索引键值参数
1
2
3
4
5
Map<String, Object> params = new HashMap<String, Object>();
params.put( "key", "name" );
params.put( "value", "Michaela" );
ExecutionResult result =
engine.execute("start n=node:people({key} = {value}) return n", params );

索引查询参数
1
2
3
Map<String, Object> params = new HashMap<String, Object>();
params.put( "query", "name:Andreas" );
ExecutionResult result = 
engine.execute("start n=node:people({query}) return n", params );

SKIP和LIMIT的数字参数
1
2
3
4
5
Map<String, Object> params = new HashMap<String, Object>();
params.put( "s", 1 );
params.put( "l", 1 );
ExecutionResult result =
engine.execute("start n=node(0,1,2) return n.name skip {s} limit {l}", params );

正则表达式参数
1
2
3
4
Map<String, Object> params = new HashMap<String, Object>();
params.put( "regex", ".*h.*" );
ExecutionResult result =
engine.execute("start n=node(0,1,2) where n.name =~ {regex} return n.name"
通过索引查询获取节点

如果开始节点可以通过索引查询得到,可以如此来写:

node:index-name(key=”value”)。在此列子中存在一个节点索引叫nodes。

查询:

START n=node:nodes(name = "A")

RETURN n

索引中命名为A的节点将被返回。

结果:



调用例子:

db = new ImpermanentGraphDatabase();
engine = new ExecutionEngine( db );
CypherParser parser = new CypherParser();
ExecutionEngine engine = new ExecutionEngine(db);
Query query = parser.parse( "start n=(0) where 1=1 return n" );
ExecutionResult result = engine.execute( query );
assertThat( result.columns(), hasItem( "n" ) );
Iterator<Node> n_column = result.columnAs( "n" );
assertThat( asIterable( n_column ), hasItem(db.getNodeById(0)) );
assertThat( result.toString(), containsString("Node[0]") );

相关推荐