Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
01-15-2023 01:44 PM - edited 01-15-2023 11:25 PM
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"
}
]
Solved! Go to Solution.
01-16-2023 12:09 AM
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)
01-16-2023 12:09 AM
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)
All the sessions of the conference are now available online