【问题标题】:Remove all labels for Neo4j Node删除 Neo4j 节点的所有标签
【发布时间】:2014-11-18 02:33:26
【问题描述】:

以下示例取自 here 的 Neo4j 文档。

使用 Cypher,可以使用如下所示的 Cypher 语句删除单个已知标签:

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

您也可以像这样删除多个标签:

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

那么如何使用简单的 Cypher 语句从节点中删除所有标签?

【问题讨论】:

    标签: neo4j cypher


    【解决方案1】:

    您也可以使用 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
    

    【讨论】:

      【解决方案2】:

      目前还没有语法!标签通常是已知数量的东西,因此您可以根据需要将它们全部列出。但是,没有动态方法可以将它们全部删除。

      【讨论】:

      • 这是个大问题。没有办法删除所有标签,似乎也没有办法更新节点,以便它只包含您想要的特定标签。换句话说,如果一个节点有标签 l1 和 l2,并且我想更新它有标签 l1 和 l3,似乎没有办法在不显式删除 l2 的情况下将标签设置为 l1 和 l3。我们使用标签来表示类型。如果应用程序中节点的类型发生变化,我们希望能够更新节点上的标签以反映这一点。如果我错了,请纠正我。
      • 如果你知道图表中的所有标签,你应该知道,你可以做类似match (n) remove n:label1:label2:label3:labelN的事情——它很丑,但它应该可以工作。
      【解决方案3】:

      那么,两步密码方法怎么样?使用 cypher 生成一些 cypher 语句,然后在 shell 中执行您的 cypher 语句。

      您可以尝试这样的方法来生成批处理密码语句

      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) 
      + ";"
      

      输出应该是这样的......

      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;
      

      您可能希望删除所有重复项,并且根据您的数据,可能会有很多重复项。

      不完全是您正在寻找的东西,但我认为仅使用 cypher 和 neo4j shell 就能使您达到相同的最终状态。


      下面闪亮的新密码和改进密码...

      我将其编辑为仅可在浏览器中使用的内容。它认为这是一个更好的解决方案。它仍然是两个步骤,但它会生成一个可以剪切并粘贴到浏览器中的语句。

      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
      

      产生这样的输出...

      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
      

      然后可以将其剪切并粘贴到 Neo4j 浏览器中。

      【讨论】:

      • 可以在密码中添加一个distinct,这样你就不需要在之后清理骗子了:)
      • 我认为 OP 是在询问如何仅使用 Cypher 本身来完成此操作。一旦您必须使用 Neo4j shell,您几乎可以手动执行任何操作。
      • 是的,我同意他们想要一个单一的声明;它仍然是密码。我认为您的意思是只能在浏览器中运行的东西?无论哪种方式,它都让我想到了一个仅浏览器的解决方案,它仍然是两个步骤,但可以在浏览器中运行。所以我制作了上面的添加。
      猜你喜欢
      • 2012-12-24
      • 2022-08-08
      • 1970-01-01
      • 1970-01-01
      • 2021-10-13
      • 2015-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多