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.

Get terminating nodes in matching path

ms007
Node Link

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

1 ACCEPTED SOLUTION

ms007
Node Link

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?

View solution in original post

10 REPLIES 10

Hello @ms007

MATCH (n:KnowledgeEntry)
WHERE n.title CONTAINS 'Talent'
AND NOT EXISTS((n)<-[:IS_PART]-(:KnowledgeEntry))
RETURN n

Regards,
Cobra

ms007
Node Link

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?

On your example, which nodes should be returned?

ms007
Node Link

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?

Why don't use the label of yellow nodes to get them all?

ms007
Node Link

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

Your query looks good

ms007
Node Link

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.

Right now, nothing come to my mind

ms007
Node Link

thank you. You put me in the right direction. Learnd something and you have saved me a lot of time