简体   繁体   English

Gremlin - 在单个查询中从多个顶点获取 select 属性的最佳方法

[英]Gremlin - Best way to select properties from multiple vertices in single query

I have two related vertices with labels 'DeviceFamily' and 'Device'.我有两个相关的顶点,标签为“DeviceFamily”和“Device”。 They are related as shown below:它们的关系如下图所示:

(DeviceFamily)-[:RELATION]->(Device)

The DeviceFamily vertex has a few properties say p,q,r and Device vertex has properties say x,y,z. DeviceFamily 顶点有一些属性,比如 p,q,r,而 Device 顶点有一些属性,比如 x,y,z。

Given a Device id, if I require to find out x,y,z properties of the corresponding Device vertex and p,q,r properties of the related DeviceFamily, what query should I execute?给定一个设备 id,如果我需要找出相应设备顶点的 x,y,z 属性和相关设备族的 p,q,r 属性,我应该执行什么查询?

One solution I found is:我发现的一种解决方案是:

g.V('<id>').hasLabel('Device').as('d1', 'd2', 'd3').inE('RELATION').outV().as('f1', 'f2', 'f3').select('d1', 'd2', 'd3', 'f1', 'f2', 'f3').by('x', 'y', 'z', 'p', 'q', 'r');

This query works but I am not sure if this is the best way to do it.此查询有效,但我不确定这是否是最好的方法。 Please let me know if there is a better way.请让我知道是否有更好的方法。

Thanks.谢谢。

I think that you should use some form of project() :我认为你应该使用某种形式的project()

g.V('<id>').
  project('device','family').
    by(__.valueMap('x','y','z'))
    by(__.in('RELATION').valueMap('p','q','r'))

Note that I omitted the hasLabel('Device') as it's redundant as you know the unique identifier for the vertex.请注意,我省略了hasLabel('Device')因为它是多余的,因为您知道顶点的唯一标识符。 Further note that while I used valueMap() in the by() modulators of project() you could easily supply any Gremlin you want there to shape your data into those two keys (I just chose valueMap() as it's convenient to write).进一步注意,当我在project() () 的by()调制器中使用valueMap()时,你可以轻松地提供任何你想要的 Gremlin 来将你的数据塑造成这两个键(我只是选择了valueMap() ,因为它方便编写)。

If you'd like something more matched to your output you could still use project() in this direct fashion (i've been assuming there is only one "RELATION" edge - hope that is the case):如果您想要与您的 output 更匹配的东西,您仍然可以以这种直接方式使用project() (我一直假设只有一个“关系”边缘 - 希望是这样):

g.V('<id>').
  project('x','y','z','p','q','r').
    by('x').
    by('y').
    by('z').
    by(in('RELATION').values('p')).
    by(in('RELATION').values('q')).
    by(in('RELATION').values('r'))

I don't think most graph databases will optimize the final three by() modulators to a single traverse over the "RELATION" edge.我认为大多数图形数据库不会将最后三个by()调制器优化为在“RELATION”边缘上的单次遍历。 I'd therefore suggest a slight adjustment - project() the edge instead:因此,我建议稍作调整 - project()改为边缘:

g.V('<id>').
  inE('RELATION').
  project('x','y','z','p','q','r').
    by(inV().values('x')).
    by(inV().values('y')).
    by(inV().values('z')).
    by(outV().values('p')).
    by(outV().values('q')).
    by(outV().values('r'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM