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.

Nodes in paths of particular length for a list of nodes

In my graph model, nodes have relationships with each other and can have loops. I want to find the nodes in paths of length 2 with distinct nodes in the paths and for a list of beginning nodes:

WITH ['begin1', 'begin2', 'begin3'] as names
MATCH pathlengthtwo= (startNode:Node)-[:link*2]->(finishNode:Node)
where (startNode.name in names) AND (isEmpty ((finishNode)-[:link]->(finishNode)))//to make nodes of the path distinct
RETURN startNode,finishNode, pathlengthtwo limit 10

But Neo4j just gives some nodes (not relationships) for 'begin1' node and I think this is because the paths of the starting node'begin1' go beyond limit 10 and Neo4j even doesn't consider the rest of nodes in the list. How should I change this query to show paths for all the starting nodes in the list and their relationships (not just the nodes)

3 REPLIES 3

ameyasoft
Graph Maven

RETURN pathlengthtwo displays nodes and relationships. nodes(pathlengthtwo) gives nodes in the path, relationships(pathlengthtwo) gives the relationships

"relationships(pathlengthtwo) "does not help the query to consider the paths starting from 'begin2', 'begin3', Still they are not considered by the query.

Hi,

@ameyasoft is right. If you have issues about not seeing some paths, remove the limit. Are you sure you have paths within the other begin's. Debug it removing begin1 of the list.

Btw, the second condition doesn't give you the distinct nodes rule.

You need something like:

WHERE NONE (n IN nodes(pathlengthtwo) 
            WHERE size(filter(x IN nodes(pathlengthtwo ) 
                              WHERE n = x))> 1)

Bennu