OrientDB group by query using graph

ε祈祈猫儿з 提交于 2019-12-10 11:40:29

问题


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:


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.



来源:https://stackoverflow.com/questions/31006259/orientdb-group-by-query-using-graph

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