How to get all nodes that are not duplicated in paths

I am new to Neo4j, have consulted documents but still confused. Could you help me?

Imagin that there are words, I link them by DERIVE relationship if possible.

w0 = etyma + suffix1 = etyma + suffix1
w1 = w0 + suffix2 = etyma + suffix1 + suffix2
w2 = w1 + suffix3 = etyma + suffix1 + suffix2 + suffix3
w3 = w2 + suffix4 = etyma + suffix1 + suffix2 + suffix3 + suffix4
w4 = prefix1 + w3 = prefix1 + etyma + suffix1 + suffix2 + suffix3 + suffix4
w5 = prefix2 + w4 = prefix2 + prefix1 + etyma + suffix1 + suffix2 + suffix3 + suffix4

CREATE (w0:Word{value:'w0'}) -[:DERIVE{value:'suffix2'}]-> (w1:Word{value:'w1'}) -[:DERIVE{value:'suffix3'}]-> (w2:Word{value:'w2'}) -[:DERIVE{value:'suffix4'}]-> (w3:Word{value:'w3'}) -[:DERIVE{value:'prefix1'}]-> (w4:Word{value:'w4'}) -[:DERIVE{value:'prefix2'}]-> (w5:Word{value:'w5'})

sometimes w6,w7,w8 exist.
w6 = prefix1 + w0 = prefix1 + etyma + suffix1
w7 = w6 + suffix2 = prefix1 + w1 = prefix1 + etyma + suffix1 + suffix2
w8 = w7 + suffix3 = prefix1 + etyma + suffix1 + suffix2 + suffix3
w4 = w8 + suffix4 = prefix1 + etyma + suffix1 + suffix2 + suffix3 + suffix4

MATCH (w0:Word{value:'w0'}) CREATE (w0) -[:DERIVE{value:'prefix1'}]-> (w6:Word{value:'w6'})
MATCH (w6:Word{value:'w6'}) CREATE (w6) -[:DERIVE{value:'suffix2'}]-> (w7:Word{value:'w7'})
MATCH (w7:Word{value:'w7'}) CREATE (w7) -[:DERIVE{value:'suffix3'}]-> (w8:Word{value:'w8'})
MATCH (w1:Word{value:'w1'}) MATCH (w7:Word{value:'w7'}) CREATE (w1) -[:DERIVE{value:'prefix1'}]-> (w7)
MATCH (w4:Word{value:'w4'}) MATCH (w8:Word{value:'w8'}) CREATE (w8) -[:DERIVE{value:'suffix4'}]-> (w4)

I want to find all derivatives by a word such as w0, then return w1,w2,w3,w4,w5,w6,w7,w8
get result below when I input MATCH p = (:Word{value:'w0'}) -[:DERIVE*1..]-> (:Word) RETURN nodes(p)

result

so how to program by cypher or with Spring Data Neo4j 7+ to get all nodes without duplicatiion

Sorry, I don't understand your requirement. Can you provide more detail or examples?

In regards to your title, do you mean duplicate nodes that occurred because multiple paths traversed through the same node? I think a simple way to accomplish this is to get all the nodes along a path (this can be done with 'nodes(path)'), then pass the list of nodes to the apoc.coll.toSet() function, which will return a list of unique nodes.

Thanks for your reply. I have clarified the question.
When I input MATCH p = (:Word{value:'w0'}) -[:DERIVE*1..]-> (:Word) RETURN nodes(p) then get all the paths that have duplicated nodes.
How can I merge them then get nodes without duplication. Please give me your code,thanks.

If you want to get the distinct list of nodes from all the paths, then try this.

MATCH p = (:Word{value:'w0'}) -[:DERIVE*1..]-> (:Word) 
UNWIND nodes(p) as node
RETURN distinct node

Thank you very much !

1 Like