27

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

6

2 Answers 2

53

Use the type() function.

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

Added distinct.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.