Search code examples
neo4jcypheragens-graph

Simplifying cypher, match all nodes and relationships between two node types


Is there anyway to further simplify this query? I want to go from kingdom to all the species, there are nodes related towards the species nodes. The image shows the result of this query, but is there any way to match the kingdom node and all the nodes and relationships until the species node. If you like: all nodes/relationships between kingdom and species.

There will never be a node not attached to the kingdom or species objects (it's a taxonomic hierarchy). The RELTYPE will probably change to something more meaningful.

MATCH (k:Kingdom)-
[:RELTYPE]->(p:Phylum)-
[:RELTYPE]-(c:Class)-
[:RELTYPE]-(o:Order)-
[:RELTYPE]-(f:Family)-
[:RELTYPE]-(g:Genus)-
[:RELTYPE]-(s:Species)
RETURN k, p, c, o, f, g, s

enter image description here


Solution

  • No need to specify the relationship types, but it is good practice to set an upper limit (even if it's a high one). For example:

    MATCH p=(:Kingdom)-[*..15]->(:Species)
    RETURN nodes(p)