2016-07-10 32 views
3

我搜索圖的最長路徑,並且想要計算此最長路徑的不同節點的數量。neo4j如何在路徑的節點上使用count(distinct())

我想用count(distinct())

我嘗試了兩種查詢。

首先是

match p=(primero)-[:ResponseTo*]-(segundo) 
with max(length(p)) as lengthPath 
match p1=(primero)-[:ResponseTo*]-(segundo) 
where length(p1) = lengthPath 
return nodes(p1) 

查詢結果與路徑節點的圖。

但是,如果我試圖查詢

match p=(primero)-[:ResponseTo*]-(segundo) 
with max(length(p)) as lengthPath 
match p1=(primero)-[:ResponseTo*]-(segundo) 
where length(p1) = lengthPath 
return count(distinct(primero)) 

結果是

count(distinct(primero)) 
2 

如何使用count(distinct())在節點PRIMERO。

節點Primero有一個名爲id的字段。

回答

2

您應該至少綁定其中一個節點,添加一個方向並考慮一個路徑限制,否則這是一個非常昂貴的查詢。

match p=(primero)-[:ResponseTo*..30]-(segundo) 
with p order by length(p) desc limit 1 
unwind nodes(p) as n 
return distinct n; 
相關問題