Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
12-20-2022 03:51 AM
Hi
I want to query on this data in Graph Neo4j
I want to get all data till the end node having relation "TRANSFORMS_INTO", both forward and backward
CASE 1
if I query on NODE "D"
then in forward I should get "G","E","H"
but in backward I should only get "B","A" but not ("C","F")
(means i should not get forward node of a backward node)
CASE 2
If I query on NODE "A"
it should get all NODES
as all are forward
CASE 3
if i query on NODE "H"
then i should get "E",''D","B","A" NOT "C","F","G"
(means i should not get forward node of a backward node)
(NOTE THIS CHAIN CAN HAVE N NUMBER OF STEPS)
single query for all cases
AND in all cases i want data of ORANGE COLOR nodes along with BLUE nodes
When I use this Query
Please Help me write a query
12-20-2022 05:43 AM
You need to specify the direction of the relationship in your match pattern.
for case 1, looking for forward nodes:
match p=(n:ASSET {name:"D"})-[:TRANSFORMS_INTO*]->(:Asset) return p
for case 1, looking for backwards nodes:
match p=(n:ASSET {name:"D"})<-[:TRANSFORMS_INTO*]-(:Asset) return p
the above queries return paths. Based on your data, you will get two paths for the forward case and one path for the backward case. It you just want the collection of nodes along all paths, you can do something like the following:
match p=(n:ASSET {name:"D"})-[:TRANSFORMS_INTO*]->(:Asset)
unwind nodes(p) as asset
return collect(distinct asset) as assets
you can add the collection of orange nodes with the following:
match p=(n:ASSET {name:"D"})-[:TRANSFORMS_INTO*]->(:Asset)
unwind nodes(p) as asset
optional match (asset)-[:HAS_EVENT]->(e:Event)
return collect(distinct asset) as assets, collect(e) as events
12-20-2022 05:45 AM
You can also use the apoc path procedures to get all the nodes along a path. I will most likely be more efficient and has a lot of configuration options.
12-21-2022 04:48 AM
match p=(n:ASSET {name:"D"})-[:TRANSFORMS_INTO*]->(:Asset)
unwind nodes(p) as asset
optional match (asset)-[:HAS_EVENT]->(e:Event)
return collect(distinct asset) as assets, collect(e) as events
in this i also want distinct all relations
and asset events as one variable
and all relations as one variable
can you please help
12-21-2022 06:42 AM
If you want to collect both the nodes and relationships along all the paths, it will be much easier to use the apoc.path.subgraphAll() procedure. It eliminates trying to build all the collection with multiple 'unwind's and 'collect distinct'. The following should give you the nodes and relationships along the forward paths. the subgraphAll procedure is configured to only traverse 'Asset' and 'Event' nodes along outgoing 'TRANSFORMS_INTO' and outgoing 'HAS_EVENT' relationships.
MATCH (n:ASSET{uniqueID: "UNIQUE_ID"})
CALL apoc.path.subgraphAll(n, {
relationshipFilter: "TRANSFORMS_INTO>|HAS_EVENT>",
nodeFilter: "Asset|Event"
})
YIELD nodes, relationships
RETURN nodes, relationships
You can get the backward paths by switching the relationship filter on the 'TRANSFORMS_INTO' relationship to incoming.
MATCH (n:ASSET{uniqueID: "UNIQUE_ID"})
CALL apoc.path.subgraphAll(n, {
relationshipFilter: "<TRANSFORMS_INTO|HAS_EVENT>",
nodeFilter: "Asset|Event"
})
YIELD nodes, relationships
RETURN nodes, relationships
These queries mix the 'Asset' and 'Event' nodes into the same collection. If you want the 'Event' nodes in a separate collection, you can use this version (forward path):
MATCH (n:ASSET{uniqueID: "UNIQUE_ID"})
CALL apoc.path.subgraphAll(n, {
relationshipFilter: "TRANSFORMS_INTO>",
nodeFilter: "Asset"
})
YIELD nodes, relationships
CALL {
with nodes
unwind nodes as asset
optional match (asset)-[:HAS_EVENT]->(e:Event)
return collect(e) as events
}
return nodes as assets, relationships, events
If you want the 'Event' and its corresponding relationship, you can collect them in a map as follows:
MATCH (n:ASSET{uniqueID: "UNIQUE_ID"})
CALL apoc.path.subgraphAll(n, {
relationshipFilter: "TRANSFORMS_INTO>",
nodeFilter: "Asset"
})
YIELD nodes, relationships
CALL {
with nodes
unwind nodes as asset
optional match (asset)-[r:HAS_EVENT]->(e:Event)
return collect({event: e, rel: r}) as events
}
12-21-2022 10:04 PM
I need both forward and backward TRANSFORMS_INTO
in a single query
that's why am trying to use UNION to get desired result
but here also I will not be able to use UNION
12-21-2022 11:50 PM
You should be able to join the two queries together with a ‘union’ clause.
12-21-2022 05:57 AM
I am having one more issue
union on query is not working in neo4j driver
I used this query
match p=(n:ASSET{uniqueID: "UNIQUE_ID"})<-[TRANSFORMS_INTO*]-()-[:HAS_EVENT]->() unwind nodes(p) as asset unwind relationships(p) as relationsp optional match p2=(asset)-[:HAS_EVENT]->(e:EVENT) unwind relationships(p2) as relationsp2 return collect(distinct asset)+collect(DISTINCT e) as distinctNodes, collect(DISTINCT relationsp)+collect(DISTINCT relationsp2) as distinctRelationships
union
match p=(n:ASSET{uniqueID: "UNIQUE_ID"})-[TRANSFORMS_INTO*]->()-[:HAS_EVENT]->() unwind nodes(p) as asset unwind relationships(p) as relationsp optional match p2=(asset)-[:HAS_EVENT]->(e:EVENT) unwind relationships(p2) as relationsp2 return collect(distinct asset)+collect(DISTINCT e) as distinctNodes, collect(DISTINCT relationsp)+collect(DISTINCT relationsp2) as distinctRelationships
Please check this
https://community.neo4j.com/t5/neo4j-graph-platform/neo4j-driver-union-not-working/m-p/63343#M37428
All the sessions of the conference are now available online