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.

Duplicate & empty rows returned in shortestPath query

tinqnit
Node Link

Hello, I have a question regarding 'shortestPath', I am getting duplicate nodes & null/empty nodes in the result.


CREATE
(a:mnode { v: "2413fb3709b05939f04cf2e92f7d0897fc2596f9ad0b8a9ea855c7bfebaae892"}),
(b:mnode { v: "f012767e628bb3cd86d65923ea029e6e0bc6e42a34c9721ec6ab8147a4af3603"}),
(c:mnode { v: "c415f7209e4024ad7bcc99b45e241c7e1c8954274990713bde0799bc4f275366"}),
(d:mnode { v: "e08a8492e9ff2773e73cc524d475d457fe1b276a8c172889c53b28a4f1ec0322"}),
(ab:mnode { v: "8326f021b328ee0d14d4d366a9f6e6f0be68ea60f7e5a64db0cdaa385fe6e0e7"}),
(cd:mnode { v: "1ecd272fc854e5a1a241431411aad6e3aa74e77942b9a7c54c5c732eaf9732a1"}),
(abcd:mnode { v: "5f143015d428cc6052649779f85ca66cefd45dd8115a436b54ab794bb6d8171c"}),
(a)-[:PARENT]->(ab),
(b)-[:PARENT]->(ab),
(c)-[:PARENT]->(cd),
(d)-[:PARENT]->(cd),
(ab)-[:PARENT]->(abcd),
(cd)-[:PARENT]->(abcd)

I am running the following query,


MATCH (leaf:mnode { v: "2413fb3709b05939f04cf2e92f7d0897fc2596f9ad0b8a9ea855c7bfebaae892" }),(root:mnode { v: "5f143015d428cc6052649779f85ca66cefd45dd8115a436b54ab794bb6d8171c" }), p = shortestPath((leaf)-[r:PARENT*]->(root))
RETURN p

And I get the following output.


[{"v":"2413fb3709b05939f04cf2e92f7d0897fc2596f9ad0b8a9ea855c7bfebaae892"},{},{"v":"8326f021b328ee0d14d4d366a9f6e6f0be68ea60f7e5a64db0cdaa385fe6e0e7"},{"v":"8326f021b328ee0d14d4d366a9f6e6f0be68ea60f7e5a64db0cdaa385fe6e0e7"},{},{"v":"5f143015d428cc6052649779f85ca66cefd45dd8115a436b54ab794bb6d8171c"}]

can someone help me get rid of the duplicate nodes/empty nodes please? Also, I would like to understand why this is happening in the first place. Thanks in advance.

2 REPLIES 2

tinqnit
Node Link

The below worked for me. Although I am not sure if this is the most appropriate solution yet.


MATCH p=shortestPath((start:mnode {v: "2413fb3709b05939f04cf2e92f7d0897fc2596f9ad0b8a9ea855c7bfebaae892"})-[rels:PARENT*]-(end:mnode {v: "5f143015d428cc6052649779f85ca66cefd45dd8115a436b54ab794bb6d8171c"}))
RETURN [mnode in nodes(p)]

As mentioned in the users slack, the string representation of a path includes relationship data, which is represented by the properties of a node at one end, then the properties of the relationship, then the properties of the node at the other end. For multi-hop queries you might see the properties of a node come up multiple times since it's between two relationships.

If you only need the nodes in the path, nodes(p) is enough to get the nodes list.