简体   繁体   English

删除Neo4j节点的所有标签

[英]Remove all labels for Neo4j Node

The following examples are taken from the Neo4j documentation found here . 以下示例摘自此处的Neo4j文档。

Using Cypher, it is possible to remove a single, known label using a Cypher statement like so: 使用Cypher,可以使用Cypher语句删除单个已知标签,如下所示:

MATCH (n { name: 'Peter' })
REMOVE n:German
RETURN n

You can also remove multiple labels like so: 您还可以删除多个标签,如下所示:

MATCH (n { name: 'Peter' })
REMOVE n:German:Swedish
RETURN n

So how would one remove all labels from a node using simple Cypher statements? 那么如何使用简单的Cypher语句从节点中删除所有标签?

There's no syntax for that yet! 目前还没有语法! Labels are usually things that are known quantities, so you can list them all out if you want. 标签通常是已知数量的物品,因此您可以根据需要列出所有标签。 There's no dynamic way to remove them all, though. 但是,没有动态的方法可以全部删除它们。

so, how about a two step cypher approach? 那么,两步密码法怎么样? use cypher to generate some cypher statements and then execute your cypher statements in the shell. 使用cypher生成一些cypher语句,然后在shell中执行您的cypher语句。

You could try something like this to generate the batch cypher statements 您可以尝试类似这样的操作来生成批处理密码语句

match (n) 
return distinct "match (n" 
+ reduce( lbl_str= "", l in labels(n) | lbl_str + ":" + l) 
+ ") remove n" 
+ reduce( lbl_str= "", l in labels(n) | lbl_str + ":" + l) 
+ ";"

The output should look something like this... 输出应该看起来像这样...

match (n:Label_1:Label_2) remove n:Label_1:Label_2;
match (n:Label_1:Label_3) remove n:Label_1:Label_3;
match (n:Label_2:Label_4) remove n:Label_2:Label_4;

You would probably want to remove any duplicates and depending on your data there could be quite a few. 您可能希望删除所有重复项,并且根据您的数据可能会很多。

Not exactly what you are looking for but I think it would get you to the same end state using just cypher and the neo4j shell. 不完全是您要查找的内容,但我认为仅使用cypher和neo4j shell就能将您带到相同的最终状态。


Shiny NEW and improved cypher below... 下面有光泽的新的和改进的密码...

I edited this down to something that would work in the browser alone. 我将其编辑为仅在浏览器中可用的内容。 It hink this is a much better solution. 它认为这是一个更好的解决方案。 It is still two steps but it produces a single statement that can be cut and paste into the browser. 它仍然是两个步骤,但是它生成一个可以剪切并粘贴到浏览器中的语句。

match (n) 
with distinct labels(n) as Labels
with reduce(lbl_str="", l in Labels | lbl_str + ":" + l) as Labels
order by Labels
with collect(Labels) as Labels
with Labels, range(0,length(Labels) - 1) as idx
unwind idx as i
return "match (n" + toString(i) + Labels[i] + ")" as Statement
union
match (n) 
with distinct labels(n) as Labels
with reduce(lbl_str="", l in Labels | lbl_str + ":" + l) as Labels
order by Labels
with collect(Labels) as Labels
with Labels, range(0,length(Labels) - 1) as idx
unwind idx as i
return "remove n" + toString(i) + Labels[i] as Statement

which produces output like this... 产生这样的输出...

match (n0:Label_A)
match (n1:Label_B)
match (n2:Label_C:Label_D)
match (n3:Label_E)
remove n0:Label_A
remove n1:Label_B
remove n2:Label_C:Label_D
remove n3:Label_E

which can then be cut and paste into the Neo4j browser. 然后可以将其剪切并粘贴到Neo4j浏览器中。

You can also try this way using doIt method from apoc library: 您也可以使用apoc库中的doIt方法尝试这种方式:

match (n {name: 'Peter'})
call apoc.cypher.doIt(
  "match (o)" +
  " where ID(o) = " + ID(n) +
  " remove "+reduce(a="o",b in labels(n) | a+":"+b) + 
  " return (o);",
null)
yield value
return value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM