SQL - MATCH

Queries the database in a declarative manner, using pattern matching. This feature was introduced in version 2.2.

Simplified Syntax

  1. MATCH
  2. {
  3. [class: <class>],
  4. [as: <alias>],
  5. [where: (<whereCondition>)]
  6. }
  7. .<functionName>(){
  8. [class: <className>],
  9. [as: <alias>],
  10. [where: (<whereCondition>)],
  11. [while: (<whileCondition>)]
  12. [maxDepth: <number>]
  13. }*
  14. RETURN <alias> [, <alias>]*
  15. LIMIT <number>
  • <class> Defines a valid target class.
  • as: <alias> Defines an alias for a node in the pattern.
  • <whereCondition> Defines a filter condition to match a node in the pattern. It supports the normal SQL WHERE clause. You can also use the $currentMatch and $matched context variables.
  • <functionName> Defines a graph function to represent the connection between two nodes. For instance, out(), in(), outE(), inE(), etc.
  • <whileCondition> Defines a condition that the statement must meet to allow the traversal of this path. It supports the normal SQL WHERE clause. You can also use the $currentMatch, $matched and $depth context variables. For more information, see Deep Traversal While Condition, below.
  • <maxDepth> Defines the maximum depth for this single path.
  • RETURN <alias> Defines elements in the pattern that you want returned. It can use one of the following:
    • Aliases defined in the as: block.
    • $matches Indicating all defined aliases.
    • $paths Indicating the full traversed paths.

BNF Syntax

  1. MatchStatement := ( <MATCH> MatchExpression ( <COMMA> MatchExpression )* <RETURN> Identifier ( <COMMA> Identifier )* ( Limit )? )
  2. MatchExpression := ( MatchFilter ( ( MatchPathItem | MultiMatchPathItem ) )* )
  3. MatchPathItem := ( MethodCall ( MatchFilter )? )
  4. MatchPathItemFirst := ( FunctionCall ( MatchFilter )? )
  5. MultiMatchPathItem := ( <DOT> <LPAREN> MatchPathItemFirst ( MatchPathItem )* <RPAREN> ( MatchFilter )? )
  6. MatchFilter := ( <LBRACE> ( MatchFilterItem ( <COMMA> MatchFilterItem )* )? <RBRACE> )
  7. MatchFilterItem := ( ( <CLASS> <COLON> Expression ) | ( <AS> <COLON> Identifier ) | ( <WHERE> <COLON> <LPAREN> ( WhereClause ) <RPAREN> ) | ( <WHILE> <COLON> <LPAREN> ( WhereClause ) <RPAREN> ) | ( <MAXDEPTH> <COLON> Integer ) )

Examples

The following examples are based on this sample data-set from the class People:

Match - 图1

