Search code examples
graph-databasesgremlintinkerpop3janusgraph

How to get combined vertex results in gremlin using union/project/select?


below is my example graph

addV('user').property('userId','user2').as('u2').
      addV('user').property('userId','user3').as('u3').
      addV('group').property('groupId','group1').as('g1').
      addV('group').property('groupId','group2').as('g2').
      addV('group').property('groupId','group3').as('g3').
      addV('folder').property('folderId','folder1').property('folderName','director').as('f1').
      addV('folder').property('folderId','folder2').property('folderName','asstDirector').as('f2').
      addV('folder').property('folderId','folder3').property('folderName','editor').as('f3').
      addV('file').property('fileId','file1').property('fileName','scene1').
      addE('in_folder').to('f3').
      addE('in_folder').from('f2').to('f1').
      addE('in_folder').from('f3').to('f2').
      addE('member_of').from('u1').to('g1').
      addE('member_of').from('u2').to('g2').
      addE('member_of').from('u3').to('g3').
      addE('member_of').from('g3').to('g1').
      addE('has_permission').property('prm','view').from('g1').to('f1').
      addE('has_permission').property('prm','view').from('g2').to('f2').
      addE('has_permission').property('prm','view').from('g3').to('f3').
      addE('has_permission').property('prm','view').from('u2').to('f1').iterate()

The use-case : Get both folders and files vertex as result where user3 have access to im able to get either folder or files where user have access to, but not able to combine them.

below is the query which cam close but not exactly exptected resultset.

g.V().has('user','userId','user3').emit().until(__.not(outE('member_of'))).repeat(out('member_of'))
.outE('has_permission').has('prm','view').inV().as('f').inE('in').outV().as('a').select('a','f')

which results

==>[a:v[6144475224],f:v[164175920]]
==>[a:v[5202170056],f:v[204857480]]

the expectation is to get 3 vertexes with value Map, not just node ids.

f:v[164175920]
a:v[6144475224]
a:v[5202170056]

Could some one check and let me know how i can utilize union here. tried below query

g.V().has('user','userId','user3').emit().until(__.not(outE('member_of'))).repeat(out('member_of'))
.outE('has_permission').has('prm','view').inV().inE('in').outV().map(union(valueMap())

this gives the file information only not the folder values.


Solution

  • You got it right in your comment query, only with few spelling mistakes which caused the error:

    g.V().has('user','userId','user3').emit().repeat(out('member_of'))
    .outE('has_permission').has('prm','view').inV().dedup()
    .union(identity(),__.repeat(__.in('in_folder')).emit()).dedup()
    

    This can be optimized a bit by moving the emit before the repeat and dropping the union:

    g.V().has('user','userId','user3').emit().repeat(out('member_of'))
    .outE('has_permission').has('prm','view').inV().dedup()
    .emit().repeat(__.in('in_folder')).dedup()
    

    Note that I used 'in_folder' instead on 'in' to match your sample data.