Search code examples
databasegraphneo4j

Neo4j query without specifying relation type


I have a graph representation of data with multiple nodes with random relationships

I'm using Neo4j to represent this and using query Using query

MATCH (a)-[r]->(b)-[r2]->(c)

I'm getting an output where ever A is related with B and B is related with C enter image description here

But I need to query nodes A and B with any relationship (call as RelationA) and all C following B (with RelationA) with the Same relation as A and B like the following image

enter image description here

if A is connected with more than one B then the expected graph will be like this

enter image description here


Solution

  • You can check the equality of the relationships in the WHERE clause.

    MATCH path=(a)-[r]->(b)-[r2]->(c) 
    WHERE type(r)=type(r2)  
    RETURN path
    

    P.S.: If you are viewing this result in the Neo4j browser, then it "connects the result nodes" which means it adds extra relationships between nodes that don’t match the criteria. Don’t forget to uncheck the "connect result nodes" option in the from the settings.