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
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.
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.
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.
The MATCH clause allows you to specify the patterns Neo4j will search for in the database.
Use the type()
function.
MATCH (n)-[r]-(m) RETURN type(r);
Added distinct.
MATCH (n)-[r]-(m) RETURN distinct type(r);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With