neo4j - how to set label with property value

你。 提交于 2019-12-08 17:47:11

问题


I have nodes without label but a property NodeType

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

Thanks!


回答1:


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

You'll have to do it in your application by fetching all nodes that you want to add a label on it and sending a Cypher Query 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;
}



回答2:


You can't use a variable but you can still do it in a cypher query (or at least a few of them) rather than a script. If you only have a handful of different labels this probably works well but not that scalable for many labels.

For Example

  1. MATCH (n) WHERE length(labels(n)) = 0 and n.type = 'XX' SET n:XX;
  2. MATCH (n) WHERE length(labels(n)) = 0 and n.type = 'XY' SET n:XY;



回答3:


Using Cypher and APOC to move a property value to a label

// create a node with property studio
   create (n:Movies {name: 'A Few Good Men', studio: 'Warner Brothers'})
// move the 'studio' property to a label and remove it as a property
   MATCH (n:Movies)
   call apoc.create.addLabels([ id(n) ], [ n.studio ]) yield node
   with node
   remove node.studio
   return node

From




回答4:


A hack solution would be to have a Cypher query that looks something like:

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

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

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

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



来源:https://stackoverflow.com/questions/26536573/neo4j-how-to-set-label-with-property-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!