ms007
(Marc Seiferle)
January 19, 2022, 2:48pm
1
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
cobra
(Cobra)
January 19, 2022, 3:10pm
2
Hello @ms007
MATCH (n:KnowledgeEntry)
WHERE n.title CONTAINS 'Talent'
AND NOT EXISTS((n)<-[:IS_PART]-(:KnowledgeEntry))
RETURN n
Regards,
Cobra
ms007
(Marc Seiferle)
January 19, 2022, 4:42pm
3
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?
cobra
(Cobra)
January 19, 2022, 4:52pm
4
On your example, which nodes should be returned?
ms007
(Marc Seiferle)
January 19, 2022, 4:54pm
5
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?
cobra
(Cobra)
January 19, 2022, 4:59pm
6
Why don't use the label of yellow nodes to get them all?
ms007
(Marc Seiferle)
January 19, 2022, 5:00pm
7
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
ms007
(Marc Seiferle)
January 19, 2022, 5:09pm
9
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.
cobra
(Cobra)
January 19, 2022, 5:29pm
10
Right now, nothing come to my mind
ms007
(Marc Seiferle)
January 19, 2022, 5:41pm
11
thank you. You put me in the right direction. Learnd something and you have saved me a lot of time
1 Like