neo4j - cypher union group by sum -
i boost edges user depending of rules base on graph traversal. in mysql :
select id, sum(weight) total ( select id, 10 weight user inner join userrel1 on user.id = userrel1.userid userrel1.attr1 in (1, 2) union select id, 5 weight user inner join userrel2 on user.id = userrel2.userid inner join userrel3 on user.id = userrel3.userid userrel2.attr2 = 'a' , userrel3.attr2 = 'z' union ... ) group id order total desc
also, have writed query in gremlin 3 compare performance cypher. read in post group on union not possible yet, mean cypher less powerful gremlin ? have set weight properties on edges achieve ?
thanks
while true post-union
processing still open feature request, not need use union
perform use case.
this query should equivalent of sql (ignoring incomplete part):
with [] res optional match (user1:user), (userrel1:userrel1) user1.id = userrel1.userid , userrel1.attr1 in [1, 2] res, (case when userrel1 not null collect({id: user1.id, weight: 10}) else [] end) data res + data res optional match (user2:user), (userrel2:userrel2) user2.id = userrel2.userid , userrel2.attr2 = 'a' optional match (userrel3:userrel3) user2.id = userrel3.userid , userrel3.attr2 = 'z' res, (case when userrel3 not null collect({id: user2.id, weight: 5}) else [] end) data res + data res unwind res result return result.id, sum(result.weight) weight;
i visually broke query separate blocks of cypher, make easier read.
the query keeps extending (and replacing) res
collection appropriate id
/weight
pairs, , aggregates @ end.
the block 2 optional match
clauses have been written using single optional match
, thought more performant same work piecemeal, , allow failure of 1 optional match
potentially inform cypher not bother other one. block's second where
clause relies on user2
node found first optional match
. user2
have value null
if first optional match
failed, , such null
value cause second where
clause fail (which in turn make userrel3
null
).
Comments
Post a Comment