How do I conditionally create edges between a nodes based on property matching in both S and T?

58 views
Skip to first unread message

William

unread,
Apr 12, 2017, 2:37:57 PM4/12/17
to OrientDB
I'm trying to construct a query that creates edges between a specific node and other nodes that match it... I've been looking at the docs on CREATE EDGE but haven't made much progress.

CREATE EDGE myEdge
FROM
#25:0
TO
(SELECT FROM NodeClass2 WHERE Label1 = <source node>.Label1 AND Label2 = <source node>.Label2)

and my node #25:0 is of type NodeClass1, but both classes have Label1 and Label2 fields that should be matched.

How do get the destination part of the query to reference my source node's fields?    I think there's something that needs to go in my <source node> fields above, but it's not $parent or $current... is there another $variable that I can reference to get at this?

Thanks!
  -William

scott molinari

unread,
Apr 20, 2017, 5:22:39 AM4/20/17
to orient-...@googlegroups.com
Can you use aliases?

CREATE EDGE myEdge
FROM (SELECT Label1 AS NodeLabel1, Label2 AS NodeLabel2 WHERE @RID = #
25:0)
TO
(SELECT FROM NodeClass2 WHERE Label1 = NodeLabel1 AND Label2 = NodeLabel2)

It's just a wild guess and not tested.

Scott


William

unread,
Apr 20, 2017, 5:41:24 PM4/20/17
to orient-...@googlegroups.com
Hi Scott,

I tried this and ran into a couple of things...

It looks like there must be a 'FROM' field in the first SELECT.  I'm not sure why this is required since my read on the syntax for SELECT is that the FROM field is optional and decoupled from WHERE:

SELECT [ <Projections> ] [ FROM <Target> [ LET <Assignment>* ] ]
   
[ WHERE <Condition>* ]
   
[ GROUP BY <Field>* ]

That's ok, I just added a "FROM V".  This changes my query to this:

CREATE EDGE myEdge
FROM
(SELECT Label1 as NodeLabel1, Label2 AS NodeLabel2 FROM V WHERE @RID = #361:0)
TO
(SELECT FROM node WHERE Label1 = NodeLabel1 AND Label2 = NodeLabel2)

It still fails, and the error message isn't very helpful:

2017-04-20 15:31:43:909 SEVER Internal server error:
com.orientechnologies.orient.core.exception.OCommandExecutionException: No edge has been created because no target vertices
DB name="testbed-004.orientdb" [ONetworkProtocolHttpDb]

I thought I'd try out something with a LET block:

CREATE EDGE myEdge
FROM
(SELECT FROM data LET $temp=(SELECT Label1,Label2 FROM data) WHERE @RID = #361:0)
TO
(SELECT FROM node WHERE Label1 = $temp.Label1 AND Label2 = $temp.Label2)

But this just gives me the same error.

I'd think there should be a way to have that second SELECT match to fields in the records in first vertex, but the solution remains elusive for me.

William

unread,
Apr 20, 2017, 6:32:08 PM4/20/17
to OrientDB
Ah, I think I figured out a way to do it...

I still don't know how to get the second SELECT to know anything about what's in the first one, but I can use a LET block in the 2nd select that also looks for the @RID.  Since there's only one that should get returned by that I can use first($src) to pull it in the 2nd query:

CREATE EDGE myEdge
FROM
(SELECT FROM data WHERE @RID = #361:0)
TO
(SELECT FROM node
        LET $src
= (SELECT FROM data WHERE @RID = #361:0)
        WHERE
Label1 = first($src).Label1 AND Label2 = first($src).Label2)

In my toy problem I have 'nodes':

Label1, Label2
Alpha,A
Alpha,B
Bravo,A
Bravo,B

and 'data':
Label1,Label2,Value
Alpha,A,10
Alpha,A,20
Alpha,B,30
Alpha,B,40

There can be many entries in 'data' that link to my 'node' entries and I only want to match my edges when both Label1 and Label2 match...

The above CREATE EDGE query generated my edges:




.. but it does feel a bit inefficient since I'm having to execute the SELECT FROM data WHERE @RID = #361:0 twice for each one.  I also will have to execute this for all of my nodes in data.  I'd really like to make this look something like:

CREATE EDGE
FROM
(SELECT FROM data)
TO
(SELECT FROM node WHERE node.Label1 = <current data node in 'FROM'>.Label1 AND node.Label2 = <current data node in 'FROM'>.Label2


FWIW, I can at least move forward with this and just loop over entries in my data or node classes.

scott molinari

unread,
Apr 24, 2017, 6:49:28 AM4/24/17
to OrientDB
Sorry, I haven't had any time to dig any deeper on this, but I'd be money this can be done easier.

Scott
Reply all
Reply to author
Forward
0 new messages