你在正确的轨道上。查看vertex steps documentation。
标记边,然后从边遍历到顶点检查,然后跳回边更新属性。
g.V(vertex.id()).
outE("votes_for").has("type", "eat").as("e").
inV().has("name", "pizza").
select("e").property("weight", 0.99d).
iterate()
完整的 Gremlin 控制台会话:
gremlin> Titan.version()
==>1.0.0
gremlin> Gremlin.version()
==>3.0.1-incubating
gremlin> graph = TitanFactory.open('inmemory'); g = graph.traversal()
==>graphtraversalsource[standardtitangraph[inmemory:[127.0.0.1]], standard]
gremlin> vertex = graph.addVertex(T.label, 'user', 'given_name', 'Joey', 'family_name', 'Tribbiani')
==>v[4200]
gremlin> pizza = graph.addVertex(T.label, 'meal', 'name', 'pizza')
==>v[4104]
gremlin> votes = vertex.addEdge('votes_for', pizza, 'type', 'eat', 'weight', 0.8d)
==>e[1zh-38o-4r9-360][4200-votes_for->4104]
gremlin> g.E(votes).valueMap(true)
==>[label:votes_for, weight:0.8, id:2rx-38o-4r9-360, type:eat]
gremlin> g.V(vertex.id()).outE('votes_for').has('type','eat').as('e').inV().has('name','pizza').select('e').property('weight', 0.99d).iterate(); g.E(votes).valueMap(true)
==>[label:votes_for, weight:0.99, id:2rx-38o-4r9-360, type:eat]
明确指定 Titan 模式会有所帮助吗?
如果您想从 Joey 节点开始而不引用 vertex 或其 id,这将是 Titan composite index 的一个很好的用例。遍历将开始于:
g.V().has("given_name", "Joey")
有什么我不知道的辅助/实用方法吗?
除了TinkerPop reference documentation,还有几个教程可以通读:
- Getting Started
- The Gremlin Console
- Recipes
使用多个 vote_for 标签而不是像 vote_for_eat 这样的标签 + 类型属性更有意义吗?
取决于您的图形模型或查询模式是什么,但更细化的标签(如 vote_for_eat)可以正常工作。您可以在遍历步骤中传递多个边缘标签:
g.V(vertex.id()).outE('vote_for_eat', 'vote_for_play', 'vote_for_sleep')
更新
两个节点之间最多只能存在一条同类型边
您可以使用 Titan 模式来帮助解决这个问题,特别是定义一个 edge label with multiplicity ONE2ONE。如果您在Joey 和pizza 之间创建多个votes_for_eat,则会引发异常。