2015-06-09 46 views
1

我那裏有代表的職位post節點和comment節點上的帖子代表的意見圖形模型:暗號查詢返回節點的計數與結果不同的屬性值

(p:post)<-[:ON]-(c:comment{role:'question'}) 
    (p:post)<-[:ON]-(c:comment{role:'criticism'}) 

每個註釋具有role屬性,它是五個可能值之一。我想編寫一個查詢,以返回給定計劃中每個角色的評論總數。

此:

MATCH (:item:initiative{urlCode:'AAECyS'})<-[:TO]-(c:comment) WITH  
COLLECT(c) AS comments 
RETURN { 
    question:length(filter(x IN comments WHERE x.role = 'question')), 
    criticism:length(filter(x IN comments WHERE x.role = 'criticism')) 
    //etc. for other three types 
} 

給我我想要的東西 - 但感覺非常醜陋的我。

有沒有更清潔/更有效的方法來做到這一點?

回答

0

也許是最簡單的查詢(你會得到每個角色的行):

MATCH (:item:initiative{urlCode:'AAECyS'})<-[:TO]-(c:comment) 
RETURN c.role, count(*); 
0

你可以把它們放在一個集合中,並返回一個集合,而且會更整潔。

MATCH (:item:initiative{urlCode:'AAECyS'})<-[:TO]-(c:comment) 
WITH [c.role, count(*)] as role_count 
RETURN collect(role_count) as role_counts 
相關問題