Neo4j Match with properties on a variable length path - neo4j

I checked on the developer manual that you can filter with property on a variable length with relationship like below cypher.
MATCH p =(charlie:Person)-[* { blocked:false }]-(martin:Person)
WHERE charlie.name = 'Charlie Sheen' AND martin.name = 'Martin Sheen'
RETURN p
What I am looking here is can I filter by some logic on numeric properties on the relationship with a variable length.
For example, change the {blocked:false} in the query to a numeric properties like {amount_paid} and filter by amount_paid > 20.
So I can hop on Person by the relationship with amount_paid > 20.

You can use the predicate ALL:
MATCH p=(charlie:Person {name: 'Charlie Sheen')-[*]-(martin:Person {name: 'Martin Sheen')
WHERE ALL(rel in relationships(p) WHERE rel.amount_paind > 20)
RETURN p

Related

How to continue executing a Neo4J Cypher request after MATCH is not matched?

I have the following params in my Neo4J:
{
"lists": [
{
"from": "someone",
"to": "somebody"
}
]
}
And the following query:
MATCH (c:Concept{name:'infranodus'}) WITH c, $lists AS list
UNWIND CASE WHEN list = [{}] THEN [null] ELSE list END AS l
WITH l
MATCH (cp1:Concept{name:l.from})
WITH cp1
MATCH (cp2:Concept{name:'somebody'})
RETURN cp1,cp2;
The query above will work.
However, if I replace l.from with a non-existent parameter, e.g. l.about, then — as the match doesn't happen — the second cp2 match doesn't fire.
How can I change this behavior and continue executing this query even if cp1 is not found? Maybe there's a way to pass on a dummy variable as a result?
MATCH (c:Concept{name:'infranodus'}) WITH c, $lists AS list
UNWIND CASE WHEN list = [{}] THEN [null] ELSE list END AS l
WITH l
MATCH (cp1:Concept{name:l.about})
WITH cp1
MATCH (cp2:Concept{name:'somebody'})
RETURN cp1,cp2;
Use OPTIONAL MATCH. If there is no match is found, then it will use NULL for the missing part of the pattern. It is similar to outer join in SQL.
NEW:
OPTIONAL MATCH (cp1:Concept{name:l.about})
OLD:
MATCH (cp1:Concept{name:l.about})
You can maybe replace it with an IN predicate ?
For eg :
WITH {from: 'Matt Olg', about: 'Matthew Olg'}
AS l
MATCH (n:Person)
WHERE n.name IN [l.from, l.to]
RETURN n.name
╒══════════╕
│"n.name" │
╞══════════╡
│"Matt Olg"│
└──────────┘

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

How to check if node globally exists and set condition on it in Cypher?

I'm struggling with problem of creating query in Cypher.
Let's use this graph as an example:
MERGE(a:Person {name:'Alice', age:38, eyes:'brown'})
MERGE(c:Person {name:'Charlie', age:53, eyes:'green'})
MERGE(d:Person {name:'Daniel', age:54, eyes:'brown'})
MERGE(b:Person {name:'Bob', age:25, eyes:'blue'})
MERGE(a)-[:KNOWS]->(c)
MERGE(a)-[:KNOWS]->(b)
MERGE(c)-[:KNOWS]->(d)
MERGE(b)-[:KNOWS]->(d)
I would like to have query that returns me nodes with name = 'Alice' only if there aren't any nodes with name = 'Bob'. So if there is Bob I would like to see no results.
Here is what I've tried:
1) With exists()
MATCH p =(n)-[*1..3]->(b)
WHERE n.name = 'Alice' AND NOT EXISTS ((n {name: 'Bob'})-[*1..3]->(b))
RETURN p
But it returns all nodes and relationships.
2) With none()
MATCH p =(n)-[*1..3]->(b)
WHERE n.name = 'Alice' AND NONE (x IN nodes(p) WHERE x.name = 'Bob')
RETURN p
This returned all nodes, without Bob...
3) with any()
MATCH p =(n)-[*1..3]->(b)
WHERE n.name = 'Alice' AND NOT ANY (x IN nodes(p) WHERE x.name = 'Bob')
RETURN p
But this gave me the same result as above.
I'm running out of ideas how to return Alice only if Bob is not present.
What I would expect from query is to return Alice when I delete Bob, but when such node exists - nothing.
Any help is appreciated:)
Thanks!
You should try this query :
MATCH (n:Person {name:'Alice'})
WHERE NOT (n)-[:KNOWS*..3]-(:Person {name:'Bob'})
RETURN n

Neo4j - Cypher - Build object where Property is dynamic, like Type(r)

From my searchings I did not find a way to set the property of an object dynamically, only by defining it hardcoded (Like below the "Detail" property)
Here is my sample:
Neo4j Graph or via Neo4j Console
MATCH(p:Person)-[r]->(m:Movie)
WITH { Person: p.name,
Detail: collect(r.roles)
} AS Result
RETURN Result
My target (not working yet):
MATCH(p:Person)-[r]->(m:Movie)
WITH { Person: p.name,
CASTED_FOR: // --> Only values(r.roles) for relationship "CASTED_FOR" needed here
ACTED_IN: // --> Only values(r.roles) for relationship "ACTED_IN" needed here
} AS Result
RETURN Result
I know I could make e.g. [r:CASTED_FOR] or WHERE Type(r) = "CASTED_FOR", but I don't like to have multiple WITH and MATCH queries.
Thank you
This query uses Cypher's list comprehension construct to do what you want:
MATCH (p:Person)-[r]->(:Movie)
WITH p, COLLECT(r) AS rs
RETURN {
Person: p.name,
CASTED_FOR: [x IN rs WHERE TYPE(x) = 'CASTED_FOR' | x.roles],
ACTED_IN: [x IN rs WHERE TYPE(x) = 'ACTED_IN' | x.roles]
} AS Result

Neo4j Creating a relationship conditionally based on one of the property of node

I need to create two different type of relationship between two nodes. Type of relationship depends upon one of the property of the node.
For Example,
I have two nodes USER and EVENT. I have two relationships to create between them.
1. invite
2. requestToInvite
Even node has property inviteOnly.
Create a "Invite" relationship if inviteOnly is true. Otherwise create "requestToInvite" relationship.
This is what i am trying:
MATCH (u:User)
WHERE ID(u) = 13
WITH u
MATCH (e:Events)
WHERE ID(e) = 0
WITH u,e
CREATE (u)-[:inviteONLYTrue]->(e) WHERE e.inviteOnly = true
CREATE (u)-[:inviteONLYFALSE]->(e) WHERE e.inviteOnly = false
WITH u,e
RETURN u,e
Currently there is no conditional but you can work around it by iterating over a zero or one-element list which is created by a CASE statement.
MATCH (u:User) WHERE ID(u) = 13
MATCH (e:Events) WHERE ID(e) = 0
FOREACH (_ in case e.inviteOnly when true then [1] else [] end |
CREATE (u)-[:inviteONLYTrue]->(e) )
FOREACH (_ in case e.inviteOnly when false then [1] else [] end |
CREATE (u)-[:inviteONLYFALSE]->(e) )
RETURN u,e
APOC Procedures just updated with support for conditional cypher execution, but in this particular case all you'll need is a way to create a relationship with a dynamic relationship type. APOC has a procedure for this too. Here's an example:
MATCH (u:User)
WHERE ID(u) = 13
WITH u
MATCH (e:Events)
WHERE ID(e) = 0
WITH u,e, CASE WHEN e.inviteOnly THEN 'inviteONLYTrue' ELSE 'inviteONLYFALSE' END as relType
CALL apoc.create.relationship(u, relType, {}, e) YIELD rel
RETURN u,e

Resources