see on GitHub

Graph Queries Expanded Search Patterns


Graph queries are modular structures that can be easily expanded from data-node to edge and from edge to node in order to reveal potential layers of meaningful data.
This article focuses on simple patterns, including directional and bi-directional queries.
Other types of patterns include multi-section and recursive queries.


Expanding The Search Pattern

You can expand an existing graph query simply by adding it an edge-and-node pair, as follows.

  • Initial query:
    Here we find all the orders made by a company we want to investigate, and all the products that each order includes.

    match
        (Orders as orders 
           where Company = "companies/34-A")-
        [Lines[].Product as product]->
        (Products as products)
    Initial Query Results

    Initial Query Results

  • Expanded query:
    And here we expand the query to also find each product's supplier.

    match
        (Orders as orders 
           where Company = "companies/34-A")-
        [Lines[].Product as product]->
        (Products as products)-
        [Supplier as supplier]->
        (Suppliers as suppliers)
    Expanded Query Results

    Expanded Query Results

    The expanded search pattern locates a new layer of relations and data nodes, by querying retrieved products and finding which suppliers they lead to.

Bi-Directional Queries

You can direct a graph query to the left and/or to the right.
  • Directional Queries
    In each section of the query, an origin data-node clause is directed to a destination data-node clause to its right or to its left.
    The following two queries for example, searching for orders to France, are equivalent.

    To The Right To The Left
    match
         (Orders as orders 
            where ShipTo.Country = "France")-
         [Company as company]->
         (Companies as companies)
    match 
         (Companies as companies)
         <-[Company as company]
         -(Orders as orders 
             where ShipTo.Country = "France")
  • Bi-Directional Queries
    A query may be combined of sections that point to the right and to the left.
    Here is one:

    match 
         (Employees as employees)
         <-[Employee as handledBy]
         -(Orders as orders)-
         [Company as orderedBy]->
         (Companies as companies)
    • The first part heads left, finding orders and the employees that handle them.
      (Employees as employees)
           <-[Employee as handledBy]
           -(Orders as orders)
    • The second part heads right, finding orders and the companies that make these orders.
      (Orders as orders)-
           [Company as orderedBy]->
           (Companies as companies)
    • Orders, common to both parts, appears only once.
      It is origin for both sections.

    The query produces these results:

    Bi-Directional Query Results

    Bi-Directional Query Results

  • It is equivalent to the following less concise multi-segment query.

    Multi-Segment (Intersection) Bi-Directional
    match 
         //First Segment
         (Orders as orders)- 
           [Employee as employee]-> 
             (Employees as employees)
        and
         //Second Segment
         (orders)- 
           [Company as company]-> 
             (Companies as companies)
    match 
         (Employees as employees)
         <-[Employee as handledBy]
         -(Orders as orders)-
         [Company as orderedBy]->
         (Companies as companies)

Formatting a query as a bi-directional or a multi-segment structure is a readability preference.
Operation-wise the two formats are equivalent, since bi-directional queries are interpreted to single-direction multi-segment queries before execution.