0

I have a gremlin query that I would like to return an array of user ids. Currently it's returning an array of arrays. One array for each projection.

Is there a way to transform this array of arrays into a single array of user ids in the query or is this something I need to handle at the application level?

Any help is very much appreciated.

g.V('testUser').fold()
.coalesce(
    unfold().project('bi_directional_connection', 'single_directional_connection')
            .by(
                bothE('bi_directional_connection')
                    .has('status', 'ACCEPTED')
                    .otherV()
                    .has('active', true)
                    .values('user_id')
                    .fold()
                    .dedup()
                    .limit(100)
            )
            .by(
                outE('single_directional_connection')
                    .otherV()
                    .values('user_id')
                    .fold()
                    .dedup()
                    .limit(100)

            ).select(values),
        
    project('err').by(constant("user does not exist"))
)

EDIT: Here is my sample data

    // Set up test data
g.addV('joshTest1')
    .property(T.id, 'joshTest1')
    .property(single, 'user_id', 'joshTest1')
    .property(single, 'role', 'test-user')
    .property(single, 'active', true)
.addV('joshTest2')
    .property(T.id, 'joshTest2')
    .property(single, 'user_id', 'joshTest2')
    .property(single, 'role', 'test-user')
    .property(single, 'active', true)
.addV('joshTest3')
    .property(T.id, 'joshTest3')
    .property(single, 'user_id', 'joshTest3')
    .property(single, 'role', 'test-user')
    .property(single, 'active', true)
.addV('joshTest4')
    .property(T.id, 'joshTest4')
    .property(single, 'user_id', 'joshTest4')
    .property(single, 'role', 'test-user')
    .property(single, 'active', true)
.addE('single_directional_connection')
    .from(V('joshTest2'))
    .to(V('joshTest1'))
    .property('status', 'ACCEPTED')
.addE('single_directional_connection')
    .from(V('joshTest3'))
    .to(V('joshTest1'))
    .property('status', 'ACCEPTED')
.addE('bi_directional_connection')
    .from(V('joshTest2'))
    .to(V('joshTest3'))
    .property('status', 'ACCEPTED')
.addE('bi_directional_connection')
    .from(V('joshTest3'))
    .to(V('joshTest2'))
    .property('status', 'ACCEPTED')
.addE('bi_directional_connection')
    .from(V('joshTest2'))
    .to(V('joshTest4'))
    .property('status', 'ACCEPTED')
.addE('bi_directional_connection')
    .from(V('joshTest4'))
    .to(V('joshTest2'))
    .property('status', 'ACCEPTED')

Here is the response I get from running the query against the sample data. I'm doing this in a AWS Jupyter notebook.

[['joshTest3', 'joshTest4', 'joshTest3', 'joshTest4'], ['joshTest1']]

Notice I'm also getting duplicates which I do not want.

What I would like to get is this:

['joshTest3', 'joshTest4', 'joshTest1']
Josh Chappelle
  • 1,558
  • 2
  • 15
  • 37
  • It would be helpful. if you could add to the question an example of the output you are currently getting and what. you would rather get. Also if you could provide some sample data that would help test possible answers. – Kelvin Lawrence Jun 22 '21 at 20:05
  • Thanks so much for the quick reply. I updated my description with the info you requested. Let me know if there's anything else that I can provide to help. – Josh Chappelle Jun 22 '21 at 21:01
  • Thanks I'll take a look. The duplicates I think you will find are because you have `dedup` after `fold`. It needs to go before. – Kelvin Lawrence Jun 22 '21 at 21:35

1 Answers1

0

Here is an answer to get you going. I moved the dedup before the fold and massaged the results at the end. I am going to study this query some more and will update this answer if I come up with a simpler query as an option.

g.V('joshTest2').fold()
.coalesce(
    unfold().project('bi_directional_connection', 'single_directional_connection')
            .by(
                bothE('bi_directional_connection')
                    .has('status', 'ACCEPTED')
                    .otherV()
                    .has('active', true)
                    .values('user_id')
                    .dedup()
                    .fold()
                    .limit(100)
            )
            .by(
                outE('single_directional_connection')
                    .otherV()
                    .values('user_id')
                    .dedup()
                    .fold()
                    .limit(100)

            ).select(values)
             .unfold()
             .unfold()
             .fold(),
        
    project('err').by(constant("user does not exist"))
)

This yields

['joshTest3', 'joshTest4', 'joshTest1']
Kelvin Lawrence
  • 14,674
  • 2
  • 16
  • 38
  • It works! I'm new to gremlin and this definitely helps me understand a bit more. Thanks so much for your time! – Josh Chappelle Jun 22 '21 at 22:11
  • This works in a Jupyter notebook but it looks like the result I'm getting back from Neptune within my application has this wrapped within an array. This is what the application is getting when I look at the data via a breakpoint. [['joshTest3', 'joshTest4', 'joshTest1']]. I'm going to dig further into this. It may be something in our application that's incorrect. I'll post a new question if I can't figure out what's going on. Thanks again! – Josh Chappelle Jun 22 '21 at 22:35