2

I want to get a ResultSet of Objects that consist of a user and a list of all users that are not yet related to this user. The result should look like this:

[[user: [USEROBJECT], usersThatAreNotFriends: [[USEROBJECT]...]]...]

I am using the Cosmos DB Gremlin Endpoint and struggle with filtering/combining the users that are already related and all users.

My idea was:

g.V().hasLabel('user').as('user').flatMap(g.V().hasLabel('user').where(__.eq(select('user').out('isFriend')).fold()).as('usersThatAreNotFriends').select('user', 'usersThatAreNotFriends')

To set up my example use:

g.addV('user').property('id','user_1').property('partition_key','1')
g.addV('user').property('id','user_2').property('partition_key','2')
g.addV('user').property('id','user_3').property('partition_key','3')
g.addV('user').property('id','user_4').property('partition_key','4')
g.V('user_1').addE('has_relation').to(g.V('user_2'))
g.V('user_2').addE('has_relation').to(g.V('user_1'))
g.V('user_2').addE('has_relation').to(g.V('user_4'))
g.V('user_4').addE('has_relation').to(g.V('user_2'))

The expected result should be represented in simple way:

[user_1: [user_3, user_4], user_2:[user_3], 
user_3:[user_1, user_2, user_3], user_4:[user_1, user_3]]

1 Answers1

3

I modified your query to add the IDs as real IDs

g.addV('user').property(id,'user_1').property('partition_key','1')
g.addV('user').property(id,'user_2').property('partition_key','2')
g.addV('user').property(id,'user_3').property('partition_key','3')
g.addV('user').property(id,'user_4').property('partition_key','4')
g.V('user_1').addE('has_relation').to(g.V('user_2'))
g.V('user_2').addE('has_relation').to(g.V('user_1'))
g.V('user_2').addE('has_relation').to(g.V('user_4'))
g.V('user_4').addE('has_relation').to(g.V('user_2'))   

In your example sometimes you showed the same person in the result so I am suggesting two different queries. The first includes the person as not being friends with themselves.

gremlin> g.V().as('p').
......1>    project('person','not-friends').
......2>      by().
......3>      by(V().where(__.not(__.in('has_relation').as('p'))).fold())
==>[person:v[user_3],not-friends:[v[user_3],v[user_2],v[user_1],v[user_4]]]
==>[person:v[user_2],not-friends:[v[user_3],v[user_2]]]
==>[person:v[user_1],not-friends:[v[user_3],v[user_1],v[user_4]]]
==>[person:v[user_4],not-friends:[v[user_3],v[user_1],v[user_4]]] 

If you want to avoid having the person appear in the results as not friends with themselves you can do this:

gremlin>  g.V().as('p').
......1>    project('person','not-friends').
......2>      by().
......3>     by(V().where(__.not(__.in('has_relation').as('p')).where(neq('p'))).fold())
==>[person:v[user_3],not-friends:[v[user_2],v[user_1],v[user_4]]]
==>[person:v[user_2],not-friends:[v[user_3]]]
==>[person:v[user_1],not-friends:[v[user_3],v[user_4]]]
==>[person:v[user_4],not-friends:[v[user_3],v[user_1]]]

As a sidenote to find who people are friends you can use a simple group().by() approach.

gremlin> g.V().aggregate('all').group().by().by(out().fold()).unfold()
==>v[user_3]=[]
==>v[user_2]=[v[user_1], v[user_4]]
==>v[user_1]=[v[user_2]]
==>v[user_4]=[v[user_2]]    
Kelvin Lawrence
  • 14,674
  • 2
  • 16
  • 38
  • The Problem with this is, that you cannot use simple symbol V in Azure Cosmos DB: ExceptionType : GraphCompileException ExceptionMessage : Gremlin Query Compilation Error: Unable to resolve symbol 'V' in the current context. @ line 1, column 56. 1 Error(s) Source : Microsoft.Azure.Cosmos.Gremlin.Core GremlinRequestId : a69f7871-2ae5-4645-91f0-0cb7af165601 Context : graphcompute Scope : graphparse-translate-validatesymbolresolution GraphInterOpStatusCode : QuerySyntaxError HResult : 0x80131500 Type ':help' or ':h' for help. – Manuel Hummler Aug 17 '20 at 14:18
  • OK, use of a mid traversal `V()` is fairly common so it seemed a natural way to solve this. I added a way to ask the inverse question that just uses a group. Perhaps use that and in your application do the rest. As time allows I will try and add an alternative form. – Kelvin Lawrence Aug 17 '20 at 14:59
  • Thank you very much for your fast answer. Hopefully Cosmos DB soon has better support for the whole Gremlin API... – Manuel Hummler Aug 18 '20 at 09:05