cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Help with children / parents query

Hi, could you please help with finding query ? I am absolute beginner.

Screenshot from 2022-12-06 11-14-56.png

Here is my schema. The problem: given list of X nodes, which are children of C,D,E,F,G find node B which has the most X nodes connected through nodes C,D,E,F,G.

After that, I need to go 1 level higher, meaning, I need to find node A. How the cypher queries would look like ?

Thanks for help

1 ACCEPTED SOLUTION

You can try something like this. 

with ['i','love','neo4j','its','a','great','tool'] as keywords
match(s:SONG)-[:HAS_X|HAS_LYRICS|HAS_INSTRUMENT|HAS_AUTHOR]->()-[:HAS_KEYWORD]->(k:KEYWORD)
where k.name in keywords
with s, count(*) as cnt
order by cnt desc
limit 1
match(s)<-[:BELONGS_TO]-(g:GENRE)
return s.name, g.name, cnt

View solution in original post

7 REPLIES 7

You did not provide node labels, nor relationship types, so I wrote a generic query you can modify with your specific constraints on node labels and relationships types. I also assumed the letters represented the nodes 'name' property. Change this to match your data model.

with['C','D','E','F','G'] as nodes
match(b:B)-->(n)
where n.name in nodes
with b, count(*) as numOfNodes
order by numOfNodes desc
limit 1
match(a:A)-->(b)
return a.name, b.name, numOfNodes

 

I am rather trying specify X nodes as input to the query. Something like in here
https://community.neo4j.com/t5/neo4j-graph-platform/cypher-query-to-match-draw-the-path-from-a-leave...
However without relationship between yellow nodes.  

ameyasoft
Graph Maven
Try this:
I created this:

Screen Shot 2022-12-06 at 12.25.02 PM.png

Ran this:
match (a:GrandChild)-[]-(b:GrandChild2)
where b.grandChild2 in['C1', 'E1', 'G1']
match (c:Child)-[]-(a)
with distinct c.child as chld1, count(distinct a) as cnt
return chld1, cnt order by cnt desc

Result:
Screen Shot 2022-12-06 at 12.25.53 PM.png

 

Ok I found some more professional tool to explain my problem. 
Here is schema of my database:

b.png

 

Now, given list of kewords for example ['i', 'love', 'neo4j', 'its', 'a', 'great', 'tool'], I want to find best corresponding song and genere, probbably based of the number of connections from keywords to specific song. 


How is that sounds ?

ameyasoft
Graph Maven

 

Try this:
I created this:

 

Screen Shot 2022-12-06 at 2.45.45 PM.png

Ran this query:
with ['i', 'love', 'neo4j', 'its'] as s1
match (a:Keyword) where a.keyword in s1
match (b:Song)-[]-(c)-[]-(a)
return a, b, c


Result:

Screen Shot 2022-12-06 at 2.46.44 PM.png

 

 

 

You can try something like this. 

with ['i','love','neo4j','its','a','great','tool'] as keywords
match(s:SONG)-[:HAS_X|HAS_LYRICS|HAS_INSTRUMENT|HAS_AUTHOR]->()-[:HAS_KEYWORD]->(k:KEYWORD)
where k.name in keywords
with s, count(*) as cnt
order by cnt desc
limit 1
match(s)<-[:BELONGS_TO]-(g:GENRE)
return s.name, g.name, cnt