在1个查询gremlin中创建不存在的顶点和边缘 [英] Create if not exist Vertex and Edge in 1 query gremlin
问题描述
我发现以下代码可以创建edge(如果尚不存在).
I find the following code to create edge if it has not existed yet.
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个查询中不存在顶点和边,则我想创建它们.
It works fine but I want to create both vertices and edge if they are not existed in 1 query.
我尝试用下面的代码创建新的图形,它只是创建新的顶点,但它们之间没有关系.
I try the following code with new graph, it just create new vertices but no relation between them.
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")
)
推荐答案
Thanks to Daniel Kuppitz in JanusGraph google group. I found out the solution. I re-post it here for anyone who need it.
查询中有两个问题.第一个是它无法按预期运行的原因:fold()步骤.使用fold()会破坏路径历史记录,但是您可以通过在子遍历中进行该操作来轻松解决该问题:
There are two issues in your query. The first one is the reason why it doesn't work as expected: the fold() step. Using fold() will destroy the path history, but you can easily work around it, by doing that part in a child traversal:
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
.
The second issue is the combination of bothE and outV. You should rather use bothE/otherV
, outE/inV
or inE/outV
.
这篇关于在1个查询gremlin中创建不存在的顶点和边缘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!