In my graph I have two node types: Page and Taxon. All Page nodes belong to Taxons and a Taxon may have ancestor Taxons.
Specifically:
- any Page belongs to one or more Taxons (relationship: BELONGS_TO)
- any Taxon may have one parent Taxon (relationship: HAS_PARENT)
- Taxons have a unique "name" property
My problem is: given a taxon name, match all the Pages that belong to the Taxon with this name, or that "descend" from a Taxon with this name.
This can be done by using a variable-length pattern, with one gotcha
MATCH (p:Page)-[:IS_TAGGED_TO]->(t:Taxon)-[:HAS_PARENT*]->(:Taxon {key: 'value'})
RETURN t.key, COUNT(p)
;
It doesn't work, because * means "one or more", not the conventional "zero or more" of other wildcard syntaxes such as regex. In other words, * expands to *1.. not *0.. The solution is to spell it out as *0..
MATCH (p:Page)-[:IS_TAGGED_TO]->(t:Taxon)-[:HAS_PARENT*0..]->(:Taxon {key: 'value'})
RETURN t.key, COUNT(p)
;