Dynamic edge creation

52 views
Skip to first unread message

Ankur Goel

unread,
Sep 15, 2017, 5:32:46 AM9/15/17
to JanusGraph users
Hi,

 In JanusGraph to create edges we have two steps process:
  1. First identify the vertex
  2. Then create edges between vertex
Linkage between both steps is through App only.

How ever in neo4j, we can create edges using one step:

MATCH (a:player), (b:Country) WHERE a.POB = "X" AND b.name = "Y" 
CREATE (a)-[r: Z]->(b) 
RETURN a,b

Can we achieve the same in JanusGraph using Janus or tinkerpoop.

~AnkurG

Robert Dale

unread,
Sep 15, 2017, 6:56:59 AM9/15/17
to Ankur Goel, JanusGraph users

g.V().has('player','POB','X').as('a').V().has('Country','name','Y').addE('Z').from('a')

or you can flip it around

g.V().has('Country','name','Y').as('b').V().has('player','POB','X').addE('Z').to('b')


Robert Dale

--
You received this message because you are subscribed to the Google Groups "JanusGraph users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to janusgraph-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/janusgraph-users/21e9d0a0-3747-49a5-8193-89f7dac51abe%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Robert Dale

unread,
Sep 15, 2017, 6:57:27 AM9/15/17
to Ankur Goel, JanusGraph users

Ankur Goel

unread,
Sep 15, 2017, 9:44:46 AM9/15/17
to JanusGraph users
Thnx Robert,

i am looking for a query like create edges where they have common properties without explicitly defining properties values:

g.V().has('player','POB',${A}).as('a').V().has('Country','name',{B}).addE('Z').from('a').where(${A} = ${B})

~AnkurG

Robert Dale

unread,
Sep 15, 2017, 9:57:35 AM9/15/17
to Ankur Goel, JanusGraph users

g.V().hasLabel('player').as('player').V().hasLabel('Country').where(eq('player')).by('name').by('POB').addE('Z').from('player')

Robert Dale

--
You received this message because you are subscribed to the Google Groups "JanusGraph users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to janusgraph-users+unsubscribe@googlegroups.com.

Daniel Kuppitz

unread,
Sep 15, 2017, 10:22:16 AM9/15/17
to JanusGraph users
This should do the trick:

g.V().has('player').as('a').
  V().has('Country').where(eq('a')).by('name').by('POB').
    addE('Z').from('a')

However, note that this query will do 2 full table scans under the hood (unless Janus already has some runtime optimization that I'm not aware of). In any case it will be at least one full table scan.

Cheers,
Daniel


Daniel Kuppitz

unread,
Sep 15, 2017, 10:24:16 AM9/15/17
to JanusGraph users
Oops, Gmail didn't notify me about your response, sorry for the double-post.

Ankur Goel

unread,
Sep 18, 2017, 1:58:53 AM9/18/17
to Daniel Kuppitz, JanusGraph users
Thnx Robert n Daniel,

Its working like charm. But it got stuck in other use case:

where i want to create relation based on team:

g.V().hasLabel('player').as('p1').V().hasLabel('player').where(eq('l1')).by('teamName').addE('sameTeam').from('p1')

in this i want to create edges between same teamName player.

Above gremlin query is working but creating all combination of edges. For example for a 3 node of player label  having same team name, its creating 9 edges, instead of 3 only. Double edges for between two vertex and one loop to same vertex.

~AnkurG


--
You received this message because you are subscribed to a topic in the Google Groups "JanusGraph users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/janusgraph-users/7Cn4G__XGjU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to janusgraph-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/janusgraph-users/CA%2Bf9seVEwxs2%2BHFmQV-8mJ_2LU79tjkAJ5AeorsH%3De2PtLwk4g%40mail.gmail.com.

Robert Dale

unread,
Sep 18, 2017, 6:21:59 AM9/18/17
to Ankur Goel, Daniel Kuppitz, JanusGraph users
If `l1` is `p1` then yes, this will create an edge from every player p1 to every player p2. You're not filtering anything out so all players show up in both `V().hasLabel('player')`.  If you want to filter out `self` and `has existing 'sameTeam' edge, try this:

g.V().hasLabel('player').as('p1').V().hasLabel('player').where(neq('p1')).where(eq('p1')).by('teamName').not(outE('sameTeam')).addE('sameTeam').from('p1')

Robert Dale

Robert Dale

unread,
Sep 18, 2017, 6:28:24 AM9/18/17
to JanusGraph users

Change `outE()` to `bothE()` since you don't care about direction.

Ankur Goel

unread,
Sep 20, 2017, 12:55:13 AM9/20/17
to JanusGraph users
Thnx Robert, it helped big time.


On Friday, September 15, 2017 at 3:02:46 PM UTC+5:30, Ankur Goel wrote:
Reply all
Reply to author
Forward
0 new messages