24
votes

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

2
It's just TYPE(r) where r is the relationship's identifier. Check it out here: neo4j.com/docs/stable/…Nicole White
Great! That was easy. I am wondering why I haven't found that page.F Lekschas
It's not exactly featured. :) In the future, keep the Cypher refcard handy as it will show you things like that. neo4j.com/docs/stable/cypher-refcardNicole White
I've been writing cypher for a few years now, and I always have the refcard and the cypher cheat sheet close by.FrobberOfBits
@FrobberOfBits Thanks, I guess you mean this PDF assets.neo4j.org/download/Neo4j_CheatSheet_v3.pdfF Lekschas

2 Answers

44
votes

Use the type() function.

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

Added distinct.

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