Graph Mathing with OSQL

52 views
Skip to first unread message

K Major

unread,
Apr 21, 2016, 1:41:26 PM4/21/16
to OrientDB

Hi, 
is it possible, using OSQL, to match the following structure in the graph ?


















So basically, I want a node going to 2 other nodes B and C such that those two last nodes are connected.

For now, I have used Gremlin to accomplish this

List<Row> list = new GremlinPipeline<Object, Object>(graphDb.getVertices("Person.name", 1)).as("a")
.out().except("a").as("b") 
.out().except("a").except("b").as("c").out().retain("b").back("c").in().retain("a").select().toList();


user.w...@gmail.com

unread,
Apr 21, 2016, 2:12:32 PM4/21/16
to orient-...@googlegroups.com
Hi,

to reproduce your graph in OrientDB you can use this commands:

create class Person extends V
create property
Person.name string
insert
into Person(name) values ("A"),("B"),("C")
create
class connect extends E
create edge connect
from (select from Person where name="A") to (select from Person where name="B")
create edge connect
from (select from Person where name="A") to (select from Person where name="C")
create edge connect
from (select from Person where name="B") to (select from Person where name="C")
create edge connect
from (select from Person where name="C") to (select from Person where name="B")

to create the edges you can also use the @rid like in the example below:

create edge connect from #12:0 to #12:1
create edge connect
from #12:0 to #12:2
create edge connect
from #12:1 to #12:2
create edge connect
from #12:2 to #12:1

this is the result:

In my opinion you can avoid the double edge between B and C because with the edges you can traverse vertices in both directions using this commands: in(), out() and both().
But it's fine as well.

Hope it helps,

Regards,
Michela

K Major

unread,
Apr 22, 2016, 2:54:58 AM4/22/16
to OrientDB
Hello,
Maybe I didn"t express myself very well.

I'm not looking for a way to create this structure but to rather find it inside a graph.

Suppose you have a large graph, I want to design a query thaht find those nodes with this particular structure inside my graph. 

Thats exactly what the query in Gremlin is doing : it scans the whole graph and looks for nodes such that they have this structure and returns the nodes having this structure.

I was wondering if it possible to design a query that also looks for this structure inside my graph but written in OSQL instead ?

Luigi Dell'Aquila

unread,
Apr 22, 2016, 3:52:45 AM4/22/16
to orient-...@googlegroups.com
Hi,

In v2.2 it's definitely easy using MATCH statements (https://github.com/orientechnologies/orientdb-docs/blob/master/SQL-Match.md)

MATCH
{class: Person, as: A, where: (name = 1)}.out(){as: B}.out(){as: C},
{as: A}.out(){as: C}.out(){as: B}
RETURN A

with a plain SQL is a bit more tricky, you have to use subqueries

Thanks

Luigi


--

---
You received this message because you are subscribed to the Google Groups "OrientDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to orient-databa...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

K Major

unread,
Apr 23, 2016, 6:18:07 AM4/23/16
to OrientDB
 Okay thanks a lot !
Reply all
Reply to author
Forward
0 new messages