Neo4j / Cypher - 如何获得所有不同的标签三元组?

时间:2013-05-17 09:55:04

标签: neo4j cypher

随着标签的引入,neo4j得到了一个管理实体的简洁机制。现在,让我们假设我们有一个包含许多不同实体的通用图,并希望找出哪种实体(标签)彼此相关。这是一个起点: http://console-test.neo4j.org/?id=wdnbuj

Graph Setup:
CYPHER 2.0
create 
(_1:Crew  {name:"Neo"}),
(_2:Crew  {name:"Morpheus"}),
(_3:Crew  {name:"Trinity"}),
(_4:Language  {name:"Cypher"}),
(_5:Machines  {name:"Agent Smith"}),
(_6:Machines  {name:"The Architect"}),
_1-[:KNOWS]->_2,
_1-[:LOVES]->_3,
_2-[:KNOWS]->_3,
_2-[:KNOWS]->_4,
_4-[:KNOWS]->_5,
_5-[:CODED_BY]->_6

Query:
CYPHER 2.0 
match x-[r]->y 
return head(labels(x)) as head, type(r), head(labels(y)) as tail

这将让我们了解彼此相关的实体:

+--------------------------------------+
| head       | type(r)    | tail       |
+--------------------------------------+
| "Machines" | "CODED_BY" | "Machines" |
| "Language" | "KNOWS"    | "Machines" |
| "Crew"     | "KNOWS"    | "Crew"     |
| "Crew"     | "KNOWS"    | "Language" |
| "Crew"     | "KNOWS"    | "Crew"     |
| "Crew"     | "LOVES"    | "Crew"     |
+--------------------------------------+

现在。是否有任何Cypher查询会为这些三元组返回不同的值?

奖金问题:我们可以获得它们的计数/频率吗?

1 个答案:

答案 0 :(得分:2)

我刚刚使用了你的起始查询,添加了distinct和count。那是你在找什么?或者你是否希望我打破标签集中的标签(如果没有某种展开功能,这是不容易的)。

match x-[r]->y 
return distinct head(labels(x)) as head, type(r), head(labels(y)) as tail

match x-[r]->y 
return head(labels(x)) as head, type(r), head(labels(y)) as tail, count(*)