Search code examples
sqlorientdbgraph-databases

OrientDB select Vertex, Edge pairs from query


In an OrientDb graph database, I'm trying to get some information about Vertex, Edge pairs.

For example, consider the following case:

V1 ---E1---> V2
   ---E2---> V3 --E3--> V2

I would like to have as result the following 3 rows;

V1, E1
V1, E2
V3, E3

I've tried the following:

select label, flatten(out.label) from V
select label from (select flatten(out) from V)
select label, flatten(out) from V
select flatten(out) from V
select $current, label from (traverse out from V while $depth <= 1) where $depth = 1

But none of these solutions seem to return what I want. How can I return Vertex, Edge pairs?


Solution

  • What you are trying to do is actually extremely simple with OrientDB, it seems you are overthinking the issue.

    Let's create your example:

    V1 ---E1---> V2
       ---E2---> V3 --E3--> V2
    

    In OrientDB, you would do this as follows:

    /* Create nodes */
    CREATE CLASS Node EXTENDS V
    CREATE PROPERTY Node.name STRING (MANDATORY TRUE)
    CREATE VERTEX Node SET name = 'V1'
    CREATE VERTEX Node SET name = 'V2'
    CREATE VERTEX Node SET name = 'V3'
    
    /* Create edges */
    CREATE CLASS Link EXTENDS E
    CREATE PROPERTY Link.name STRING (MANDATORY TRUE)
    CREATE EDGE Link
        FROM (SELECT FROM Node WHERE name = 'V1') 
        TO (SELECT FROM Node WHERE name = 'V2')
        SET name = 'E1'
    CREATE EDGE Link
        FROM (SELECT FROM Node WHERE name = 'V1')
        TO (SELECT FROM Node WHERE name = 'V3')
        SET name = 'E2'
    CREATE EDGE Link 
        FROM (SELECT FROM Node WHERE name = 'V3')
        TO (SELECT FROM Node WHERE name = 'V2')
        SET name = 'E3'
    

    This creates the following graph:

    Example OrientDB graph

    Now a little explanation of how to query in OrientDB. Let's say you load one vertex: SELECT * FROM Node WHERE name = 'V1'. Then, to load other information, you use:

    • To load all incoming vertices (skipping the edges): in()
    • To load all incoming vertices of class Link (skipping the edges): in('Link')
    • To load all incoming edges: inE()
    • To load all incoming edges of class Link: inE('Link')
    • To load all outgoing vertices (skipping the edges): out()
    • To load all outgoing vertices of class Link (skipping the edges): out('Link')
    • To load all outgoing edges: outE()
    • To load all outgoing edges of class Link: outE('Link')

    So in your case, you want to load all the vertices and their outgoing edges, so we do:

    SELECT name, outE('Link') FROM Node
    

    Which loads the name of the vertices and a pointer to the outgoing edges:

    Example of OrientDB query

    If you would like to have a list of the names of the outgoing edges, we simply do:

    SELECT name, outE('Link').name FROM Node
    

    Which gives:

    Example of OrientDB query 2

    Which is exactly what you asked for in your question. As you can see, this is extremely simple to do in OrientDB, you just need to realize that OrientDB is smarter than you think :)