3
votes

I am a beginner in gremlin with Tinker pop. I have a graph where a cuisine has multiple inE("serves") from restaurants: enter image description here

From these restaurants, I want only two restaurants which have location "Karachi". These are: enter image description here enter image description here

For this, I've written the query and it works fine:

g.V(26).as("cui").inE("serves").outV().hasLabel("restaurant").has("location", "karachi").as("rest")

What I want is a gremlin query that gets the inE("reviews") of these restaurants and shape the data in this way.

[
"restaurant" : // info related to restaurant,
"reviews": [{array of reviews}]
]

I've tried this script:

 g.V(26).as("cui").inE("serves").outV().hasLabel("restaurant").has("location", "karachi").as("rest").where(__.inE("review").has("value",is(gte(2)))).inE("review").order().by("value", desc).as("rev").select("rest","rev").by(elementMap())

But it returns an array of data for every rating in this structure:

["rest": {}, "rating:" {}],
["rest": {}, "rating:" {}],


==>{rest={id=54, label=restaurant, name=restaurant5, location=karachi}, rev={id=67, label=review, IN={id=54, label=restaurant}, OUT={id=9, label=user}, upvotes=3, rated_at=Tue Nov 16 22:13:58 PKT 2021, downvotes=1, value=12}}
==>{rest={id=60, label=restaurant, name=restaurant7, location=karachi}, rev={id=69, label=review, IN={id=60, label=restaurant}, OUT={id=6, label=user}, upvotes=4, rated_at=Tue Nov 16 22:16:45 PKT 2021, downvotes=3, value=5}}
==>{rest={id=60, label=restaurant, name=restaurant7, location=karachi}, rev={id=71, label=review, IN={id=60, label=restaurant}, OUT={id=9, label=user}, upvotes=1, rated_at=Tue Nov 16 22:16:45 PKT 2021, downvotes=5, value=5}}
==>{rest={id=12, label=restaurant, name=restaurant1, location=karachi}, rev={id=41, label=review, IN={id=12, label=restaurant}, OUT={id=0, label=user}, upvotes=8, rated_at=Tue Nov 16 21:49:07 PKT 2021, downvotes=4, value=4}}
==>{rest={id=54, label=restaurant, name=restaurant5, location=karachi}, rev={id=65, label=review, IN={id=54, label=restaurant}, OUT={id=6, label=user}, upvotes=4, rated_at=Tue Nov 16 22:12:57 PKT 2021, downvotes=1, value=4}}
==>{rest={id=12, label=restaurant, name=restaurant1, location=karachi}, rev={id=40, label=review, IN={id=12, label=restaurant}, OUT={id=3, label=user}, upvotes=8, rated_at=Tue Nov 16 21:49:07 PKT 2021, downvotes=4, value=3.7}}
==>{rest={id=60, label=restaurant, name=restaurant7, location=karachi}, rev={id=70, label=review, IN={id=60, label=restaurant}, OUT={id=0, label=user}, upvotes=5, rated_at=Tue Nov 16 22:16:45 PKT 2021, downvotes=2, value=3}}
==>{rest={id=54, label=restaurant, name=restaurant5, location=karachi}, rev={id=68, label=review, IN={id=54, label=restaurant}, OUT={id=3, label=user}, upvotes=2, rated_at=Tue Nov 16 22:14:21 PKT 2021, downvotes=9, value=3}}
==>{rest={id=60, label=restaurant, name=restaurant7, location=karachi}, rev={id=72, label=review, IN={id=60, label=restaurant}, OUT={id=3, label=user}, upvotes=5, rated_at=Tue Nov 16 22:16:46 PKT 2021, downvotes=1, value=2}}
==>{rest={id=54, label=restaurant, name=restaurant5, location=karachi}, rev={id=66, label=review, IN={id=54, label=restaurant}, OUT={id=0, label=user}, upvotes=4.7, rated_at=Tue Nov 16 22:13:32 PKT 2021, downvotes=3, value=2}}

How can I achieve this? Thanks in advance.

1

1 Answers

2
votes

You should look at using the project() step (documentation here) to get this sort of information. Without the steps to reproduce your graph, it is hard to give you the exact query, but it should look similar to the query below:

  g.V(26).as("cui").
  inE("serves").
  outV().
  hasLabel("restaurant").
  has("location", "karachi").
  project('restaurant', 'reviews').
    by(elementMap()).
    by(
      inE("review").
      has('value', gte(2)).
      order().by("value", desc).
      fold())