Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache Tinkerpop Gremlin Returns Query Results using Select Values As

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?

like image 361
user962206 Avatar asked Feb 27 '26 23:02

user962206


1 Answers

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.

like image 133
stephen mallette Avatar answered Mar 02 '26 15:03

stephen mallette



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!