Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 758
F Lekschas Avatar asked Jul 17 '15 22:07

F Lekschas


People also ask

How do I return all nodes and relationships in Neo4j?

When you want to return all nodes, relationships and paths found in a query, you can use the * symbol. This returns the two nodes, the relationship and the path used in the query.

How do you perform aggregation in Cypher?

The Cypher query offers aggregation similar to GROUP BY offered by SQL. The aggregate function can take multiple values and can calculate the aggregated values for them. In this recipe, we will learn the common aggregation techniques, with the help of examples.

WHAT IS WITH clause in Cypher?

The WITH clause allows query parts to be chained together, piping the results from one to be used as starting points or criteria in the next. It is important to note that WITH affects variables in scope. Any variables not included in the WITH clause are not carried over to the rest of the query.

What is match in Neo4j?

The MATCH clause allows you to specify the patterns Neo4j will search for in the database.


2 Answers

Use the type() function.

MATCH (n)-[r]-(m) RETURN type(r); 
like image 118
FrobberOfBits Avatar answered Sep 22 '22 19:09

FrobberOfBits


Added distinct.

MATCH (n)-[r]-(m) RETURN distinct type(r); 
like image 40
Pratheep Avatar answered Sep 20 '22 19:09

Pratheep