Match - 图2

  • Find all people with the name John:

    1. orientdb> MATCH {class: Person, as: people, where: (name = 'John')}
    2. RETURN people
    3.  
    4. ---------
    5. people
    6. ---------
    7. #12:0
    8. #12:1
    9. ---------
  • Find all people with the name John and the surname Smith:

    1. orientdb> MATCH {class: Person, as: people, where: (name = 'John' AND
    2. surname = 'Smith')} RETURN people
    3.  
    4. -------
    5. people
    6. -------
    7. #12:1
    8. -------
  • Find people named John with their friends:

    1. orientdb> MATCH {class: Person, as: person, where:
    2. (name = 'John')}.both('Friend') {as: friend}
    3. RETURN person, friend
    4.  
    5. --------+---------
    6. people | friend
    7. --------+---------
    8. #12:0 | #12:1
    9. #12:0 | #12:2
    10. #12:0 | #12:3
    11. #12:1 | #12:0
    12. #12:1 | #12:2
    13. --------+---------
  • Find friends of friends:

    1. orientdb> MATCH {class: Person, as: person, where: (name = 'John' AND
    2. surname = 'Doe')}.both('Friend').both('Friend')
    3. {as: friendOfFriend} RETURN person, friendOfFriend
    4.  
    5. --------+----------------
    6. people | friendOfFriend
    7. --------+----------------
    8. #12:0 | #12:0
    9. #12:0 | #12:1
    10. #12:0 | #12:2
    11. #12:0 | #12:3
    12. #12:0 | #12:4
    13. --------+----------------
  • Find people, excluding the current user:

    1. orientdb> MATCH {class: Person, as: person, where: (name = 'John' AND
    2. surname = 'Doe')}.both('Friend').both('Friend'){as: friendOfFriend,
    3. where: ($matched.person != $currentMatch)}
    4. RETURN person, friendOfFriend
    5.  
    6. --------+----------------
    7. people | friendOfFriend
    8. --------+----------------
    9. #12:0 | #12:1
    10. #12:0 | #12:2
    11. #12:0 | #12:3
    12. #12:0 | #12:4
    13. --------+----------------
  • Find friends of friends to the sixth degree of separation:

    1. orientdb> MATCH {class: Person, as: person, where: (name = 'John' AND
    2. surname = 'Doe')}.both('Friend'){as: friend,
    3. where: ($matched.person != $currentMatch) while: ($depth < 6)}
    4. RETURN person, friend
    5.  
    6. --------+---------
    7. people | friend
    8. --------+---------
    9. #12:0 | #12:0
    10. #12:0 | #12:1
    11. #12:0 | #12:2
    12. #12:0 | #12:3
    13. #12:0 | #12:4
    14. --------+---------
  • Finding friends of friends to six degrees of separation, since a particular date:

    1. orientdb> MATCH {class: Person, as: person,
    2. where: (name = 'John')}.(bothE('Friend'){
    3. where: (date < ?)}.bothV()){as: friend,
    4. while: ($depth < 6)} RETURN person, friend

    In this case, the condition $depth < 6 refers to traversing the block bothE('Friend') six times.

  • Find friends of my friends who are aslo my friends, using multiple paths:

    1. orientdb> MATCH {class: Person, as: person, where: (name = 'John' AND
    2. surname = 'Doe')}.both('Friend').both('Friend'){as: friend},
    3. { as: person }.both('Friend'){ as: friend }
    4. RETURN person, friend
    5.  
    6. --------+--------
    7. people | friend
    8. --------+--------
    9. #12:0 | #12:1
    10. #12:0 | #12:2
    11. --------+--------

    In this case, the statement matches two expression: the first to friends of friends, the second to direct friends. Each expression shares the common aliases (person and friend). To match the whole statement, the result must match both expressions, where the alias values for the first expression are the same as that of the second.

  • Find common friends of John and Jenny:

    1. orientdb> MATCH {class: Person, where: (name = 'John' AND
    2. surname = 'Doe')}.both('Friend'){as: friend}.both('Friend')
    3. {class: Person, where: (name = 'Jenny')} RETURN friend
    4.  
    5. --------
    6. friend
    7. --------
    8. #12:1
    9. --------

    The same, with two match expressions:

    1. orientdb> MATCH {class: Person, where: (name = 'John' AND
    2. surname = 'Doe')}.both('Friend'){as: friend},
    3. {class: Person, where: (name = 'Jenny')}.both('Friend')
    4. {as: friend} RETURN friend

Context Variables

When running these queries, you can use any of the following context variables:

Variable Description
$matched Gives the current matched record. You must explicitly define the attributes for this record in order to access them. You can use this in the where: and while: conditions to refer to current partial matches or as part of the RETURN value.
$currentMatch Gives the current complete node during the match.
$depth Gives the traversal depth, following a single path item where a while: condition is defined.

Use Cases

Expanding Attributes

