Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check the type of a node in cypher?

Tags:

neo4j

cypher

I have a graph with three node types: NodeX, NodeY, and NodeZ

I have the this cypher query:

MATCH (x:NodeX)-[*]->(d)  
WHERE x.Name = 'pqr'  
RETURN x,d;

Here (d) may could be either NodeY or NodeZ. I'm looking to handle different nodetypes separately. Something like:

MATCH (x:NodeX)-[*]->(d)  
WHERE x.Name = 'pqr'  
WITH d
CASE WHEN typeof(d)=NodeY THEN {MATCH (y:NodeY)-[*]-(z:NodeZ)}
     WHEN typeof(d)=NodeZ THEN {MATCH (z:NodeZ)-[*]-(y:NodeY)}
RETURN y,z

y and z correspond to d. Is this possible to do so?

like image 948
IronMan Avatar asked Jun 29 '17 21:06

IronMan


People also ask

How can I see all nodes in neo4j?

neo4j Share on : The Cypher Query to fetch all nodes of a neo4j graph database. In the neo4j graph database, we use Cypher queries to add and get data. To retrieve all nodes or data points in the neo4j graph database we can use the above Cypher query which will show you all nodes in the graph database.

What is the syntax for getting all the nodes under specific label in neo4j?

If you want to get the labels of a specify node, then use labels(node) ; If you only want to get all node labels in neo4j, then use this function instead: call db. labels; , never ever use this query: MATCH n RETURN DISTINCT LABELS(n) . It will do a full table scan, which is very very slow..

What are properties in neo4j?

Properties Properties are key-value pairs that are used for storing data on nodes and relationships. The value part of a property: Can hold different data types, such as number , string , or boolean .

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.


1 Answers

Nodes have "labels", not "types" (the latter term only applies to relationships).

To get the labels of a node, you can use the LABELS() function. So, to test if node n has the label Foo, you can do something like this in Cypher:

CASE WHEN 'Foo' IN LABELS(n) THEN ... 

However, the CASE clause cannot contain a MATCH clause.

[EDITED]

In your specific case, something like this query (which assumes that, as you said, the only labels possible for d are NodeY and NodeZ) may work:

MATCH (x:NodeX)-[*]->(d)
WHERE x.Name = 'pqr'  
WITH d, CASE WHEN 'NodeY' IN LABELS(d) THEN 'NodeZ' ELSE 'NodeY' END AS otherLabel
MATCH (d)-[*]-(other)
WHERE otherLabel IN LABELS(other)
RETURN d, other;
like image 176
cybersam Avatar answered Oct 01 '22 18:10

cybersam