OrientDB group by query using graph

2019-05-31 07:51发布

I need to perform an grouped aggregate on a property of vertices of a certain class, the group by field is however a vertex two steps away from my current node and I can't make it work.

My case: The vertex A contains the property I want to aggregate on and have n number of vertices with label references. The vertex I want to group on is any of those vertices (B, C or D) if that vertex has a defined by edge to vertex F.

A ----references--> B --defined by--> E
  \---references--> C --defined by--> F
   \--references--> D --defined by--> G
     ...

The query I thought would work is:

select sum(property), groupOn from (
    select property, out('references')[out('definedBy').@rid = F] as groupOn from AClass
) group by groupOn

But it doesn't work, the inner statement gives me a strange response which isn't correct (returns no vertices) and I suspect that out() isn't supported for bracket conditions (the reason for the .@rid is that the docs I found stated that only "=" are supported.

out('references')[out('definedBy') contains F] doesn't work either, that returns the out('definedBy') for the $current vertex).

Anyone with an idea how to achieve this? In my example, the result I would like is the property in one column and the @rid of the C vertex in another. Then I can happily perform my group by aggregates.

1条回答
兄弟一词,经得起流年.
2楼-- · 2019-05-31 08:23

Solved it! In OrientDB 2.1 (I'm trying rc4) there's a new clause called UNWIND (see SELECT in docs).

Using UNWIND I can do:

SELECT sum(property), groupOn from (
  SELECT property, out('references') as groupOn
  FROM AClass
  UNWIND groupOn
) WHERE groupOn.out('definedBy')=F
GROUP BY groupOn

It could potentially be a slow function depending on the number of vertices of AClass and its references, I'll report back if I find any performance issues.

查看更多
登录 后发表回答