AfterEight
AfterEight

Reputation: 63

Cypher Query: Return type of relationship of paths with variable length

users, I am trying to receive all paths (e.g., length < 3) , print the node titles, but also the relationship type.

Using:

MATCH (p0:Page {title:'New Zealand'}), (p1:Page {title:'Kiwi'}), p = (p0)-[r*..2]-(p1)
RETURN p AS path

Prints the paths, but the relationship is represented as {}

As I am using *..2,

`RETURN p,type(r)'

is not working(Type mismatch: expected Relationship but was Collection)

I can use a workaround like:

MATCH
  (p0:Page {title:'New Zealand'}),
  (p1:Page {title:'Kiwi'}),
  p = (p0)-[r]-(p3)-[r1]-(p1)
RETURN p0,type(r),p3,type(r1)

but with increasing pathlength, I would have executed a query for each pathlength.

I tried:

MATCH
  (p0:Page {title:'New Zealand'}),
  (p1:Page {title:'Kiwi'}),
  p = (p0)-[r*..2]-(p1) FOREACH(r in p | RETURN p,p0,type(r),p1)

-> expected Collection but was Path

Does anyone have hint?

Additional information JSON output(snippet) from Neo4j interface:

    {
  "results": [
    {
      "columns": [
        "p",
        "rels(p)",
        "nodes(p)"
      ],
      "data": [
        {
          "row": [
            [
              {
                "title": "New Zealand"
              },
              {},
              {
                "title": "Category:Birds of New Zealand"
              },
              {},
              {
                "title": "Kiwi"
              }
            ],
            [
              {},
              {}
            ],
            [
              {
                "title": "New Zealand"
              },
              {
                "title": "Category:Birds of New Zealand"
              },
              {
                "title": "Kiwi"
              }
            ]
          ],
          "graph": {
            "nodes": [
              {
                "id": "11120",
                "labels": [
                  "Page"
                ],
                "properties": {
                  "title": "Kiwi"
                }
              },
              {
                "id": "1942858",
                "labels": [
                  "Page"
                ],
                "properties": {
                  "title": "New Zealand"
                }
              },
              {
                "id": "11994493",
                "labels": [
                  "Category"
                ],
                "properties": {
                  "title": "Category:Birds of New Zealand"
                }
              }
            ],
            "relationships": [
              {
                "id": "1070940",
                "type": "To_Category",
                "startNode": "11120",
                "endNode": "11994493",
                "properties": {}
              },

Upvotes: 3

Views: 2595

Answers (2)

Christophe Willemsen
Christophe Willemsen

Reputation: 20185

You can simply use extract for extracting the type of relationships inside the path.

Based on the simple movie graph on http://console.neo4j.org :

MATCH p=(:Crew { name:"Neo" })-[r*3]-()
RETURN p, extract(x IN rels(p)| type(x)) AS types

Your query would then be :

MATCH (p0:Page {title:'New Zealand'}), (p1:Page {title:'Kiwi'}), 
p=(p0)-[r*..2]-(p1)
RETURN p, extract (rel in rels(p) | type(rel) ) as types

This will return you a collection of types :

[LOVE, KNOWS, KNOWS]

So you can have duplicates. If you need to de-duplicate them, use UNWIND and distinct :

MATCH (p0:Page {title:'New Zealand'}), (p1:Page {title:'Kiwi'}), 
p=(p0)-[r*..2]-(p1)
WITH p, extract (rel in rels(p) | type(rel) ) as types
UNWIND types as t
RETURN p, collect(distinct t) as types

UPDATE

I wasn't so happy with the last one, so here an easier way :

MATCH (p0:Page {title:'New Zealand'}), (p1:Page {title:'Kiwi'}), 
p=(p0)-[r*..2]-(p1)
UNWIND rels(p) as rel
RETURN p, collect(distinct type(rel)) as types

Upvotes: 3

Brian Underwood
Brian Underwood

Reputation: 10856

Does using the rels function help you?

MATCH (p0:Page {title:'New Zealand'}), (p1:Page {title:'Kiwi'}), p = (p0)-[r*..2]-(p1)
RETURN p, rels(p), nodes(p)

Upvotes: 1

Related Questions