Query to find related nodes common to a set of source nodes

12 views
Skip to first unread message

Nikhil Lanjewar

unread,
Jan 13, 2012, 3:17:27 AM1/13/12
to Gremlin-users
Hey Guys,

I'm trying to find a set of related nodes which are common to a set of
source nodes using Gremlin via Neography v0.0.20 in a Rails
application. A general scenario can be:

v[1] - related - v[2]
v[3] - related - v[2]
v[4] - related - v[2]
v[1] - related - v[5]
v[3] - related - v[6]
v[4] - related - v[6]
v[1] - related - v[6]

The function related_for(v[1],v[3],v[4]) is expected to emit v[2],v[6]
as an output. I'm using the following script to achieve this for 3
source nodes and getting desired results.

[...]
neo.execute_script("g.v(nodes[0]).both.both.filter{it ==
g.v(nodes[1])}.back(2).uniqueObject().both.filter{it ==
g.v(nodes[2])}.back(2).uniqueObject().filter{it.result_type ==
result_type}.name",{:nodes => [1,3,4], :result_type => 'SomeType'})

I'm adding a 'result_type' filter finally just to get rid of unwanted
nodes. (neo is an object of Neography::Rest, result_type is one of the
attributes of each of my nodes)

I was wondering if this lookup could be made dynamic so that it
responds to a 'nodes' array of any size. I tried using nodes.pop()
along with 'loop' but it couldn't fetch any results. Here's the script
for the same:

[...]
neo.execute_script("g.v(nodes.pop()).both.both.filter{it ==
g.v(nodes.pop())}.back(2).uniqueObject().loop(2){nodes.size() >
0}.filter{it.result_type == result_type}.name",{:nodes =>
[1,3,4], :result_type => 'SomeType'})

Further, I feel that the above can be optimized for performance and
that there's a better way to do this. Any suggestions are welcome.

--
Nikhil
Reply all
Reply to author
Forward
0 new messages