Writing cypher query for traversing the path - neo4j

I am new to Neo4j.
I am using this query to get the below layout in traversing the path in first image:
Code:
MATCH p = (r:Reports)<--(s:Schedules)<--(m:MDRMs)<--(br:Business_Requirements)-->(rp: Report_Logic)-->(ra: Reporting_layer_attributes)
where r.Report_Name ='FFIEC 031' and s.Schedule = 'RC-B - Securities' RETURN p
Blue coloured nodes are referred as Business_Requirements which have additional linked Silver coloured (Business_Attributes) nodes with relationship as MAPPED_TO.
How can I bring out the below layout through cypher query. Since the silver color nodes are not in the path, I was not able to pull the below required layout in the second image:

You can extend the MATCH by adding a second path p2, and including it in the RETURN
MATCH p = (r:Reports)<--(s:Schedules)<--(m:MDRMs)<--(br:Business_Requirements)-->(rp: Report_Logic)-->(ra: Reporting_layer_attributes),
p2 = (br)<-[:MAPPED_TO]-(ba:Business_Attributes)
where r.Report_Name ='FFIEC 031' and s.Schedule = 'RC-B - Securities'
RETURN p,p2
in case the MAPPED_TO rel is not always there, you can also use the OPTIONAL MATCH
MATCH p = (r:Reports)<--(s:Schedules)<--(m:MDRMs)<--(br:Business_Requirements)-->(rp: Report_Logic)-->(ra: Reporting_layer_attributes)
where r.Report_Name ='FFIEC 031' and s.Schedule = 'RC-B - Securities'
OPTIONAL MATCH p2 = (br)<-[MAPPED_TO]-(ba:Business_Attributes)
RETURN p,p2

Related

How to filter path results based on a specific node with a specific property

enter image description here
I have a query to find the paths between two nodes. Now, I want to find any paths that have a node with a status = 'down'. However, the only query that works is to find paths without any nodes with a status of down.
Here is the query that is able to give me all the paths without a node with a status = 'down'.
MATCH path=((dev)-[r:part_of|CONNECTS*..20]-(dev2))
WHERE dev.hostname = 'fwmc0208-01' AND dev2.hostname = 'cemc0208-01.edg'
WITH nodes(path) AS n
WHERE NONE(node IN n WHERE (node.status IS NOT NULL) and node.status = 'down')
RETURN n
enter image description here
If I replace NONE with ALL, I get zero results. I've tried flipping the status to equal 'up', but this doesn't work either.
You should use the ANY function:
MATCH path=((dev)-[r:part_of|CONNECTS*..20]-(dev2))
WHERE dev.hostname = 'fwmc0208-01' AND dev2.hostname = 'cemc0208-01.edg'
WITH nodes(path) AS n
WHERE ANY(node IN n WHERE (node.status IS NOT NULL) and node.status = 'down')
RETURN n

How to query with two conditions in Neo4j

I am new with Neo4j and I am stucked trying to get a query with two conditions, where I want to get all the "Autors" related to "Pixar" and "Fox". So far I have tried the following two ways:
MATCH (a:Autor)- [:AUTOR_DE]-> (t:Título) -[:PRODUCIDO_POR] ->( p:Productora {Nombre: "Pixar"}),
and
MATCH (a:Autor)- [:AUTOR_DE]-> (t:Título) -[:PRODUCIDO_POR] ->( p:Productora {Nombre: "Fox"}),
return a,p
and
MATCH (a:Autor)- [:AUTOR_DE]-> (t:Título) -[:PRODUCIDO_POR] ->( p:Productora)
WHERE ( (p:Productora) = "Fox" OR (p:Productora) = "Pixar")
return a,p
Thanks in advance
Assuming that a Productora node stores its name in a name property, and that every Productora node has a unique name, this should work:
MATCH (a:Autor)-[:AUTOR_DE]->(:Título)-[:PRODUCIDO_POR]->(p:Productora)
WHERE p.name = "Fox" OR p.name = "Pixar"
WITH a, COLLECT(DISTINCT p) AS ps
WHERE SIZE(ps) = 2
return a, ps
And this should also work:
MATCH (fox:Productora), (pixar:Productora), (a:Autor)
WHERE fox.name = "Fox" AND pixar.name = "Pixar" AND
(a)-[:AUTOR_DE]->(:Título)-[:PRODUCIDO_POR]->(fox) AND
(a)-[:AUTOR_DE]->(:Título)-[:PRODUCIDO_POR]->(pixar)
return a, fox, pixar

To get all nodes and path from edge to root using neo4j

