Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
01-13-2023 04:37 AM - edited 01-13-2023 05:22 AM
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 one node to another and filter it by relationship property. So, I 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'])
AND to.id IN ['1']
UNWIND relationships(path) as r
WITH {id: endNode(r).id, type: r.type} AS result
RETURN COLLECT(result)
How can I split my result into to objects and get smth like this:
[
{
to: "1",
path: [
{
id: "5",
type: "type1"
},
{
id: "3",
type: "type1"
},
{
id: "1",
type: "type1"
}
},
{
to: "1",
path: [
{
id: "5",
type: "type1"
},
{
id: "3",
type: "type1"
},
{
id: "2",
type: "type2"
}
}
]
Solved! Go to Solution.
01-13-2023 06:08 AM
try this:
MATCH path = (from:Item {id: '5'})-[relations:RELATED_TO*]->(to:Item)
WHERE ALL(r IN relations WHERE r.type IN ['type1', 'type2'])
AND to.id IN ['1']
WITH {
to: to.id,
path: [i in relationships(path) | {id: endNode(i).id, type: i.type}]
} as result
RETURN COLLECT(result)
{
"path": [
{
"id": "4",
"type": "type1"
},
{
"id": "3",
"type": "type1"
},
{
"id": "1",
"type": "type2"
}
],
"to": "1"
},{
"path": [
{
"id": "4",
"type": "type1"
},
{
"id": "2",
"type": "type1"
},
{
"id": "1",
"type": "type1"
}
],
"to": "1"
}
01-13-2023 06:08 AM
try this:
MATCH path = (from:Item {id: '5'})-[relations:RELATED_TO*]->(to:Item)
WHERE ALL(r IN relations WHERE r.type IN ['type1', 'type2'])
AND to.id IN ['1']
WITH {
to: to.id,
path: [i in relationships(path) | {id: endNode(i).id, type: i.type}]
} as result
RETURN COLLECT(result)
{
"path": [
{
"id": "4",
"type": "type1"
},
{
"id": "3",
"type": "type1"
},
{
"id": "1",
"type": "type2"
}
],
"to": "1"
},{
"path": [
{
"id": "4",
"type": "type1"
},
{
"id": "2",
"type": "type1"
},
{
"id": "1",
"type": "type1"
}
],
"to": "1"
}
01-13-2023 12:24 PM
Thanks you, this is exactly what I need
All the sessions of the conference are now available online