Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
03-11-2021 10:27 AM
First, I am continuously learning new ways in Neo4j and APOC, have tried ingesting from CSVs and exporting using APOC and querying methods. I am also researching knowledge sites as I learn and explore methods in Desktop 1.3.x
For this type of query, I have ingested data like the pattern below. I am unable to find a path and list all Schools and Games that are common between Child 1 and Child 2, for example. I tried to follow the apoc.coll.intersection example on this link but could not replicate and result was , not sure if it applies to this case, Performing match intersection - Knowledge Base
Appreciate any suggestions or solution as I tried [*1..2] and [*1..3], obviously with different results, not sure if either is correct to use.
MATCH path = (Parents:PARENT)-[*1..3]-(Games:GAME)
where Parents.name=“Child1" OR Parents.name=“Child2"
RETURN path
How does the solution change to know the path and list all Games (regardless of Schools) that are common between Child 1 and Child 2?
Thanks for your help.
Solved! Go to Solution.
03-16-2021 06:21 PM
Try this:
Query 1:
match (c:Child)-[]->(a:School)
where c.name = "C1"
match (d:Child)-[]->(a)
where d.name = "C2"
match (a)-[]->(g:Game)
return c, d, a, g
//c.name, d.name, a.name, g.name
Query 2:
match (g:Game)
match (a:Child)-[]->()-[]->(g)
where a.name = "C1"
match (b:Child)-[]->()-[]->(g)
where b.name = "C2"
return a, b, g
//a.name, b.name, g.name
03-11-2021 03:59 PM
Find the path:
match (p:Parent)
CALL apoc.path.spanningTree(p,{maxLevel:3}) YIELD path
return path
Find the number of children playing the same game:
match (p:Parent)-[]->(c:Child)-[]->(s:School)-[]->(f:Plays)
with p.name as parent, f.name as plays, count(distinct c.name) as cnt
return parent, plays, cnt order by cnt desc
Result:
03-16-2021 03:13 PM
Thanks ameyasoft for your suggestion on the cypher. I am trying to understand the apoc.path.spanningTree results and what it does by including where clause. I also tried the second query and again, am trying to understand the results as it shows me the counts and I am trying to list out the nodes, relationships, and show the graph. So, I am trying variations. Still learning.
I checked on requirements so am going to try present here again. The School to Game is a 1-to-1 relationship not 1-to-many as it shows in my drawing. That's a correction.
The data is like this:
Child1, School1, Soccer
Child1, School2, Rugby
Child1, School3, Football
Child2, School1, Soccer
Child2, School2, Rugby
Child2, School4, Swimming
Child2, School4, Football
Query 1). Show graph output where the Schools AND Games both match for Child1 and Child2
Resulting graph shows 6 nodes and 6 relationships
Nodes: (2 Children, 2 Schools, and 2 Games) = 6
Relationships: (4 between Children and Schools) + (2 between Schools and Games) = 6
Child1, School1, Soccer
Child2, School1, Soccer
Child1, School2, Rugby
Child2, School2, Rugby
Query 2). Show graph output where the Game is common for Child1 and Child2, regardless of the Schools
Resulting graph shows 2 additional paths
Child1, School1, Soccer
Child2, School1, Soccer
Child1, School2, Rugby
Child2, School2, Rugby
Child1, School3, Football
Child2, School4, Football
Greatly appreciate any further suggestions.
03-16-2021 06:21 PM
Try this:
Query 1:
match (c:Child)-[]->(a:School)
where c.name = "C1"
match (d:Child)-[]->(a)
where d.name = "C2"
match (a)-[]->(g:Game)
return c, d, a, g
//c.name, d.name, a.name, g.name
Query 2:
match (g:Game)
match (a:Child)-[]->()-[]->(g)
where a.name = "C1"
match (b:Child)-[]->()-[]->(g)
where b.name = "C2"
return a, b, g
//a.name, b.name, g.name
03-18-2021 10:42 AM
Awesome, Thanks. Perfect solution and it worked with no changes for Query 1 and with a minor change to Query 2.
In general, I am not sure I understand the difference between using
where c.name = "C1" or c.name = "C2" (//OR vs. AND)
vs. splitting them as below with two match statements
match ...
where c.name = "C1"
match ...
where d.name = "C2"
For Query 2, the results with no changes gave me the Child nodes C1 and C2 and Games but no relationships in the graph. So, I tried changing as below and it worked.
added school in the middle for both match statements
match (a:Child)-->(s:School)-->(g)
...
match (b:Child)-->(t:School)-->(g)
I am constantly learning and always looking for pointers to solve complex queries that I know are coming.
03-18-2021 11:01 AM
In general, I am not sure I understand the difference between using
where c.name = "C1" or c.name = "C2" (//OR vs. AND)
A. First we have to collect all the games child C1 plays. Then we will check to see if child C2 has played any games that child C1 plays.
All the sessions of the conference are now available online