Search code examples
neo4jorientdborientdb-2.1nosql

Rewrite neo4j queries for OrientDB


I'm testing performance neo4j vs OrientDB graph DBs. I'm using MoiveDatabase dataset available on neo4j site. I've followed the instructions:

export-graphml -t -o /tmp/out.graphml
CREATE DATABASE plocal:/tmp/db/test
IMPORT DATABASE /tmp/out.graphml storeVertexIds=true

Here is classes output so I guess import was OK.

CLASSES
----------------------+------------------------------------+------------+----------------+
 NAME                 | SUPERCLASS                         | CLUSTERS   | RECORDS        |
----------------------+------------------------------------+------------+----------------+
 ACTS_IN              | [E]                                | 14         |          94700 |
 DIRECTED             | [E]                                | 13         |          11915 |
 E                    |                                    | 10         |              0 |
 FRIEND               | [E]                                | 16         |              6 |
 Movie                | [V]                                | 12         |           6379 |
 OFunction            |                                    | 6          |              0 |
 OIdentity            |                                    | -          |              0 |
 ORestricted          |                                    | -          |              0 |
 ORIDs                |                                    | 8          |              0 |
 ORole                | [OIdentity]                        | 4          |              3 |
 OSchedule            |                                    | 7          |              0 |
 OTriggered           |                                    | -          |              0 |
 OUser                | [OIdentity]                        | 5          |              3 |
 Person               | [V]                                | 11         |          50013 |
 RATED                | [E]                                | 15         |             30 |
 V                    |                                    | 9          |          61752 |
----------------------+------------------------------------+------------+----------------+
 TOTAL = 16                                                                       224801 |
----------------------+------------------------------------+------------+----------------+

But when I try to execute this query: SELECT in() FROM Movie LIMIT 10 or SELEC out() FROM Person LIMIT 10 I don't get any records. Why is that?

I'm trying to rewrite co-actors query:

 MATCH (tom:Person {name:"Tom Hanks"})-[:ACTED_IN]->(:Movie)<-[:ACTED_IN]-(coActor:Person)
 RETURN coActor.name 

and "co-co-actors" query:

MATCH (tom:Person)-[:ACTED_IN]->(movie1)<-[:ACTED_IN]-(coActor:Person),
(coActor)-[:ACTED_IN]->(movie2)<-[:ACTED_IN]-(coCoActor:Person)
WHERE tom.name = "Tom Hanks" AND   NOT    (tom)-[:ACTED_IN]->(movie2)

RETURN coCoActor.name

Should I use TRAVERSE for this?


Solution

  • I created this small DB to try your case:

    create class Person extends V
    create class Movie extends V
    create class acts_In extends E
    create class directed extends E
    create class friend extends E
    create class rated extends E
    
    create property Person.name String
    create property Person.surname String
    create property Movie.title String
    
    create vertex Person set name="Tom", surname="Hanks"
    create vertex Person set name="Robin", surname="Wright"
    create vertex Person set name="Helen", surname="Hunt"
    create vertex Person set name="Robert", surname="Zemeckis"
    create vertex Person set name="Russell", surname="Crowe"
    create vertex Person set name="Ben", surname="Affleck"
    create vertex Person set name="Kevin", surname="Macdonald"
    create vertex Person set name="John"
    create vertex Person set name="Mark"
    create vertex Person set name="Paul"
    create vertex Person set name="Mel", surname="Gibson"
    create vertex Person set name="Nancy", surname="Meyers"
    create vertex Movie set title="Forrest Gump"
    create vertex Movie set title="Cast Away"
    create vertex Movie set title="State of Play"
    create vertex Movie set title="What Women Want"
    
    create edge acts_In from (select from Person where name="Tom" and surname="Hanks") to (select from Movie where title="Forrest Gump")
    create edge acts_In from (select from Person where name="Tom" and surname="Hanks") to (select from Movie where title="Cast Away")
    create edge acts_In from (select from Person where name="Robin" and surname="Wright") to (select from Movie where title="Forrest Gump")
    create edge acts_In from (select from Person where name="Robin" and surname="Wright") to (select from Movie where title="State of Play")
    create edge acts_In from (select from Person where name="Helen" and surname="Hunt") to (select from Movie where title="Cast Away")
    create edge acts_In from (select from Person where name="Helen" and surname="Hunt") to (select from Movie where title="What Women Want")
    create edge acts_In from (select from Person where name="Mel" and surname="Gibson") to (select from Movie where title="What Women Want")
    create edge acts_In from (select from Person where name="Russell" and surname="Crowe") to (select from Movie where title="State of Play")
    create edge acts_In from (select from Person where name="Ben" and surname="Affleck") to (select from Movie where title="State of Play")
    create edge friend from (select from Person where name="Mel" and surname="Gibson") to (select from Person where name="Helen" and surname="Hunt")
    create edge friend from (select from Person where name="Ben" and surname="Affleck") to (select from Person where name="Russell" and surname="Crowe")
    create edge directed from (select from Movie where title="What Women Want") to (select from Person where name="Nancy" and surname="Meyers")
    create edge directed from (select from Movie where title="Cast Away") to (select from Person where name="Robert" and surname="Zemeckis")
    create edge directed from (select from Movie where title="Forrest Gump") to (select from Person where name="Robert" and surname="Zemeckis")
    create edge directed from (select from Movie where title="State of Play") to (select from Person where name="Kevin" and surname="Macdonald")
    create edge rated from (select from Movie where title="What Women Want") to (select from Person where name="Paul")
    create edge rated from (select from Movie where title="Cast Away") to (select from Person where name="John")
    create edge rated from (select from Movie where title="Forrest Gump") to (select from Person where name="Mark")
    create edge rated from (select from Movie where title="State of Play") to (select from Person where name="John")
    

    First query: find the co-actor of Tom Hanks

    select distinct(name) as name, distinct(surname) as surname from (select expand(in('acts_In')) from Movie where in('acts_In').name in 'Tom' 
    and in('acts_In').surname in 'Hanks') where name<>'Tom' and in('acts_In').surname<>'Hanks'
    

    Output:

    ----+------+-----+-------
    #   |@CLASS|name |surname
    ----+------+-----+-------
    0   |null  |Robin|Wright
    1   |null  |Helen|Hunt
    ----+------+-----+-------
    

    Second query: find the co-co-actor with whom Tom Hanks hasn't starred

    select name, surname from (select expand($ris)
    let $a=(select from Person where out('acts_In').size()>0 and name<>'Tom' and surname<>'Hanks'),
        $b=(select from (select expand(in('acts_In')) from Movie where in('acts_In').name in 'Tom' and in('acts_In').surname in 'Hanks') where name<>'Tom' and in('acts_In').surname<>'Hanks'),
        $ris=difference($a,$b))
    

    Output:

    ----+------+-------+-------
    #   |@CLASS|name   |surname
    ----+------+-------+-------
    0   |null  |Russell|Crowe
    1   |null  |Ben    |Affleck
    2   |null  |Mel    |Gibson
    ----+------+-------+-------
    

    Hope it helps