cancel
Showing results forΒ
Did you mean:Β

Head's Up! Site migration is underway. Expect disruption to service on Thursday, Feb. 9!

## Find all paths from node

Hello.

I have graph:

CREATE (a:Item {id: '1'}), (b:Item {id: '2'}), (c:Item {id: '3'}), (d:Item {id: '4'}), (e:Item {id: '5'})

MATCH (a:Item {id: '5'}), (b:Item {id: '4'})
CREATE (a)-[r:RELATED_TO {type: 'type1'}]->(b)

MATCH (a:Item {id: '4}), (b:Item {id: '2'})
CREATE (a)-[r:RELATED_TO {type: 'type1'}]->(b)

MATCH (a:Item {id: '4'}), (b:Item {id: '3'})
CREATE (a)-[r:RELATED_TO {type: 'type1'}]->(b)

MATCH (a:Item {id: '2'}), (b:Item {id: '1'})
CREATE (a)-[r:RELATED_TO {type: 'type1'}]->(b)

MATCH (a:Item {id: '3'}), (b:Item {id: '1'})
CREATE (a)-[r:RELATED_TO {type: 'type2'}]->(b)

I want to get all paths from given node and filter them by relationship property. So, I(thx glilienfield) write this cypher:

MATCH path = (from:Item {id: '10'})-[relations:RELATED_TO*]->(to:Item)
WHERE ALL(r IN relations WHERE r.type IN ['type1', 'type2'])
WITH {
to: to.id
path: [i in relationships(path) | {id: endNode(i).id, type: i.type}]
} as result
RETURN COLLECT(result)

This cypher returns me all possible ways from node with id 10:

[
{
"path": [
{
"id": "5",
"type": "type1"
}
],
"to": "5"
}
,
{
"path": [
{
"id": "5",
"type": "type1"
},
{
"id": "3",
"type": "type1"
}
],
"to": "3"
}
,
{
"path": [
{
"id": "5",
"type": "type1"
},
{
"id": "3",
"type": "type1"
},
{
"id": "1",
"type": "type1"
}
],
"to": "1"
}
,
{
"path": [
{
"id": "5",
"type": "type1"
},
{
"id": "2",
"type": "type1"
}
],
"to": "2"
}
,
{
"path": [
{
"id": "5",
"type": "type1"
},
{
"id": "2",
"type": "type1"
},
{
"id": "1",
"type": "type2"
}
],
"to": "1"
}
]

How can I filter this result using neo4j and return only these paths:

[
{
"path": [
{
"id": "5",
"type": "type1"
},
{
"id": "3",
"type": "type1"
},
{
"id": "1",
"type": "type1"
}
],
"to": "1"
}
,
{
"path": [
{
"id": "5",
"type": "type1"
},
{
"id": "2",
"type": "type1"
},
{
"id": "1",
"type": "type2"
}
],
"to": "1"
}
]

1 ACCEPTED SOLUTION

I solve my problem. The followig cypher return exatly what I need:

MATCH path = (from:Item {id: '10'})-[relations:RELATED_TO*]->(to:Item)
WHERE ALL(r IN relations WHERE r.type IN ['type1', 'type2'])
AND NOT (to)-[:RELATED_TO]->()
WITH {
to: to.id
path: [i in relationships(path) | {id: endNode(i).id, type: i.type}]
} as result
RETURN COLLECT(result)