简体   繁体   中英

2-steps query in OrientDB

I'm evaluating OrientDB and Neo4j in this simple toy example composed by:

  • Employees, identified by eid
  • Meetings, identified by mid and having start and end attributes encoding their start and end DateTime.

Both entities are represented by different classes of vertices, namely Employee and CalendarEvent , which are connected by Involves edges specifying that CalendarEvent-[Involves]->Employee .

My task is to write a query that returns, for each pair of employees, the date/time of their first meeting and the number of meetings they co-attended. In Cypher I would write something like:

MATCH (e0: Employee)<-[:INVOLVES]-(c:CalendarEvent)-[:INVOLVES]->(e1: Employee)
WHERE e0.eid > e1.eid
RETURN e0.eid, e1.eid, min(c.start) as first_met, count(*) as frequency

I wrote the following query for OrientDB:

SELECT eid, other, count(*) AS frequency, min(start) as first_met
FROM (
  SELECT eid, event.start as start, event.out('Involves').eid as other
  FROM (
    SELECT 
    eid, 
    in('Involves') as event
    FROM Employee UNWIND event
    ) UNWIND other ) 
GROUP BY eid, other

but it seems over-complicated to me. Does anybody knows if there is an easier way to express the same query?

yes, your query is correct and this is what you have to do in current version (2.1.x).

From 2.2, with MATCH statement ( https://github.com/orientechnologies/orientdb-docs/blob/master/source/SQL-Match.md ), you will be able to write a query very similar to Cypher version:

select eid0, eid1, min(start) as firstMet, count(*) from (
    MATCH {class:Person, as:e0}.in("Involves"){as: meeting}.out("Involves"){as:e1}
    return e0.eid as eid0, e1.eid as eid1, meeting.start as start
) group by eid0, eid1

This feature is till in beta, probably in final version you will have more operators in the MATCH statement itself and the query will be even shorter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM