If I have a graph that looks like so for nodes (p) and (e):
(p:Person)-[r:WorksFor]->(e:Employer)
And I have the following data:
(Person {name: Andrew})-[r:WorksFor]->(Employer {name: Google})
(Person {name: James})-[r:WorksFor]->(Employer {name: Google})
(Person {name: James})-[r:WorksFor]->(Employer {name: Apple})
(Person {name: Evan})-[r:WorksFor]->(Employer {name: Apple})
How can I query between (Person {name: Evan}) through each relationship and get to (Person {name: Andrew}) returning each employer and person along the way with an arbitrary number of employers and persons in-between?
Ideally the above would return a chain that looked like:
(Andrew)->(Google)->(James)->(Apple)->(Evan)
Thank you for your help.
(EDIT) Addendum:
The following seems to work but only if the players are separated by only two degrees, is there a way to make this completely variable length?
MATCH
(p:Person {name: "Andrew"})-->(e:Employer)<--(p3:Person)-->(e2:Employer)<--(p2:Person {name: "Evan"})
RETURN *
You want variable-length pattern matching.
Depending on your graph you can define the relationships to traverse or leave off the type. We can omit the direction to specify that we don't care about the direction of relationships to traverse:
MATCH path = (p:Person {name: "Andrew"})-[:WorksFor*]-(p2:Person {name: "Evan"})
RETURN path
If you want the nodes in the path, you can return nodes(path) to get that list.
If you only want the shortest path between these two, you can match to both then match using the shortestPath function:
MATCH (p:Person {name: "Andrew"}), (p2:Person {name: "Evan"})
MATCH path = shortestPath((p)-[:WorksFor*]-(p2))
RETURN path
Related
I'm trying to find a match pattern to match paths of certain node types. I don't care about the type of relation. Any relation type may match. I only care about the node types.
Of course the following would work:
MATCH (n)-->(:a)-->(:b)-->(:c) WHERE id(n) = 0
But, some of these paths may have relations to themselves. This could be for :b, so I'd also like to match:
MATCH (n)-->(:a)-->(:b)-->(:b)-->(:c) WHERE id(n) = 0
And:
MATCH (n)-->(:a)-->(:b)-->(:b)-->(:b)-->(:c) WHERE id(n) = 0
I can do this with relations easily enough, but I can't figure out how to do this with nodes, something like:
MATCH (n)-->(:a)-->(:b*1..)-->(:c) WHERE id(n) = 0
As a practical example, let's say I have a database with people, cars and bikes. The cars and bikes are "owned" by people, and people have relationships like son, daughter, husband, wife, etc. What I'm looking for is a query that from a specific node, gets all nodes of related types. So:
MATCH (n)-->(:person*1..)-->(:car) WHERE Id(n) = 0
I would expect that to get node "n", it's parents, grandparents, children, grandchildren, all recursively. And then of those people, their cars. If I could assume that I know the full list of relations, and that they only apply to people, I could get this to work as follows:
MATCH
p = (n)-->(:person)-[:son|daughter|husband|wife|etc*0..]->(:person)-->(:car)
WHERE Id(n) = 0
RETURN nodes(p)
What I'm looking for is the same without having to specify the full list of relations; but just the node label.
Edit:
If you want to find the path from one Person node to each Car node, using only the node labels, and assuming nodes may create cycles, you can use apoc.path.expandConfig.
For example:
MERGE (mark:Person {name: "Mark"})
MERGE (lju:Person {name: "Lju"})
MERGE (praveena:Person {name: "Praveena"})
MERGE (zhen:Person {name: "Zhen"})
MERGE (martin:Person {name: "Martin"})
MERGE (joe:Person {name: "Joe"})
MERGE (stefan:Person {name: "Stefan"})
MERGE (alicia:Person {name: "Alicia"})
MERGE (markCar:Car {name: "Mark's car"})
MERGE (ljuCar:Car {name: "Lju's car"})
MERGE (praveenaCar:Car {name: "Praveena's car"})
MERGE (zhenCar:Car {name: "Zhen's car"})
MERGE (zhen)-[:CHILD_OF]-(mark)
MERGE (praveena)-[:CHILD_OF]-(martin)
MERGE (praveena)-[:MARRIED_TO]-(joe)
MERGE (zhen)-[:CHILD_OF]-(joe)
MERGE (alicia)-[:CHILD_OF]-(joe)
MERGE (zhen)-[:CHILD_OF]-(mark)
MERGE (anthony)-[:CHILD_OF]-(rik)
MERGE (martin)-[:CHILD_OF]-(mark)
MERGE (stefan)-[:CHILD_OF]-(zhen)
MERGE (lju)-[:CHILD_OF]-(stefan)
MERGE (markCar)-[:OWNED]-(mark)
MERGE (ljuCar)-[:OWNED]-(lju)
MERGE (praveenaCar)-[:OWNED]-(praveena)
MERGE (zhenCar)-[:OWNED]-(zhen)
Running a query:
MATCH (n:Person{name:'Joe'})
CALL apoc.path.expandConfig(n, {labelFilter: "Person|/Car", uniqueness: "NODE_GLOBAL"})
YIELD path
RETURN path
will return four unique paths from Joe node to the four car nodes. There are several options for uniqueness of the path, see uniqueness
The /CAR makes it a Termination label, i.e. returned paths are only up to this given label.
I have a graph dataset loaded in Neo4j with nodes being various persons and relationships being some "real" relationships between them. What makes it complicated is that each relationship has a time period during which it was valid. For example:
(p1:PERSON {name: "Andy"})
-[r1:HAS_RELATIONSHIP {from: "20190201", to: "20190215"}]->
(p2:PERSON {name: "Betty"})
-[r2:HAS_RELATIONSHIP {from: "20190301", to: "20190331"}]->
(p3:PERSON {name: "Cecil"})
I'd like to take one concrete person P and get a list of all persons with whom P was in an indirect relationship through other persons. It must hold that the intersection of dates in any relationship chain is nonempty.
So from the previous example, if we take Andy as P, the result should be Andy, Betty, because the relationship with Cecil was valid in a completely different period of time. But in the following case:
(p1:PERSON {name: "Andy"})
-[r1:HAS_RELATIONSHIP {from: "20190201", to: "20190215"}]->
(p2:PERSON {name: "Betty"})
-[r2:HAS_RELATIONSHIP {from: "20190210", to: "20190301"}]->
(p3:PERSON {name: "Cecil"})
the result should be Andy, Betty, Cecil.
Is there a way how to specify this condition in Cypher? I'm looking for an efficient solution which prunes the already found paths.
You basically have a list of intervals from all relationships on a path. For this list of intervals you need to check if they all overlap. This can be done by checking max(from) <= min(to), in cypher:
MATCH path=(p:PERSON {name:'Andy'})-[*..10]-(other) // Doesn't matter how you get the paths
UNWIND relationships(path) as r
WITH path,max(r.from) AS maxFrom,min(r.to) AS minTo
WHERE maxFrom <= minTo
RETURN extract(x in nodes(path) | x.name)
I would like to match all paths from one given node.
-->(c: {name:"*Tom*"})
/
(a)-->(b)-->(d: {name:"*Tom*"})
\
-->(e: {name:"*Tom*"})
These paths have specified structure that:
- the name of all children of the second-last node (b) should contain "Tom" substring.
How to write correct Cypher?
Let's recreate the dataset:
CREATE
(a:Person {name: 'Start'}),
(b:Person),
(c:Person {name: 'Tommy Lee Jones'}),
(d:Person {name: 'Tom Hanks'}),
(e:Person {name: 'Tom the Cat'}),
(a)-[:FRIEND]->(b),
(b)-[:FRIEND]->(c),
(b)-[:FRIEND]->(d),
(b)-[:FRIEND]->(e)
As you said in the comment, all requires a list. To get a list, you should use the collect function on the neighbours of b:
MATCH (:Person)-[:FRIEND]->(b:Person)-[:FRIEND]->(bn:Person)
WITH b, collect(bn) AS bns
WHERE all(bn in bns where bn.name =~ '.*Tom.*')
RETURN b, bns
We call b's neighbours as bn and collect them to a bns list.
I have the following query, where there are 3 MATCHES, connected with WITH, searching through 3 paths.
MATCH (:File {name: 'A'})-[:FILE_OF]->(:Fun {name: 'B'})-->(ent:CFGEntry)-[:Flows*]->()-->(expr:CallExpr {name: 'C'})-->()-[:IS_PARENT]->(Callee {name: 'd'})
WITH expr, ent
MATCH (expr)-->(:Arg {chNum: '1'})-->(id:Id)
WITH id, ent
MATCH (entry)-[:Flows*]->(:IdDecl)-[:Def]->(sym:Sym)
WHERE id.name = sym.name
RETURN id.name
The query returns two distinct id and one distinct entry, and 7 distinct sym.
The problem is that since in the second MATCH I pass "WITH id, entry", and two distinct id were found, two instances of entry is passed to the third match instead of 1, and the run time of the third match unnecessarily gets doubled at least.
I am wondering if anyone know how I should write this query to just make use of one single instance of entry.
Your best bet will be to aggregate id, but then you'll need to adjust your logic in the third part of your query accordingly:
MATCH (:File {name: 'A'})-[:FILE_OF]->(:Fun {name: 'B'})-->(ent:CFGEntry)-[:Flows*]->()-->(expr:CallExpr {name: 'C'})-->()-[:IS_PARENT]->(Callee {name: 'd'})
WITH expr, ent
MATCH (expr)-->(:Arg {chNum: '1'})-->(id:Id)
WITH collect(id.name) as names, ent
MATCH (entry)-[:Flows*]->(:IdDecl)-[:Def]->(sym:Sym)
WHERE sym.name in names
RETURN sym.name
I have two graphs built like this :
CREATE (level1a:Bug {name: 'a'})
CREATE (level1b:Bug {name: 'b'})
CREATE (level2c:Bug {name: 'c'})
CREATE (level2d:Bug {name: 'd'})
CREATE (level3e:Bug {name: 'e'})
CREATE (level3f:Bug {name: 'f'})
CREATE (level3g:Bug {name: 'g'})
CREATE (level3h:Bug {name: 'h'})
CREATE (level1a)-[:LINK]->(level2c)
CREATE (level1b)-[:LINK]->(level2d)
CREATE (level2c)-[:LINK]->(level3e)
CREATE (level2c)-[:LINK]->(level3f)
CREATE (level2d)-[:LINK]->(level3g)
CREATE (level2d)-[:LINK]->(level3h)
And also available here : http://console.neo4j.org/?id=duplicate_bug2
When I execute the query :
MATCH (a:Bug {name: 'a'})-[:LINK]->()-[:LINK]->(end) return end
I get the expected two nodes (f and e). But if I do two match queries like this :
MATCH (a:Bug {name: 'a'})-[:LINK]->()-[:LINK]->(end)
MATCH (b:Bug {name: 'b'})-[:LINK]->()-[:LINK]->(end2)
return end, end2
I get duplicates nodes in end and end2. Why is this? The two graphs are not even connected!
BR,
S
Since both matches will return multiple rows and there is no correlation between the two match statements it will generate a cross product of the two result sets. In this case it is 2x2 so you get four rows of each node with each node.
I think what you are after is something like this query. It finds all of the ends from the first match, combines them in a collection and then repeats the process for the second match. Then it returns a single row in the result set with all of the ends of a and all of the ends of b regardless of how many there are at the end of each match.
MATCH (a:Bug {name: 'a'})-[:LINK]->()-[:LINK]->(end)
with collect(end) as end
MATCH (b:Bug {name: 'b'})-[:LINK]->()-[:LINK]->(end2)
return end, collect(end2) as end2