Query to find node which have only one vertex in common

94 views
Skip to first unread message

Harshit Sharma

unread,
Mar 31, 2021, 1:01:26 PM3/31/21
to Gremlin-users
I have the following vertices -

Person1 -> Device1 <- Person2 
         |              |
        v             v
  Email1  <-  Person3

Now I want to write a gremlin query (janusgraph) which will give me all persons connected to the device(only) with which person1 is connected.
So according to the above graph, our output should be - [person2].
Person3 is not in output because person3 is also connected with "Email1" of "Person1"

Harshit Sharma

unread,
Mar 31, 2021, 1:15:42 PM3/31/21
to Gremlin-users
Updated graph

Person1 -> Device1 <- Person2 
                      ^
         |            |
        v             
  Email1  <-  Person3

Harshit Sharma

unread,
Apr 1, 2021, 12:11:17 AM4/1/21
to Gremlin-users
Screenshot 2021-04-01 at 9.41.02 AM.png

Bassem Naguib

unread,
Apr 1, 2021, 3:45:12 AM4/1/21
to Gremlin-users
It helps a lot when you give us a traversal that creates the graph. Like so

g.addV('person').property('name', 'Person1').as('p1').
  addV('person').property('name', 'Person2').as('p2').
  addV('person').property('name', 'Person3').as('p3').
  addV('device').as('d1').
  addV('email').as('e1').
  addE('HAS_DEVICE').from('p1').to('d1').
  addE('HAS_EMAIL').from('p1').to('e1').
  addE('HAS_DEVICE').from('p2').to('d1').
  addE('HAS_DEVICE').from('p3').to('d1').
  addE('HAS_EMAIL').from('p3').to('e1')

The following traversal will give you the person vertices that are connected to "Person1" via one or more "device" vertices and not connected via any other type of vertices.

g.V().has('person', 'name', 'Person1').as('p1').
  out().as('connector').
  in().where(neq('p1')).
  group().
    by().
    by(select('connector').label().fold()).
  unfold().
  where(
    select(values).
    unfold().dedup().fold().  // just in case the persons are connected by multiple devices
    is(eq(['device']))
  ).
  select(keys)

I hope this helps.

Harshit Sharma

unread,
Apr 1, 2021, 5:26:31 AM4/1/21
to Gremlin-users
Actually, I tried this same query for Email, by replacing 'device' with 'Email'. So in that case it is not working.

Harshit Sharma

unread,
Apr 1, 2021, 5:27:10 AM4/1/21
to Gremlin-users
g.V().has('person', 'name', 'Person1').as('p1').
  out().as('connector').
  in().where(neq('p1')).
  group().
    by().
    by(select('connector').label().fold()).
  unfold().
  where(
    select(values).
    unfold().dedup().fold().  // just in case the persons are connected by multiple devices
    is(eq(['email']))
  ).
  select(keys)

Harshit Sharma

unread,
Apr 1, 2021, 6:32:28 AM4/1/21
to Gremlin-users
It is working fine now.. thanks
Message has been deleted

Harshit Sharma

unread,
Jul 28, 2021, 9:16:17 AM7/28/21
to Gremlin-users
Is it required to use where(neq('p1')) ? i tried without this and answer is still same.
Is it possible to optimize this step - group().by().by(select('connector').label().fold()) ?because as per profile() this step is taking majority of the time and in case of large data this query becomes slow.
On Thursday, April 1, 2021 at 1:15:12 PM UTC+5:30 bass...@gmail.com wrote:
Reply all
Reply to author
Forward
0 new messages