0

I am using the following neo4j cypher query on my graph:

MATCH (start:N1{id:'xyz'})
CALL apoc.path.expandConfig(start, {sequence:'N1, a>, N2, b>, N3, c>, 
N4', maxLevel:3}) YIELD path
RETURN path
ORDER BY length(path) DESC

This returns all paths like

X-a->Y-b->Z-c->**A**
X-a->Y-b->Z-c->**B**
X-a->Y-b->Z-c->**C**
X-a->Y-b->Z
X-a->Y
X
X-a->V
X-a->W

But all I want to return is paths having no duplicate shorter paths because they are already included in a longer path.

Here is the output I want:

X-a->Y-b->Z-c->**A**
X-a->Y-b->Z-c->**B**
X-a->Y-b->Z-c->**C**
X-a->V
X-a->W

Can someone please help? I am new to Neo4j

1 Answers1

2

At the moment, the cypher has no language capabilities to check whether one path is nested in another. But you can use a simple trick: convert the IDs of the path relationship into text and compare with all the others:

MATCH (start:N1{id:'xyz'})
CALL apoc.path.expandConfig(start, {sequence:'N1, a>, N2, b>, N3, c>, 
N4', maxLevel:3}) YIELD path
WITH path ORDER BY LENGTH(path) ASC
WITH COLLECT(path) AS paths
UNWIND paths AS path
WITH 
  paths, path, 
  apoc.text.join([r in relationships(path)| '' + ID(r)],'.') AS pathTR
  WHERE ALL(p IN paths WHERE 
    CASE 
      WHEN SIZE(pathTR) > 0 AND path <> p AND
           apoc.text.join([r IN relationships(p)| '' + ID(r)],'.') CONTAINS pathTR 
      THEN FALSE 
      ELSE TRUE 
    END 
  )
RETURN path
ORDER BY LENGTH(path) DESC
stdob--
  • 28,222
  • 5
  • 58
  • 73