I want to get all nodes information with paths from edge to root node.Using one of the edge property.
This is the three layer node structure.
MATCH (g:GrandChild{name:"C"})<-[:childToGrandChild]-(c:Child)<-[p:Parent*0..]-(c:Child) RETURN c,g,p
This will return only B,C nodes with relationship like this
cypher used
CREATE (p: Parent{name : '1'} ) RETURN p
MATCH (p:Parent) WHERE p.name = '1' CREATE (c: Child{name : '2'} )<-[:parentToChild]-(p) RETURN p
MATCH (c:Child) WHERE c.name = '3' CREATE (g: GrandChild {name : '2'} )<-[:childToGrandChild]-(c) RETURN c
Please help..
You have missed parentTochild relationship which will be like,
MATCH (g:GrandChild{name:"C"})<-[:childToGrandChild]-(c:Child)<-[parentToChild*0..]-(p:Parent)
RETURN c,g,p
Try this :
MATCH (g:GrandChild{name:"C"})<-[:childToGrandChild]-(c:Child)
MATCH (c)<-[p:Parent*0..]-(c2:Child)
RETURN c,c2,g,p

neo4j sum up values

I'm trying to get Total(Sum) of a property of "df" object.I have attached screen shot of sample database I'm using
I tried to get the graph using following query
MATCH P= (n:Org)-[:O_CH*0..]->()-[:LEAF*0..]->()-[:CH*0..]->()-[:FOR*0..]->() RETURN P
To create objects
create(n:Org{name:'Root',id:1})
create(n:Org{name:'L1.1',id:2,parent:1})
create(n:Org{name:'L1.2',id:3,parent:1})
create(n:Org{name:'L1.3',id:4,parent:1})
create(n:Org{name:'L2.1',id:5,parent:3})
create(n:Org{name:'L2.2',id:6,parent:4})
create(n:Op{name:'CH1',id:7,orgId:5})
create(n:Op{name:'CH2',id:8,orgId:5 ,parent:'CH1'})
create(n:Proj{name:'P1',id:9,opp:'CH2'})
create(n:Proj{name:'P2',id:10,opp:'CH1'})
create(n:R{name:'R1',id:200,orgId:2 })
create (n:df{id:100,map:8,forecast:toFloat(10)})
create (n:df{id:101,map:7,forecast:toFloat(10)})
create (n:df{id:102,map:9,forecast:toFloat(10)})
create (n:df{id:103,map:10,forecast:toFloat(10)})
create (n:df{id:104,map:200,forecast:toFloat(10)})
To Crate relationships
MATCH (c:Org),(p:Org) WHERE c.parent = p.id create (p)-[:O_CH]->(c)
MATCH (c:Op),(p:Op) WHERE c.parent = p.name create (p)-[:CH]->(c)
MATCH (c:Op),(p:Org) WHERE c.orgId = p.id create (p)-[:LEAF]->(c)
MATCH (c:Proj),(p:Op) WHERE c.opp = p.name create (p)-[:CH]->(c)
MATCH (c:R),(p:Org) WHERE c.orgId = p.id create (p)-[:LEAF]->(c)
MATCH (c:df),(p:) WHERE c.map = p.id create (p)-[:FOR]->(c)
I'm expecting 60 as total where I get 260 as total. Please let me know where I'm wrong . Need your help to figure out.
I'm trying to get Total(Sum) of a property of "df" object.
I believe you needs a simple query that match all nodes with label :df and return the sum of node.forecast. Try it:
// Match all nodes with :df label
MATCH(n:df)
// Return the sum of the property 'forecast' of each matched node
RETURN sum(n.forecast)
From comments:
Thank you Bruno.But the thing i need to get the aggregated value as a
example if i select L2.1 , i need to get the sum of df objects which
are under that node
This should work:
MATCH({name:'L2.1'})-[*]->(n)
RETURN SUM(n.forecast)

Shortest path through a node

How to get the shtortest path between two nodes, where the path is going through a specific node. This is what I've got so far:
MATCH (a:User { username:"User1" }),(b:User { username:"User2" }),(c:User { username:"User3" }),
p = allShortestPaths((a)-[*]-(b))
RETURN p
I want a result in which the path goes through User3 (c).
You can find each leg separately:
MATCH (a:User {username:"User1"}),(b:User {username:"User2"}),(c:User {username:"User3"}),
p = allShortestPaths((a)-[*]-(c)), q = allShortestPaths((c)-[*]-(b))
RETURN p, q
Be aware, though, that if you have very long paths, or cycles, this query can take a long time or never finish. You should consider putting an upper bound on the path lengths, e.g.:
MATCH (a:User {username:"User1"}),(b:User {username:"User2"}),(c:User {username:"User3"}),
p = allShortestPaths((a)-[*..10]-(c)), q = allShortestPaths((c)-[*..10]-(b))
RETURN p, q

Resources