user490895
user490895

Reputation: 335

Gremlin query to combine edge with property

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?

Upvotes: 4

Views: 1519

Answers (1)

Jason Plurad
Jason Plurad

Reputation: 6792

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

Upvotes: 8

Related Questions