[英]Create if not exist Vertex and Edge in 1 Gremlin Query
我找到以下代碼來創建邊緣(如果它還不存在)。
g.V().hasLabel("V1")
.has("userId", userId).as("a")
.V().hasLabel("V1").has("userId", userId2)
.coalesce(
bothE("link").where(outV().as("a")),
addE("link").from("a")
)
它工作正常,但如果頂點和邊在 1 個查詢中不存在,我想創建它們。
我用新圖嘗試以下代碼,它只是創建新頂點但它們之間沒有關系。
g.V().hasLabel("V1")
.has("userId", userId).fold()
.coalesce(
unfold(),
addV("V1").property("userId", userId1)
).as("a")
.V().hasLabel("V1").has("userId", userId2).fold()
.coalesce(
unfold(),
addV("V1").property("userId", userId2)
)
.coalesce(
bothE("link").where(outV().as("a")),
addE("link").from("a")
)
感謝JanusGraph google小組的 Daniel Kuppitz。 我找到了解決方案。 我在這里重新發布它給任何需要它的人。
您的查詢中有兩個問題。 第一個是它不能按預期工作的原因:fold()步驟。 使用fold()將破壞路徑歷史記錄,但您可以通過在子遍歷中執行該部分來輕松解決它:
g.V().has("V1","userId", userId1).fold().
coalesce(unfold(),
addV("V1").property("userId", userId1)).as("a").
map(V().has("V1","userId", userId2).fold()).
coalesce(unfold(),
addV("V1").property("userId", userId2))
coalesce(inE("link").where(outV().as("a")),
addE("link").from("a"))
第二個問題是E和outV的結合。 您應該使用bothE/otherV
, outE/inV
或inE/outV
。
我使用了@thangdc94建議的方法(謝謝!),發現“地圖”步驟需要很長時間,這個查詢對我來說工作得更快(X20):
g.V().has("V1","userId", userId1).fold().
coalesce(unfold(),
addV("V1").property("userId", userId1)).as("a").iterate();
g.V().has("V1","userId", userId2).fold().
coalesce(unfold(),
addV("V1").property("userId", userId2)).as("b").
V().has("V1","userId", userId1).
coalesce(outE("link").where(inV().as("b")),
addE("link").to("b"))
評論:我使用了 Neptune DB
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.