Neo4j - how to set a label with a property value

I have nodes without a label, but the NodeType property

Is there a way to set the label of these nodes with the value of the NodeType property?

Thanks!

+6
source share
3 answers

No, there is currently no way to define a label with a variable.

You will need to do this in your application, removing all the nodes that you want to add to it, and send a Cypher request to add this label.

A quick example in PHP:

$nodes = $client->sendCypherQuery('MATCH (n) WHERE n.nodeType = "MyType" RETURN n'); foreach ($nodes as $node) { $label = $node->getProperty('nodeType'); $id = $node->getId(); $client->sendCypherQuery('MATCH (n) WHERE id(n) = '.$id.' SET n :'.$label; } 
+4
source

You cannot use a variable, but you can still do it in a cypher request (or at least a few of them), and not in a script. If you have only a few different tags, this probably works well, but is not so scalable for many tags.

Example

  • MATCH (n) WHERE length (labels (n)) = 0 and n.type = 'XX' SET n: XX;
  • MATCH (n) Length WHERE (labels (n)) = 0 and n.type = 'XY' SET n: XY;
+3
source

The hack solution should be to have a Cypher request that looks something like this:

 start n=node({nodeId}) set n :LABEL with n return labels(n) 

And run some text manipulation on this request so that you insert a label. Here is a java example:

 String setNodeLabelQuery = getQueryString(); setNodeLabelQuery = setNodeLabelQuery.replaceFirst("LABEL", "LABEL_B); 

Where getQueryString() is a method that returns a query string.

0
source

Source: https://habr.com/ru/post/977154/


All Articles