Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
11-17-2022 02:50 AM
Hi all,
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.
Solved! Go to Solution.
11-17-2022 03:04 AM - edited 11-17-2022 03:11 AM
Try this:
match(t:Taxon{name:’insert name to find’})
match(t)<-[:HAS_PARENT*0..]-(:Taxon)<-[:BELONGS_TO]-(p:Page)
return p
11-17-2022 03:04 AM - edited 11-17-2022 03:11 AM
Try this:
match(t:Taxon{name:’insert name to find’})
match(t)<-[:HAS_PARENT*0..]-(:Taxon)<-[:BELONGS_TO]-(p:Page)
return p
11-17-2022 03:08 AM - edited 11-17-2022 03:09 AM
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)
;
11-17-2022 03:21 AM
Thanks both. I did indeed make the mistake of thinking * is *0..
All the sessions of the conference are now available online