Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
01-19-2022 06:48 AM
Hey there
Given a hierarchical graph we search for nodes that contains a specific title and we only need the terminating nodes (nodes that do not have children with matching title)
You may take this simplyfied graph
MERGE (:KnowledgeEntry {title: "Kosten"})-[:IS_PART]->(:KnowledgeEntry {title: "Talent Programme"})-[:IS_PART]->(:KnowledgeEntry {title: "Programme"})-[:IS_PART]->(:KnowledgeEntry {title: "Talent Förderung"})-[:IS_PART]->(n0:KnowledgeEntry {title: "TalentFactory"})<-[:IS_PART]-(:KnowledgeEntry {title: "Projekte"})<-[:IS_PART]-(:KnowledgeEntry {title: "Bahnknowhow"})<-[:IS_PART]-(:KnowledgeEntry {title: "Visualisierung"})<-[:IS_PART]-(:KnowledgeEntry {title: "D3 Talente"})
MERGE (:KnowledgeEntry {title: "Talentierte Studenten"})-[:IS_PART]->(:KnowledgeEntry {title: "PiBS Talente"})-[:IS_PART]->(:KnowledgeEntry {title: "Studenten"})-[:IS_PART]->(n0)
We can get all nodes which contains a given part of the title with
MATCH (n:KnowledgeEntry) WHERE n.title contains 'Talent' RETURN n
But how can we get only the terminating nodes?
When searching for 'Talent' would be only the yellow ones in the Screen Shot below
Thank you for your appreciated help
Solved! Go to Solution.
01-19-2022 08:54 AM
Only the yellow ones.
Found a solution:
MATCH (n:KnowledgeEntry) WHERE n.title CONTAINS 'Talent'
AND NOT EXISTS {
(n)<-[:IS_PART*]-(m:KnowledgeEntry) WHERE m.title CONTAINS 'Talent'
}
RETURN n
Do you know how the repetitive CONTAINS part could be omited?
01-19-2022 07:10 AM
Hello @ms007
MATCH (n:KnowledgeEntry)
WHERE n.title CONTAINS 'Talent'
AND NOT EXISTS((n)<-[:IS_PART]-(:KnowledgeEntry))
RETURN n
Regards,
Cobra
01-19-2022 08:42 AM
Thank you Cobra for the quick reply.
Unfortunately this returns only two of the three yellow nodes.
The yellow node with the title 'Talent Programme' (path on the left in the screen shot) is not returned because it has a child element.
How to modify the NOT EXISTS query so that only nodes with the specific title should be non-existent?
01-19-2022 08:52 AM
On your example, which nodes should be returned?
01-19-2022 08:54 AM
Only the yellow ones.
Found a solution:
MATCH (n:KnowledgeEntry) WHERE n.title CONTAINS 'Talent'
AND NOT EXISTS {
(n)<-[:IS_PART*]-(m:KnowledgeEntry) WHERE m.title CONTAINS 'Talent'
}
RETURN n
Do you know how the repetitive CONTAINS part could be omited?
01-19-2022 08:59 AM
Why don't use the label of yellow nodes to get them all?
01-19-2022 09:00 AM
I only put it there to be able to create the screen shot to color the nodes I want to return.
In the real database there's no additional label
01-19-2022 09:07 AM
Your query looks good
01-19-2022 09:09 AM
Do you think the returned KnowledgeEntries from line 1 could somehow be reused in the WHERE clause on line 3.
Like this there will be another database hit to load the same nodes.
Or is this just how it is.
01-19-2022 09:29 AM
Right now, nothing come to my mind
01-19-2022 09:41 AM
thank you. You put me in the right direction. Learnd something and you have saved me a lot of time
All the sessions of the conference are now available online