OrientDB select Vertex, Edge pairs from query

by gerben   Last Updated October 16, 2019 12:26 PM - source

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?



Answers 3


FLATTEN operator works alone, because get a field and let it to become the result. I don't understand what you want to do. Can you write the expected output please?

Lvca
Lvca
February 26, 2013 11:44 AM

The CYPHER syntax, as used in Neo4j finally rescued me.

start n=node(*) MATCH (n)-[left]->(n2)<-[right]-(n3) WHERE n.type? ='myType' AND left.line > right.line - 1 AND left.line < right.line + 1 RETURN n, left, n2, right, n3

The node n is the pivoting element, on wich an filter can be provided, just as on each other step within the path. For me it was important to select a further step depending on an other part of the path.

With OrientDb I couldnt find a way to relate the properties to each other easily.

gerben
gerben
February 26, 2013 13:32 PM

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 :)

Jean-Paul
Jean-Paul
October 15, 2019 09:34 AM

Related Questions



Modelling Tools for Graph Databases

Updated May 01, 2017 08:26 AM

how to connect gremlin server with orientDB

Updated May 04, 2018 05:26 AM