How do I use unfolded list values to specify a vertex?

狂风中的少年 提交于 2021-01-29 06:12:40

问题


I'm trying to write a Gremlin traversal meaning "add all of these identities to this group", patterning it after the "inject list" example in the recipes. This is the logic I'm attempting:

List identityIds = [...]

gts.inject(identityIds).unfold()
  .V() // filter statement goes here?
  .addE(GROUP_INCLUDES_IDENTITY).from(V(groupId))
  .iterate()

As I understand it, this will spawn a traverser for each element in the list and execute the addE operation. However, I can't figure out how to express V().hasId(it) in the traversal. The identity() step seems like it ought to be applicable, but I can't use it as an argument to either V or hasId.

I've tried to form an expression using unfold().as("id"), but I have the same problem that select() returns a traversal and I can't use a traversal in the obvious places.


回答1:


You can use where() to do what you want:

gremlin> g.inject(1,2).as('id').
......1>   V().as('v').
......2>   where('id',eq('v')).
......3>     by().by(id)
==>v[1]
==>v[2]

but it's not ideal because it's unlikely that this traversal will get optimized to use an index.

I think that the best approach at this time is to use V(identityIds). You allude to a more complex case and often times that complex case relates to wanting to pass additional data with the id (typically properties) in which case, I would still stick with V(identityIds) and simply treat that additional data as a side-effect:

gremlin> g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> data = [[i: 1, w: 0.99],[i: 2, w: 0.98]]
==>[i:1,w:0.99]
==>[i:2,w:0.98]
gremlin> g.withSideEffect('d',data).
......1>   V(data.collect{it.i}).as('v').
......2>   addE('knows').to(V(6)).as('e').
......3>   select('d').unfold().as('p').
......4>   where('p',eq('v')).
......5>     by('i').by(id).as('weight').
......6>   select('e').
......7>   property('weight',select('weight'))
==>e[13][1-knows->6]
==>e[14][2-knows->6]
gremlin> g.E(13,14).elementMap()
==>[id:13,label:knows,IN:[id:6,label:person],OUT:[id:1,label:person],weight:[i:1,w:0.99]]
==>[id:14,label:knows,IN:[id:6,label:person],OUT:[id:2,label:person],weight:[i:2,w:0.98]]

There might be a nicer way to write that but the general idea is that you make sure you hit an index for the vertex lookup with V(ids) and then treat the data you want to use as some sort of side-effect which you have to match the vertex id to and then with the Map in hand you can update whatever properties you want. This blog post describes the general approach to this for inserts in greater detail.



来源:https://stackoverflow.com/questions/65880973/how-do-i-use-unfolded-list-values-to-specify-a-vertex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!