Retrieve hierarchy based on node property

Hi,

I have nodes in my graph where they are have a hierarchy. The hierarchy is saved as a node property instead of edges. There are no connecting edges between these nodes in the graph.

eg: For,  P1 -CHILD-> P2, P3 -CHILD-> P4

P1 has Node property: "Child" - [P2,P3,P4]
P2 has Node property: "Child" - [P4]
P3 has Node property: "Child" - [P4]
P4 has Node property: "Child" - None

How can I retrieve all the hierarchy starting from P1 in a cypher query?

Thanks a lot!

You would need a recursive algorithm to traverse your data staring from the root node. Cypher doesn’t have that capability. You could write something if you had a max depth, but it would be every increasing in complexity each extras level.

The correct approach is to convert that data into relationships. This the point of using a graph database. You can do what you are doing with a relational database using a single table.

It would be very easy to write a cypher script to create the relationships. You then be able to use cypher or apoc path procedures to get all the nodes related to a given node.

Thanks a lot! I converted the data into relationships. I am able to retrieve the hierarchies now. How do I get the other relationships associated with the nodes in each level of the hierarchy for a knowledge graph using cypher/apoc?

What is your data model? What is your current query? What missing info do you want from the graph?

Hi,

I have a knowledge graph of this kind:

The P nodes are in a hierarchy and other nodes and relationships are connected to them, I want to retrieve the D nodes and the edges and nodes attached to them like C and X nodes shown in the sample image below.
I want to write a query where I provide the id for node P1 and subsequently retrieve all the connections.

You could do this in cypher, but you would get duplicate nodes and relations in your collections of paths returned, which you would need to filter out. An alternative is to use one of the apoc path procedures that are designed to travers a subgraphAll from a root node. They will be more efficient then doing similar stuff in cypher.

I suggest you look at using apoc.path.subgrapghAll if you want list of nodes and relationships back. Look at the other ones as well.

Hi,

I am using the apoc path procedures. For the following graph:


I have used the following query:

MATCH (p:P1 {id: "1"})
CALL apoc.path.subgraphAll(p, {
    relationshipFilter: "CHILD>|<KNOWS|FOLLOWS|HAS",
    minLevel: 1
    maxLevel: 4
})
YIELD nodes, relationships
RETURN nodes, relationships;

How do I filter nodes and edges on the basis of node/edge properties?

Oh, you also want to add constraints to exclude nodes and relationships with certain properties? The apoc.path procedures don’t have that capability.

If this was for me, I would write a custom procedure to perform this. I says this because I already have a suite of them I wrote for my application, so adding a new one is not issue.

If that is not feasible for you, I guess we can try a cypher solution. What are the properties?

Yes, edge properties like the HAS relationships has a p value, the FOLLOW relationships has also has a confidence score. I would like to filter these say all the HAS edges having <=0.05 pval and the FOLLOW edges having >0.8 confidence score.

Do you want the D node if the related C and X nodes don’t have relationship properties that meet your criteria?

Yes, I require all the D nodes, I need to filter the C and X nodes/edges mainly based on the edge properties.

please give me solution. because same issue I am also face