2016-06-28 107 views
0

我的用例包含兩種類型的節點ProblemTag的其中一個Problem可以有「一到多」與Tag關係,即有多個(Problem)-[:CONATINS]->(Tag)關係,爲一個單一的問題節點(忽略句法)。隨着標籤的給定的數組我想暗號查詢來獲取Problem不包含任何那些TagNeo4j的排除匹配任何給定的關係

樣品節點:

Problem {id:101, rank:2.389}; Tag {name: "python"}

回答

0

考慮這個例子的數據集:

CREATE (p1:Problem {id:1}), 
     (p2:Problem {id:2}), 
     (p3:Problem {id:3}), 

     (t1:Tag {name:'python'}), 
     (t2:Tag {name:'cypher'}), 
     (t3:Tag {name:'neo4j'}), 
     (t4:Tag {name:'ruby'}), 

     (t1)-[:TAGS]->(p1), 
     (t2)-[:TAGS]->(p1), 
     (t2)-[:TAGS]->(p2), 
     (t3)-[:TAGS]->(p2), 
     (t3)-[:TAGS]->(p3), 
     (t4)-[:TAGS]->(p3) 

enter image description here

如果你想不受pythoncypher標籤的問題,你只希望問題3退還。

MATCH (t:Tag) 
WHERE t.name IN ['python', 'cypher'] 
MATCH (p:Problem) 
WITH p, sum(size((t)-[:TAGS]->(p))) AS matches 
WHERE matches = 0 
RETURN p; 

這將返回唯一的問題3.

+0

非常感謝@ nicole-white :)爲我完美工作 – esquarer

0

你的情況,你必須MATCH這不必Tag和連接到其它Tag節點(但不是在你的列表中的)連接節點的兩個節點。我會在兩個查詢拆分此:

// unconnected nodes 
MATCH (p:Problem) 
WHERE NOT (p)-[:CONTAINS]-() 
RETURN p 
// combine queries with union (both have to return the same) 
UNION 
// nodes which fullfill you criterion 
MATCH (p:Problem)-[:CONTAINS]->(t:Tag) 
// collect distinct Problem nodes with a list of associated Tag names 
WITH DISTINCT p, collect(t.name) AS tag_name 
// use a none predicate to filter all Problems where none 
// of the query tag names are found 
WHERE none(x IN ['python', 'java', 'haskell'] WHERE x IN tag_name) 
RETURN p 
+0

不,我想匹配的那些「一到多」關係中的任何一個。即使問題有一個給定的標籤..它將被排除 – esquarer

+0

是的,我忽略瞭如果有任何「標籤」存在,你想排除一個節點。其他答案很有效,但我認爲你也可以用'none'謂詞來做到這一點。 –