Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gremlin query to combine edge with property

Tags:

gremlin

I have a data schema where users can review products. user and product are vertices, and reviews is a MANY2MANY relation between users and products. reviews has edge properties such as title and body.

I want to write a query to get all reviews for a product, as well as the users that wrote the review. I can get the reviews using the following, but I don't know how to add all the user properties.

g.({product}).inE("reviews").values()

How can I get user information per review as well?

like image 695
user490895 Avatar asked Feb 06 '23 14:02

user490895


1 Answers

You could do something like this:

l = g.V(pvid).                          // start with a product vertex id
      inE("reviews").as("r").           // label the review edges
      outV().as("u").                   // label the users
      select("r", "u").by(valueMap()).  // properties map
      toList()                          // iterate traversal into a list

This will return a list of maps. Each map will have 2 keys, r and u, corresponding to the select("r", "u") step. Then the value of the r in the map are the properties for the review. Similarly the value of the u in the map are the properties for the user.

Related documentation

  • As step
  • ValueMap step
like image 99
Jason Plurad Avatar answered Feb 16 '23 09:02

Jason Plurad