You can run this statement as a sub-query inside of another statement. Doing this allows you to obtain details and aggregate data from the inner SELECT query.

  1. orientdb> SELECT person.name AS name, person.surname AS surname,
  2. friend.name AS friendName, friend.surname AS friendSurname
  3. FROM (MATCH {class: Person, as: person,
  4. where: (name = 'John')}.both('Friend'){as: friend}
  5. RETURN person, friend)
  6.  
  7. --------+----------+------------+---------------
  8. name | surname | friendName | friendSurname
  9. --------+----------+------------+---------------
  10. John | Doe | John | Smith
  11. John | Doe | Jenny | Smith
  12. John | Doe | Frank | Bean
  13. John | Smith | John | Doe
  14. John | Smith | Jenny | Smith
  15. --------+----------+------------+---------------

Incomplete Hierarchy

Consider building a database for a company that shows a hierarchy of departments within the company. For instance,

  1. [manager] department
  2. (employees in department)
  3. [m0]0
  4. (e1)
  5. / \
  6. / \
  7. / \
  8. [m1]1 [m2]2
  9. (e2, e3) (e4, e5)
  10. / \ / \
  11. 3 4 5 6
  12. (e6) (e7) (e8) (e9)
  13. / \
  14. [m3]7 8
  15. (e10) (e11)
  16. /
  17. 9
  18. (e12, e13)

This loosely shows that,

  • Department 0 is the company itself, manager 0 (m0) is the CEO
  • e10 works at department 7, his manager is m3
  • e12 works at department 9, this department has no direct manager, so e12‘s manager is m3 (the upper manager)

In this case, you would use the following query to find out who’s the manager to a particular employee:

  1. orientdb> SELECT EXPAND(manager) FROM (MATCH {class:Employee,
  2. where: (name = ?)}.out('WorksAt').out('ParentDepartment')
  3. {while: (out('Manager').size() == 0),
  4. where: (out('Manager').size() > 0)}.out('Manager')
  5. {as: manager} RETURN manager)

Deep Traversal

Match path items act in a different manners, depending on whether or not you use while: conditions in the statement.

For instance, consider the following graph:

  1. [name='a'] -FriendOf-> [name='b'] -FriendOf-> [name='c']

Running the following statement on this graph only returns b:

  1. orientdb> MATCH {class: Person, where: (name = 'a')}.out("FriendOf")
  2. {as: friend} RETURN friend
  3.  
  4. --------
  5. friend
  6. --------
  7. b
  8. --------

What this means is that it traverses the path item out("FriendOf") exactly once. It only returns the result of that traversal.

If you add a while condition:

  1. orientdb> MATCH {class: Person, where: (name = 'a')}.out("FriendOf")
  2. {as: friend, while: ($depth <= 1)} RETURN friend
  3.  
  4. ---------
  5. friend
  6. ---------
  7. a
  8. b
  9. ---------

Including a while: condition on the match path item causes OrientDB to evaluate this item as zero to n times. That means that it returns the starting node, (a, in this case), as the result of zero traversal.

To exclude the starting point, you need to add a where: condition, such as:

  1. orientdb> MATCH {class: Person, where: (name = 'a')}.out("FriendOf")
  2. {as: friend, while: ($depth <= 1) where: ($depth > 0)}
  3. RETURN friend

As a general rule,

  • while Conditions: Define this if it must execute the next traversal, (it evaluates at level zero, on the origin node).
  • where Condition: Define this if the current element, (the origin node at the zero iteration the right node on the iteration is greater than zero), must be returned as a result of the traversal.

For instance, suppose that you have a genealogical tree. In the tree, you want to show a person, grandparent and the grandparent of that grandparent, and so on. The result: saying that the person is at level zero, parents at level one, grandparents at level two, etc., you would see all ancestors on even levels. That is, level % 2 == 0.

To get this, you might use the following query:

  1. orientdb> MATCH {class: Person, where: (name = 'a')}.out("Parent")
  2. {as: ancestor, while: (true) where: ($depth % 2 = 0)}
  3. RETURN ancestor

Best practices

Queries can involve multiple operations, based on the domain model and use case. In some cases, like projection and aggregation, you can easily manage them with a SELECT query. With others, such as pattern matching and deep traversal, MATCH statements are more appropriate.

Use SELECT and MATCH statements together (that is, through sub-queries), to give each statement the correct responsibilities. Here,

