如何使用展开的列表值指定顶点? [英] How do I use unfolded list values to specify a vertex?

查看:77
本文介绍了如何使用展开的列表值指定顶点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写Gremlin遍历,意思是将所有这些身份添加到该组",在插入列表"食谱中的例子.这是我尝试的逻辑:

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()

据我了解,这将为列表中的每个元素生成一个遍历器,并执行 addE 操作.但是,我不知道如何在遍历中表达 V().hasId(it). identity()步骤似乎应该适用,但是我不能将其用作 V hasId 的参数.

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.

我尝试使用 unfold().as("id")来构成表达式,但是我遇到的问题是 select()返回一个遍历,我不能在明显的地方使用遍历.

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.

推荐答案

您可以使用 where()进行所需的操作:

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.

我认为目前最好的方法是使用 V(identityIds).您提到了一个更复杂的情况,并且通常情况下,这个复杂情况与想要传递带有id(通常是属性)的其他数据有关,在这种情况下,我仍然会坚持使用 V(identityIds)并简单地对待这些额外的数据会产生副作用:

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]]

可能有一种更好的编写方法,但是通常的想法是,确保使用 V(ids)命中顶点查找的索引,然后处理要使用的数据作为某种副作用,您必须将顶点ID与之匹配,然后使用 Map 即可更新所需的任何属性.此博客帖子描述了针对此问题的一般方法详细插入.

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.

这篇关于如何使用展开的列表值指定顶点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