I would like to share gremlin querying using the .Net Neo4jClient.
Consider the following graph
The object is to produce a table of results that shows
- ReferralDate (ReferralDecisionSection Node)
- ReferralId (Referral Node)
- FamilyName (User Node)
- GivenName (User Node)
- The trick is we want to get all referrals but we also what referrals that do not have a who section, so the ReferralDate will be NULL. We also want to get referrals that are indirectly linked to a program (via a decision) but we also want the ones that are not indirectly linked to a program
|13 Jan 2012||1||Derbynew||Romiko|
So, what we doing is essentially left/right joins on ReferralNode, ReferralDecisionNode and Program.
Lets see how we can do this in .Net Neo4jClient
return graphClient .RootNode .CopySplitV<Referral>(( new IdentityPipe() .Out(Hosts.TypeKey, a => a.Key == userIdentifier.AgencyKey) .In(UserBelongsTo.TypeKey, u => u.Username == userIdentifier.Username) .Out(UserLinkedToProgram.TypeKey, p => p.Name == "Foundation") .In(HasSuggestedProgram.TypeKey) .In(ReferralHasDecisionsSection.TypeKey, r => r.Completed == false) .AggregateV("ReferralWithProgramFoundation"), new IdentityPipe() .Out(Hosts.TypeKey, a => a.Key == userIdentifier.AgencyKey) .In(ReferralBelongsTo.TypeKey, r => r.Completed == false) ) .FairMerge() .ExceptV("ReferralWithProgramFoundation") .GremlinDistinct() .Out(ReferralHasWhoSection.TypeKey) .As("ReferralDate") .In(ReferralHasWhoSection.TypeKey) .As("ReferralId") .Out(CreatedBy.TypeKey, u => u.Username == userIdentifier.Username) .As("UserGivenName") .As("UserFamilyName") .Table( who => who.ReferralDate, referral => referral.UniqueId, user => user.FamilyName, user => user.GivenName );
Notice the following
- CopySplit uses a concept of an identity pipe as a continuation of the previous output
- CopySplit will execute the two queries in parallel
- We are getting all the referrals in the system and then we are getting all the referrals in the system that have a program “Foundation”
- We then store the referrals that have a program (“Foundation”) in a aggregate (variable)
- We merge the parallel query results together with a FaireMerge
- We exclude referrals that in a a Program called “Foundation” with an Except
- We then deduplicate results with GremlinDistinct
- We then use AS to mark areas we need for table projections
Note: Using the AS clause within a CopySplit pipe in conjunction with table projections will produce undesired results, I am not sure if Gremlin supports such operations, if you know, please contact me.
Visit Marko Rodriguez for in depth discussions on Gremlin.