3

I have this query that it is working as expected for me.

g.V().or(hasLabel("poi"),hasLabel("business")).as("dest")
.outE().inV().hasLabel("region").as("reg")
.select("dest").values("name").as("dest_name")
.select("dest").values("budget").as("dest_budget")
.select("reg").values("name").as("reg_name")
.select("reg_name","dest_name","dest_budget")

This query yields this result.

enter image description here

As I have expected. However I need to retrieve more properties from the "destination" I need to retrieve more 10 properties. This will me into something like this

g.V().or(hasLabel("poi"),hasLabel("business")).as("dest")
.outE().inV().hasLabel("region").as("reg")
.select("dest").values("name").as("dest_name")
.select("dest").values("budget").as("dest_budget")
.select("dest").values("property3").as("property3")
.select("dest").values("property4").as("property4")
//insert more queries like from the above
.select("reg").values("name").as("reg_name")
.select("reg_name","dest_name","dest_budget","property3","property4")

The query will grow long eventually which I am trying to avoid since I need to select values as well from the region as well. So My initial thought was to use select to select multiple values and label them each with an alias like this

g.V().
or(hasLabel("poi"),hasLabel("business"))
.as("destination")
.outE().inV().as("region")
.select("destination").values("name","budget").as("dest_name","dest_budget")
.select("region").values("name").as("reg_name")
.select("dest_name","reg_name","dest_budget")

However I was surprised with this result. Which I was not expecting.

enter image description here

To my understanding the names in values will be mapped to each values passed in the as step. Am I wrong?

Is there anyway for me to retrieve the result from the first screenshot without writing a long query?

user962206
  • 15,637
  • 61
  • 177
  • 270

1 Answers1

2

The as() labels the step, not the values within that step. So by doing:

.select("destination").values("name","budget").as("dest_name","dest_budget")

you're just naming the values() step twice. I think that you can drastically simplify this traversal for what you want to get as the result though and it doesn't involve stringing together a lot of select() steps:

g.V().or(hasLabel("poi"),hasLabel("business")).
  project('dest_name','dest_budget','reg_name').
    by('name').
    by('budget').
    by(out().hasLabel("region").values('name').fold())

You will get a slightly different structure in that "reg_name" will be a list of all the region names rather than having a flattened structure, but you could unroll that I imagine if needed.

stephen mallette
  • 45,298
  • 5
  • 67
  • 135
  • What about if OP needs to retrieve multiple values from the "out.hasLabels("region")? since to my understanding the OP wants to retrieve more values from the region as well (such as region_name , region points and etc) – KyelJmD Jan 13 '19 at 23:44
  • the `by()` modulator takes a `Traversal` (among other things) for its argument so any bit of Gremlin you need there will work. Instead of `values('name')`, do a `project()` and instead of a `List` of "name" values you get a `List` of `Map` values with whatever keys of the regions you want. – stephen mallette Jan 14 '19 at 12:37