我的用例包含两种类型的节点Problem
和Tag
,其中Problem
可以与Tag
具有“一对多”关系,即有多个单个问题的(Problem)-[:CONATINS]->(Tag)
关系(忽略语法)。使用给定的标记数组,我希望cypher查询获取Problem
,其中不包含任何Tag
示例节点:
Problem {id:101, rank:2.389}
; Tag {name: "python"}
答案 0 :(得分: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
答案 1 :(得分: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)
如果您希望问题未被python
或cypher
标记,则只需要返回问题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.