简体   繁体   English

Cypher可选配对

[英]Cypher Optional Match

I have a graph in that contains two types of nodes (objects and pieces) and two types of links (similarTo and contains). 我有一个图表,其中包含两种类型的节点(对象和片段)和两种类型的链接(similarTo和contains)。 Some pieces are made of the pieces. 有些碎片是由碎片制成的。

I would like to extract the path to each piece starting from a set of objects. 我想从一组对象开始提取每个部分的路径。

MATCH (o:Object)
WITH o
OPTIONAL MATCH path = (p:Piece) <-[:contains*]- (o) -[:similarTo]- (:Object)
RETURN path

The above query only returns part of the pieces. 以上查询仅返回部分内容。 In the returned graph, some objects do not directly connect to any pieces, the latter are not returned, although they actually do! 在返回的图形中,一些对象不直接连接到任何块,后者不会返回,尽管它们实际上是这样做的!

I can change the query to: 我可以将查询更改为:

MATCH (o:Object) -[:contains*]-> (p:Piece) 
OPTIONAL MATCH (o) –[:similarTo]- (:Object) 

However, I did not manage to return the whole path for that query, which I need to return collection of nodes and links with: 但是,我没有设法返回该查询的整个路径,我需要返回节点和链接的集合:

WITH rels(path) as relations , nodes(path) as nodes 
UNWIND relations as r unwind nodes as n 
RETURN {nodes: collect(distinct n), links: collect(distinct {source: id(startNode(r)), target: id(endNode(r))})}

I'd be grateful to any recommendation. 我很感激任何建议。

Would something like this do the trick ? 这样的事情可以做到吗?

I created a small graph representing objects and pieces here : http://console.neo4j.org/r/abztz4 我在这里创建了一个代表对象和碎片的小图: http//console.neo4j.org/r/abztz4

Execute distinct queries with UNION ALL 使用UNION ALL执行不同的查询

Here you'll combine the two use cases in one set of paths : 在这里,您将在一组路径中组合两个用例:

MATCH (o:Object)
WITH o
OPTIONAL MATCH p=(o)-[:CONTAINS]->(piece)
RETURN p
UNION ALL 
MATCH (o:Object)
WITH o
OPTIONAL MATCH p=(o)-[:SIMILAR_TO]-()-[:CONTAINS]->(piece)
RETURN p

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM