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.

Split path into two maps

Dragonprod
Node Link

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"
     }
  }
]

 

 

1 ACCEPTED SOLUTION

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"
}

View solution in original post

2 REPLIES 2

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"
}

Thanks you, this is exactly what I need