Have you tried using filter()
or none()
? I think I correctly understood your problem but here's how I am using the above functions. (If this is off, just downwote.)
The gist: http://console.neo4j.org/?id=99xkcu
CREATE
(g:Person {name: 'Gorduin'}), (a:Person {name: 'Alvaro'}),
(pn:PhoneNumber {number: '555-512-2017'}),
(e11:Extension {extension: 11}),
(e27:Extension {extension: 27}),
(e19:Extension {extension: 19}),
(e11)-[:extension_of]->(pn)<-[:extension_of]-(e27),
(e19)-[:extension_of]->(pn),
(g)<-[:phone_number_of]-(e11),
(g)<-[:phone_number_of]-(e27),
(a)<-[:phone_number_of]-(e19),
(a)<-[:phone_number_of]-(pn);
Variable length query needs to be used because the :phone_number_of
pointer can originate from the extension (linking to the phone number) or the phone number itself. Arrow directions don't matter, you can reverse any of them and try the queries below.
(Limiting the length of the query would be the obvious solution in my
case such as
MATCH path = (p:Person)-[*1..3]-(n:PhoneNumber)
RETURN nodes(path);
but that wasn't the OP's question.)
(1) Getting every possible path from a person to a phone number (edited for readability):
MATCH path = (p:Person)-[*]-(n:PhoneNumber)
RETURN nodes(path) as every_possible_path_from_a_Person_to_a_PhoneNumber;
╒══════════════════════════════════════════════════════════════════════╕
│"every_possible_path_from_a_Person_to_a_PhoneNumber" │
╞══════════════════════════════════════════════════════════════════════╡
│[a,pn] │
├──────────────────────────────────────────────────────────────────────┤
│[g,e11,pn,e19,a,pn] │
├──────────────────────────────────────────────────────────────────────┤
│[g,e27,pn,e19,a,pn] │
├──────────────────────────────────────────────────────────────────────┤
│[g,e11,pn] │
├──────────────────────────────────────────────────────────────────────┤
│[a,pn,e27,g,e11,pn] │
├──────────────────────────────────────────────────────────────────────┤
│[a,e19,pn,e27,g,e11,pn] │
├──────────────────────────────────────────────────────────────────────┤
│[a,e19,pn] │
├──────────────────────────────────────────────────────────────────────┤
│[g,e11,pn,a,e19,pn] │
├──────────────────────────────────────────────────────────────────────┤
│[g,e27,pn,a,e19,pn] │
├──────────────────────────────────────────────────────────────────────┤
│[g,e27,pn] │
├──────────────────────────────────────────────────────────────────────┤
│[a,pn,e11,g,e27,pn] │
├──────────────────────────────────────────────────────────────────────┤
│[a,e19,pn,e11,g,e27,pn] │
└──────────────────────────────────────────────────────────────────────┘
(2) Using none()
to remove redundancy by filtering out paths that contain a specific node (a node with certain properties or labels):
MATCH path = (p:Person)-[*]-(n:PhoneNumber)
WITH nodes(path) as ns
WHERE NONE(node IN ns WHERE (exists(node.name) and node.name ='Gorduin'))
RETURN ns as path_nodes_NOT_containing_a_specific_person;
╒══════════════════════════════════════════════════════════════╕
│"path_nodes_NOT_containing_a_specific_person" │
╞══════════════════════════════════════════════════════════════╡
│[a,pn] │
├──────────────────────────────────────────────────────────────┤
│[a,e19,pn] │
└──────────────────────────────────────────────────────────────┘
(3) Using filter()
to remove a specific node from the returned paths:
MATCH path = (p:Person)-[*]-(n:PhoneNumber)
WITH nodes(path) as ns
WHERE NONE(node IN ns WHERE (exists(node.name) and node.name ='Gorduin'))
RETURN filter(node in ns WHERE NOT node:Person) as personless_nodelist;
╒══════════════════════════════════════════════════════════════╕
│"personless_nodelist" │
╞══════════════════════════════════════════════════════════════╡
│[pn] │
├──────────────────────────────────────────────────────────────┤
│[e19,pn] │
└──────────────────────────────────────────────────────────────┘