cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Failing to write a cypher query that _should_ be simple

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.

1 ACCEPTED SOLUTION

glilienfield
Ninja
Ninja

Try this:

match(t:Taxon{name:’insert name to find’})

match(t)<-[:HAS_PARENT*0..]-(:Taxon)<-[:BELONGS_TO]-(p:Page)

return p

View solution in original post

3 REPLIES 3

glilienfield
Ninja
Ninja

Try this:

match(t:Taxon{name:’insert name to find’})

match(t)<-[:HAS_PARENT*0..]-(:Taxon)<-[:BELONGS_TO]-(p:Page)

return p

nacnudus
Node

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

 

Thanks both. I did indeed make the mistake of thinking * is *0..

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

All the sessions of the conference are now available online