2015-10-07 65 views
1
MATCH (category:Category {id:'123'}), 
        (stream:Stream {id:'567'}) 
OPTIONAL MATCH path=(stream)-[:CONTAINS]->(categories:Category)-[:CHILD_OF*0..50]->(category)-[:CHILD_OF*0..50]->(subcats2:Category)<-[:PHOTO_OF]-(photo) 
with stream, subcats2, photo 
OPTIONAL MATCH (stream)<-[:PARTICIPANT_OF]-(users:User)-[:OWNER]->(photo)-[:PHOTO_OF]->(subcats:Category 
return photo 

我想要這樣的查詢;Neo4j條件相交的可選匹配

如果流contains類別做的第一可選

如果流(已)participants用戶做第二個可選

是否已經都返回的照片

但在上面的查詢的交點;它就像聯合查詢,如果流有參與者或用戶,則返回OK,但如果它具有類別和用戶,則無法取交集。

需要像

MATCH (category:Category {id:'123'}), 
     (stream:Stream {id:'567'}) 
MATCH (stream)-[:CONTAINS]->(categories:Category) 
WITH count(categories) as cat_count 
IF cat_count > 0 
    MATCH path=(stream)-[:CONTAINS]->(categories:Category)-[:CHILD_OF*0..50]->(category)-[:CHILD_OF*0..50]->(subcats2:Category)<-[:PHOTO_OF]-(photo) 
    with stream, subcats2, photo 
MATCH (stream)-[:PARTICIPANT_OF]->(users:User) 
WITH count(users) as user_count 
IF user_count > 0 
    MATCH (stream)<-[:PARTICIPANT_OF]-(users:User)-[:OWNER]-> (photo)-[:PHOTO_OF]->(subcats:Category 
return photo 

任何想法?

+0

是'subcats'應該是'subcats2'? – cybersam

+0

他們是不同的subcats,它不是重要的部分@cybersam – tunaktunak

+0

我並不想暗示這是最重要的事情:-),但它確實會影響任何潛在的解決方案。 – cybersam

回答

0

你可以做一個聯盟,其中第二部分還檢查,如果它不被使用:

MATCH (stream:Stream {id:'567'}) 
WHERE size((stream)-[:CONTAINS]->()) > 0 
MATCH (category:Category {id:'123'}) 
MATCH path=(stream)-[:CONTAINS]->(categories:Category)-[:CHILD_OF*0..50]-(category)-[:CHILD_OF*0..50]->(subcats2:Category)<-[:PHOTO_OF]-(photo) 
RETURN stream, subcats2 as category, photo 

UNION 

MATCH (stream:Stream {id:'567'}) 
WHERE size((stream)-[:CONTAINS]->()) = 0 
MATCH (category:Category {id:'123'}) 
MATCH (stream)<-[:PARTICIPANT_OF]-(users:User)-[:OWNER]->(photo)-[:PHOTO_OF]-(category) 
return stream, category,photo 
+0

在這種情況下,如果第一個查詢返回時它不返回交集照片,第二個查詢應該使用從第一個查詢返回的照片 – tunaktunak

+0

我需要使用第二個查詢中的'photo' – tunaktunak