有关注图
CREATE (s1:SOME {id:'s1'} )
CREATE (s2:SOME {id:'s2'} )
CREATE (a11:ANOTHER {id:'a11', tags:["tag1", "tag2"] } )
CREATE (a12:ANOTHER {id:'a12', tags:["tag2", "tag3"] } )
CREATE (a21:ANOTHER {id:'a21', tags:["tag3", "tag4"] } )
CREATE (a22:ANOTHER {id:'a22', tags:["tag4", "tag5"] } )
CREATE (s1)-[:CONTAINS ]->(a11),
(s1)-[:CONTAINS ]->(a12),
(s2)-[:CONTAINS ]->(a21),
(s2)-[:CONTAINS ]->(a22)
并使用以下查询填充记录:
match (s:SOME)
with s
OPTIONAL MATCH (a:ANOTHER)-[r:CONTAINS]-(s)
WITH s, COLLECT(DISTINCT a) AS aa
return s.id, aa
现在我想对ANOTHER.tags字段添加一些限制:
match (s:SOME)
with s
OPTIONAL MATCH (a:ANOTHER)-[r:CONTAINS]-(s)
where 'tag2' in a.tags
WITH s, COLLECT(DISTINCT a) AS aa
return s.id, aa
结果:
╒══════╤══════════════════════════════════════════════════════════════════════╕
│"s.id"│"aa" │
╞══════╪══════════════════════════════════════════════════════════════════════╡
│"s2" │[] │
├──────┼──────────────────────────────────────────────────────────────────────┤
│"s1" │[{"id":"a11","tags":["tag1","tag2"]},{"id":"a12","tags":["tag2","tag3"│
│ │]}] │
└──────┴──────────────────────────────────────────────────────────────────────┘
但是我想完全跳过s2记录,因为它不包含必需的标记
另一件事-如果我要检查多个标签怎么办?像[“ tag1”,“ tag2”](在这种情况下应该用空数据响应)我可以使用类似交集的东西吗?
答案 0 :(得分:0)
您只需要从OPTIONAL MATCH
切换到MATCH
,它应该跳过空值。
我们还可以仅使用一个MATCH
而不是两个来缩短查询:
match (s:SOME)<-[r:CONTAINS]-(s)
where 'tag2' in a.tags
WITH s, COLLECT(DISTINCT a) AS aa
return s.id, aa