Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return Boolean value from Neo4j cypher query

I would like to check whether a node exists using its name (instead of its ID). The Cypher query looks like :

MATCH (c:Jaguar{name:"JLR 2.5Ltr"})-[:REPRESENTED_BY]->(v) RETURN c IS NOT NULL

However, Using neo4j shell/web console, the result returned is of type String. The same fails in spring-data-neo4j with error :

Null return value from advice does not match primitive return type for: public abstract boolean xxx.yyy.repository.SomeRepository.checkIfDatasetExists(java.lang.String)

Has somebody come across any work around for this

like image 917
Thilak Maskibail Avatar asked Sep 24 '15 12:09

Thilak Maskibail


People also ask

How do I return all nodes and relationships in Neo4j?

Return all elements When you want to return all nodes, relationships and paths found in a query, you can use the * symbol.

Is Neo4j key-value?

(b) Use Neo4j, by creating a node per key-value setting the key and value as properties on the node, but there is no relationship other then having an index to group these nodes together, exposing the key of the key-value as the key on the index.

Can u relabel node in Neo4j?

You can change the nodes associated with a label but you can't change the type of a relationship. Conceptually, if you take your chicken out of one coop and put it in another you haven't altered the substance of the chicken.

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.


2 Answers

The answer provided by Supamiu will not work unfortunately, you need to hack this by returning a count expression :

MATCH (c:Jaguar{name:"JLR 2.5Ltr"})-[:REPRESENTED_BY]->(v) 
RETURN count(c) > 0 as c
like image 175
Christophe Willemsen Avatar answered Sep 21 '22 11:09

Christophe Willemsen


You should use CASE to check if your node is null or not, and return the value you need :

MATCH (c:Jaguar{name:"JLR 2.5Ltr"})-[:REPRESENTED_BY]->(v)
RETURN CASE WHEN c IS NULL THEN false ELSE true END as c

More informations can be found on Neo4j's Documentation

like image 30
Supamiu Avatar answered Sep 19 '22 11:09

Supamiu