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

996 Views Asked by At

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

2

There are 2 best solutions below

0
On BEST ANSWER

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)
7
On

You can use a variable length relationship in your query. *6 means you can get the nodes from Kingdom up to max of six hops (or relationships) until you reach Species. Then from this path, return all nodes that you found.

MATCH p=(:Kingdom)-[:RELTYPE*6]-(:Species)
RETURN nodes(p)