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

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

Related

Writing cypher query for traversing the path

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

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

Match nodes with two different values in same property Neo4j

I have this kind of graph:
I want to retrieve all Products that have pending or open requests. This is how Im trying
MATCH (s:ServiceRequest {srStatus: "Open"} OR {srStatus: "Pending"}) -[:FOR]->(p:Product) RETURN p
But this does not work. How can I do that?
This should work:
MATCH (s:ServiceRequest)-[:FOR]->(p:Product)
WHERE s.srStatus IN ["Open", "Pending"]
RETURN p;
and so should this:
MATCH (s:ServiceRequest)-[:FOR]->(p:Product)
WHERE s.srStatus = "Open" OR s.srStatus = "Pending"
RETURN p;

How to set enumeration on properties for selected nodes in one cypher statement?

My graph contains a set of nodes which are enumerated using a dedicated field fid. I want to update this enumeration periodically.
My current approach is to reset the enumeration and execute multiple statements that increase the fid for each node.
1. (f:File) set f.fid = -1
for(int i = 0; i < count ; i++) {
2. (f:File) set f.fid = i where id(f) = nodeId
}
I guess it should be possible to execute this task using a single cypher statement using the foreach clause.
MATCH p=(f:File)
FOREACH (n IN nodes(p)| SET f.fid = -1 )
I was looking for something similar to this statement.
MATCH (f:File)
WITH COLLECT(f) AS fs
WITH fs, i = 0
FOREACH (f in fs, i=i+1| SET f.fid = i ) return f.fid, f.name
Based on the following console set : http://console.neo4j.org/r/447qni
The following query seems to do the trick :
MATCH (f:File)
WITH collect(f) as f, count(f) AS c
UNWIND range(0,c-1) AS x
WITH f[x] AS file,x
SET file.iteration = x+1

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