How to return relationship type with Neo4J's Cypher queries?

F Lekschas picture F Lekschas · Jul 18, 2015 · Viewed 16.2k times · Source

I am trying to get the relationship type of a very simple Cypher query, like the following

MATCH (n)-[r]-(m) RETURN n, r, m;

Unfortunately this return an empty object for r. This is troublesome since I can't distinguish between the different types of relationships. I can monkey patch this by adding a property like [r:KNOWS {type:'KNOWS'}] but I am wondering if there isn't a direct way to get the relationship type.

I even followed the official Neo4J tutorial (as described below), demonstrating the problem.

Graph Setup:

create (_0 {`age`:55, `happy`:"Yes!", `name`:"A"})
create (_1 {`name`:"B"})
create _0-[:`KNOWS`]->_1
create _0-[:`BLOCKS`]->_1

Query:

MATCH p=(a { name: "A" })-[r]->(b)
RETURN *

JSON RESPONSE BODY:

{
    "results": [
        {
            "columns": [
                "a",
                "b",
                "p",
                "r"
            ],
            "data": [
                {
                    "row": [
                        {
                            "name": "A",
                            "age": 55,
                            "happy": "Yes!"
                        },
                        {
                            "name": "B"
                        },
                        [
                            {
                                "name": "A",
                                "age": 55,
                                "happy": "Yes!"
                            },
                            {},
                            {
                                "name": "B"
                            }
                        ],
                        {}
                    ]
                },
                {
                    "row": [
                        {
                            "name": "A",
                            "age": 55,
                            "happy": "Yes!"
                        },
                        {
                            "name": "B"
                        },
                        [
                            {
                                "name": "A",
                                "age": 55,
                                "happy": "Yes!"
                            },
                            {},
                            {
                                "name": "B"
                            }
                        ],
                        {}
                    ]
                }
            ]
        }
    ],
    "errors": []
}

As you can see, I get an empty object for r, which makes it impossible to distinguish between the relationships.

NOTE: I am running Neo4J v.2.2.2

Answer

FrobberOfBits picture FrobberOfBits · Jul 18, 2015

Use the type() function.

MATCH (n)-[r]-(m) RETURN type(r);