user1313016
user1313016

Reputation: 43

In Neo4j find path from root to a node in a tree

I am using Neo4J neosemantics plugin to load ontology. I want to find the path (all intermediate nodes and edges with their properties) from root of the tree to a particular node. What would be the best (optimal) way to get this information in cypher? The nodes are connected through subclassof relationships.

Upvotes: 0

Views: 360

Answers (1)

user15863090
user15863090

Reputation: 21

If you import an ontology with neosemantics using the basic settings, you can get the path(s) between a class and it's parents all the way to the root with the following query. (I've imported the ESCO ontology):

MATCH path = (c:n4sch__Class)-[:n4sch__SCO*]->(root:n4sch__Class) //(1)
WHERE c.uri = "http://data.europa.eu/esco/skill/74ce6ebc-8886-437e-bee9-ce5735aaa020"   // (2)
    AND not (root)-[:n4sch__SCO]->() //(3)
RETURN path //(4)
  • in (1) we define the exploration pattern: follow SCO relationships all the way. That's what the asterisk means.
  • in (2) we specify the starting class by its uri ( although you could use a label or any other property)
  • in (3) we specify that the root is a root because it's not the child of any other class
  • in (4) we return the whole path. Great for visualisation in the browser like this one. But if you want to return just a property of the nodes (say for instance the skos:prefLabel) in the path you can use this fragment instead: RETURN [n in nodes(path) | n.skos__prefLabel]

Upvotes: 1

Related Questions