This may be a newbie question but i couldn't find an answer.
I have a query that fetches a graph of nodes but the result duplicates the nodes per row with unique IDs each time. I'm wondering how if I can write cypher in a way to dedupe this output?
MATCH (n)-[r]-(n2)
RETURN n as source, r as relation, n2 as target
But some nodes are linked to more than one other. A--r--B
and A-r-C
In the neo4j visualizer "table" view I see a row for each relation, which includes the start node each time even if it's the same named node:
Neo4Js own visualizer will dedupe these nodes:
However the data I'm getting back makes it hard to dedupe them since they all have different IDs, and the relations refer to the different node IDs for each row. I would have to write my own code to dedupe them based on looking up the nodes, finding labels, modifying the relations, or something similar.
Is there a way to write a better cypher query that will just reference the same node ID each time?
I'm using the JS driver so I have to manually parse stuff myself. In the end I have something like this, for each row and then dedupe the final nodes afterwards based on .name
field
export function parseRelation(row: any) {
const source = row.get("source")
const relation = row.get("relation")
const target = row.get("target")
clog.log("parseRelation", { source, relation, target })
const clump = {
nodes: [
{
id: source.properties.name,
},
{
id: target.properties.name,
},
],
links: [
{
source: source.properties.name,
target: target.properties.name,
},
],
}
return clump
}