-1

I am creating a vertex with multiple edges but need a bit of help writing a query to retrieve the data.

Creation query

g.addV("referral")
                            .as("r")
                            .property("createdAt", Date.now())
                            .addE("prospect")
                            .from_("r")
                            .to(__.V(user.id))
                            .addE("opportunity")
                            .from_("r")
                            .to(__.V(second_user.id))
                            .addE("referredBy")
                            .from_("r")
                            .to(__.V(business.id))
                            .select("r")
                            .next()

I want to run a query that gets data from the first and second user. So far I have

g.V(business.id) //business
            .in_("opportunity")
            .as("referral")
            .outV("referredBy")
            .as("referrer")
            .inV("prospect")
            .as("prospect")
            .select("referral", "referrer", "prospect")
            .toList()
            .next()

I'm getting an error when running this query. I basically want an array of a referral, referrer and prospect in one object that I can iterate through. Also any help on making my initial query better would be helpful.

Please let me know if this makes sense or if you need any other info. Any help would be appreciated.

xeroshogun
  • 1,062
  • 1
  • 18
  • 31

1 Answers1

1

The errors are because you are using outV when you should be using out. You only need to use inV and outV after outE and inE. So your query should be

g.V(business.id) //business
 .in("opportunity")
 .as("referral")
 .out("referredBy")
 .as("referrer")
 .in("prospect")
 .as("prospect")
 .select("referral", "referrer", "prospect")
 .toList()

Also you don't need next as you already have toList.

Lastly rather than use as and select I would look at the path step instead.

g.V(business.id) //business
 .in("opportunity")
 .out("referredBy")
 .in("prospect")
 .path()
 .toList()
Kelvin Lawrence
  • 14,674
  • 2
  • 16
  • 38
  • So this isn't quite returning what I want. I'm pretty sure after the "referredBy" step I need to go back a step before doing prospect. The "referral" vertex has 3 edges all pointing out. Its the central vertex in this model. If I do "out('referredBy')" I believe that takes me to a user vertex, but "in('prospect')" needs to go in from "referral" vertex, not "user" vertex. I think I need a way to save what I have so far, then go back a step to referral vertex, then go in to get prospect. Let me know if that makes sense and thanks for the help so far. – xeroshogun Oct 21 '21 at 14:46
  • If you could edit the question to include just some `addV` and `addE` steps to create a sample graph (as I do not know what `business.id` refers to etc. and also show an example of the exact output you need that would help build a tested solution. – Kelvin Lawrence Oct 21 '21 at 14:51
  • There is an example of a small sample graph being created in this post https://stackoverflow.com/questions/51388315/gremlin-choose-one-item-at-random – Kelvin Lawrence Oct 21 '21 at 15:03
  • 1
    I was able to figure it out. Seeing how path is used helped me to get what I wanted. Essentially did ```.in("opportunity").out("referredBy").in("referredBy").out("prospect").path().unfold().dedup()```. I went out from the referral vertex then back to that vertex to get the other edge that I wanted and dedup to take the duplicate out of the path. Thanks for your help – xeroshogun Oct 21 '21 at 15:20