Filtering Record Attributes for a Single Class

Filtering based on record attributes for a single class is a trivial operation through both statements. That is, finding all people named John can be written as:

  1. orientdb> SELECT FROM Person WHERE name = 'John'

You can also write it as,

  1. orientdb> MATCH {class: Person, as: person, where: (name = 'John')}
  2. RETURN person

The efficiency remains the same. Both queries use an index. With SELECT, you obtain expanded records, while with MATCH, you only obtain the Record ID’s.

Filtering on Record Attributes of Connected Elements

Filtering based on the record attributes of connected elements, such as neighboring vertices, can grow trick when using SELECT, while with MATCH it is simple.

For instance, find all people living in Rome that have a friend called John. There are three different ways you can write this, using SELECT:

  1. orientdb> SELECT FROM Person WHERE BOTH('Friend').name CONTAINS 'John'
  2. AND out('LivesIn').name CONTAINS 'Rome'
  3.  
  4. orientdb> SELECT FROM (SELECT BOTH('Friend') FROM Person WHERE name
  5. 'John') WHERE out('LivesIn').name CONTAINS 'Rome'
  6.  
  7. orientdb> SELECT FROM (SELECT in('LivesIn') FROM City WHERE name = 'Rome')
  8. WHERE BOTH('Friend').name CONTAINS 'John'

In the first version, the query is more readable, but it does not use indexes, so it is less optimal in terms of execution time. The second and third use indexes if they exist, (on Person.name or City.name, both in the sub-query), but they’re harder to read. Which index they use depends only on the way you write the query. That is, if you only have an index on City.name and not Person.name, the second version doesn’t use an index.

Using a MATCH statement, the query becomes:

  1. orientdb> MATCH {class: Person, where: (name = 'John')}.both("Friend")
  2. {as: result}.out('LivesIn'){class: City, where: (name = 'Rome')}
  3. RETURN result

Here, the query executor optimizes the query for you, choosing indexes where they exist. Moreover, the query becomes more readable, especially in complex cases, such as multiple nested SELECT queries.

TRAVERSE Alternative

There are similar limitations to using TRAVERSE. You may benefit from using MATCH as an alternative.

For instance, consider a simple TRAVERSE statement, like:

  1. orientdb> TRAVERSE out('Friend') FROM (SELECT FROM Person WHERE name = 'John')
  2. WHILE $depth < 3

Using a MATCH statement, you can write the same query as:

  1. orientdb> MATCH {class: Person, where: (name = 'John')}.both("Friend")
  2. {as: friend, while: ($depth < 3)} RETURN friend

Consider a case where you have a since date property on the edge Friend. You want to traverse the relationship only for edges where the since value is greater than a given date. In a TRAVERSE statement, you might write the query as:

  1. orientdb> TRAVERSE bothE('Friend')[since > date('2012-07-02', 'yyyy-MM-dd')].bothV()
  2. FROM (SELECT FROM Person WHERE name = 'John') WHILE $depth < 3

Unforunately, this statement DOESN”T WORK in the current release. However, you can get the results you want using a MATCH statement:

  1. orientdb> MATCH {class: Person, where: (name = 'John')}.(bothE("Friend")
  2. {where: (since > date('2012-07-02', 'yyyy-MM-dd'))}.bothV())
  3. {as: friend, while: ($depth < 3)} RETURN friend

Projections and Grouping Operations

Projections and grouping operations are better expressed with a SELECT query. If you need to filter and do projection or aggregation in the same query, you can use SELECT and MATCH in the same statement.

This is particular important when you expect a result that contains attributes from different connected records (cartesian product). FOr instance, to retrieve names, their friends and the date since they became friends:

  1. orientdb> SELECT person.name AS name, friendship.since AS since, friend.name
  2. AS friend FROM (MATCH {class: Person, as: person}.bothE('Friend')
  3. {as: friendship}.bothV(){as: friend,
  4. where: ($matched.person != $currentMatch)}
  5. RETURN person, friendship, friend)