2015-10-19 58 views
0

我有一個大的Cypher查詢,我收集了一些信息片段在我的應用程序的視圖。在一部分中,我使用可選匹配來獲得即將到來的聚會,但是我需要他們在進入COLLECT之前通過ASC訂購,並使用WITH語句向前傳遞。我試圖將它添加到WHERE子句的下面,顯然,它會拋出一個錯誤。ORDER BY之前收集和與

有沒有辦法在可選MATCH或COLLECT語句中進行排序?

這是我的Cypher查詢

MATCH (o:Person)<-[:OWNED_BY]-(g:Group {group_slug:"'.$group_slug.'"})<-[:MEMBER_OF]-(m:Person)-[:JOB_TITLE]->(title:Title) 
WITH o,g,COLLECT({first_name: m.first_name, last_name: m.last_name, image_name: m.image_name, username: m.username, job_title: title.job_title}) as members,title,COUNT(m) as member_count 
OPTIONAL MATCH (g)-[:TOPIC_OF]->(t:Topic) 
WITH o,members,g,COLLECT(DISTINCT t.name) as topics,title,member_count 
OPTIONAL MATCH (g)-[:CREATED]->(mu:Meetup{status:"Active"})-[:MEETUP_AT]->(l:Location) 
WHERE toInt(mu.meet_date) >= '.time().' 
WITH o,members,g,title,topics,COLLECT({location_name:l.location_name, address:l.address,city:l.city,state:l.state,zip_code:l.zip_code,meet_date:mu.meet_date,meeting_id:id(mu)}) as meetups,member_count 
OPTIONAL MATCH (g)-[]->(amu:Meetup) 
WITH o,members,g,title,topics,meetups,COUNT(amu) as meetup_count,member_count 
OPTIONAL MATCH (g)<-[:REQUESTED]-(pm:Person)-[:JOB_TITLE]->(pmt:Title) 
WITH o,members,g,title,topics,meetups,meetup_count,COLLECT({first_name: pm.first_name, last_name: pm.last_name, image_name: pm.image_name, username: pm.username, job_title: pmt.job_title}) as requested,member_count 
OPTIONAL MATCH(g) <-[:FAVORITED]-(fp:Person) 
WITH o,members,g,title,topics,meetups,meetup_count,requested,COUNT(fp) as favorited,member_count 
RETURN o.username as owner_username, o.first_name as owner_first, o.last_name as owner_last, g.group_name as group_name, g.description as description, g.access as access, g.group_slug as group_slug, 
topics, members,meetups,meetup_count, favorited,requested,member_count 

其中違規查詢部分是

OPTIONAL MATCH (g)-[:CREATED]->(mu:Meetup{status:"Active"})-[:MEETUP_AT]->(l:Location) 
WHERE toInt(mu.meet_date) >= time() 
+0

什麼是錯誤訊息?另外請注意,timestamp函數是'timestamp()'而不是'time()'。 –

+0

時間()是PHP。對不起,我沒有指定 –

回答

0

將帖子

WITH子句可隨後ORDER BYSKIPLIMIT子句。

此查詢是否適合您?

WITH timestamp() AS now 
MATCH (o:Person)<-[:OWNED_BY]-(g:Group {group_slug:"'.$group_slug.'"})<-[:MEMBER_OF]-(m:Person)-[:JOB_TITLE]->(title:Title) 
WITH o,g,COLLECT({first_name: m.first_name, last_name: m.last_name, image_name: m.image_name, username: m.username, job_title: title.job_title}) as members,title,COUNT(m) as member_count 
OPTIONAL MATCH (g)-[:TOPIC_OF]->(t:Topic) 
WITH o,members,g,COLLECT(DISTINCT t.name) as topics,title,member_count 
OPTIONAL MATCH (g)-[:CREATED]->(mu:Meetup{status:"Active"})-[:MEETUP_AT]->(l:Location) 
WHERE mu.meet_date >= now 
WITH o,members,g,topics,title,member_count,mu,l 
ORDER BY mu.meet_date 
WITH o,members,g,title,topics,COLLECT({location_name:l.location_name, address:l.address,city:l.city,state:l.state,zip_code:l.zip_code,meet_date:mu.meet_date,meeting_id:id(mu)}) as meetups,member_count 
OPTIONAL MATCH (g)-[]->(amu:Meetup) 
WITH o,members,g,title,topics,meetups,COUNT(amu) as meetup_count,member_count 
OPTIONAL MATCH (g)<-[:REQUESTED]-(pm:Person)-[:JOB_TITLE]->(pmt:Title) 
WITH o,members,g,title,topics,meetups,meetup_count,COLLECT({first_name: pm.first_name, last_name: pm.last_name, image_name: pm.image_name, username: pm.username, job_title: pmt.job_title}) as requested,member_count 
OPTIONAL MATCH(g) <-[:FAVORITED]-(fp:Person) 
WITH o,members,g,title,topics,meetups,meetup_count,requested,COUNT(fp) as favorited,member_count 
RETURN o.username as owner_username, o.first_name as owner_first, o.last_name as owner_last, g.group_name as group_name, g.description as description, g.access as access, g.group_slug as group_slug, 
topics, members,meetups,meetup_count, favorited,requested,member_count; 

爲了提高效率,我們首先創建一個now變量,而不是重新計算每次我們需要它時的當前時間。這也將確保一致的結果:

WITH timestamp() AS now 

中的「違規」查詢部分的WHERE條款已被取代:

WHERE mu.meet_date >= now 
WITH o,members,g,topics,title,member_count,mu,l 
ORDER BY mu.meet_date 
+0

現在的順序工作,但我仍然只能拉日期大於或等於今天? –

+0

看到我編輯的答案。 – cybersam