gpt4 book ai didi

Gremlin - 顶点的 Upsert 不适用于合并

转载 作者:行者123 更新时间:2023-12-04 10:06:52 24 4
gpt4 key购买 nike

我是小鬼的新手,有一个非常简单的案例,我需要检查一下:

  • 如果顶点存在
  • 更新属性
  • 别的
  • 添加具有属性的顶点

  • 我为此使用 Java API。

    我的代码:
    g.V().hasLabel("Entity").has("identifier", "123").fold()
    .coalesce(
    __.unfold(),
    __.addV("Entity")
    .property("identifier", "123")
    .property("value", "A")
    .property("action", "add")
    )
    .property("value", "A")
    .property("action", "update")
    .iterate();

    我知道这是一个非常简单的案例,我引用了 [ CosmosDB Graph : "upsert" query pattern 中给出的例子。

    但它不起作用。如果顶点不存在,则会添加属性,但也会更新属性。

    最佳答案

    当您编写 Gremlin 时,您需要从流的角度进行思考。 V()生成图中所有顶点的流。设想该流中的每个项目点击 hasLabel()has()过滤器配对,直到它们达到 fold() 的减少步骤产生 List具有与过滤条件匹配的顶点,或者如果没有,则简单地生成一个空列表,该列表成为流中的新对象。

    从那里 coalesce()产生一种 if-then 类型的情况,其中提供给它的第一个返回值的子流最终被耗尽,其余的子流被忽略。因此,如果 unfold()这需要 List顶点由 fold() 生成包含一个顶点,然后将其提供给流并且该顶点存在且 coalesce()因此生成现有顶点并继续执行 property("value", "A").property("action", "update") 的最后两个步骤.如果List为空然后 unfold()流不产生任何对象并转到下一个以 addV() 开头的子流. addV()流显然会产生一个新的 Vertex具有指定的属性,然后 coalesce()因为它的父节点会在流中生成新添加的顶点,它也会继续执行最后两个步骤,从而覆盖您提供给 addV() 的属性值。 .

    如果你想有两个单独的路径,那么你可以做这样的事情:

    g.V().hasLabel("Entity").has("identifier", "123").fold()
    .coalesce((Traversal)
    __.unfold()
    .property("value", "A")
    .property("action", "update"),
    __.addV("Entity")
    .property("identifier", "123")
    .property("value", "A")
    .property("action", "add")
    )
    .iterate();

    关于Gremlin - 顶点的 Upsert 不适用于合并,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61541672/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com