2017-04-01 55 views
0

這個工作查詢開始:如何使用計數過濾WHERE?

MATCH (post:Post)<-[:AUTHOR]-(author:User) 
    WHERE post.createdAt > { hotAfter } AND post.text =~ { keyword } 
    MATCH (post)-[:HAS_COMMENT]->(comment:Comment)<-[:AUTHOR]-(commentAuthor:User) 
    WITH post, author, count(DISTINCT commentAuthor) as participantsCount, count(comment) as commentsCount 
    ORDER BY (participantsCount * commentsCount) DESC 
    RETURN collect(post {.*, author, participantsCount, commentsCount})[0..{ LIMIT }] as posts 

我還喜歡濾波器的評論數量符合條件的職位:

WHERE (count(DISTINCT commentAuthor) * count(comment)) <= { someNumber } 

但我不知道在哪裏/如何申請這個邏輯例如這是不正確的:

MATCH (post:Post)<-[:AUTHOR]-(author:User) 
    WHERE post.createdAt > { hotAfter } AND post.text =~ { keyword } 
    MATCH (post)-[:HAS_COMMENT]->(comment:Comment)<-[:AUTHOR]-(commentAuthor:User) 
    WHERE count(DISTINCT commentAuthor) * count(comment) <= 10 
    WITH post, author, count(DISTINCT commentAuthor) as participantsCount, count(comment) as commentsCount 
    ORDER BY (participantsCount * commentsCount) DESC 
    RETURN collect(post {.*, author, participantsCount, commentsCount})[0..{ LIMIT }] as posts 

回答

1

你做你的ORDER BY之前,您可以將其添加爲WHERE子句:

MATCH (post:Post)<-[:AUTHOR]-(author:User) 
WHERE post.createdAt > { hotAfter } AND post.text =~ { keyword } 
MATCH (post)-[:HAS_COMMENT]->(comment:Comment)<-[:AUTHOR]-(commentAuthor:User) 
WITH post, author, count(DISTINCT commentAuthor) as participantsCount, count(comment) as commentsCount 
WITH post, author, participantsCount, commentsCount, participantsCount * commentsCount as countProduct 
ORDER BY countProduct DESC 
WHERE countProduct <= { someNumber } 
RETURN collect(post {.*, author, participantsCount, commentsCount})[0..{ LIMIT }] 
+0

嗨,其實只是嘗試了這一點,並查詢似乎無效。 – kayla

+0

你能提供關於錯誤的更多細節嗎? – InverseFalcon

+0

'無效的輸入'D'(第6行,第5列(偏移量:308)) 「ORDER BY participantsCount * commentsCount DESC」 ^' – kayla

0

過濾集合在最後似乎工作。

MATCH (post:Post)<-[:AUTHOR]-(author:User) 
    WHERE post.createdAt > { hotAfter } AND post.text =~ { keyword } 
    MATCH (post)-[:HAS_COMMENT]->(comment:Comment)<-[:AUTHOR]-(commentAuthor:User) 
    WITH post, author, count(DISTINCT commentAuthor) as participantsCount, count(comment) as commentsCount 
    ORDER BY (participantsCount * commentsCount) DESC 
    RETURN filter (
    x in collect(post {.*, author, participantsCount, commentsCount}) 
    WHERE (x.participantsCount * x.commentsCount) <= { someNumber } 
)[0..{ LIMIT }] as posts