Updated on 2024-05-23 GMT+08:00

Basic Operations and Compatibility

Basic Operations

Operation

Cypher Statement

Querying vertices

match (n) return n

Querying edges

match (n)-[r]->(m) return n, r, m

Querying paths

match (n:user)-[r]->(m:movie)-->(s:series) return n,r,m,s

Querying information by specifying filtering criteria

match(n:user) where n.userid>=5 return n

Grouping and aggregating

match(n:movie) return n.genres, count(*)

Deduplicating

match(n:movie) return distinct n.genres

Sorting

match(n:movie) return n order by n.movieid

Creating a vertex

create (n:user{userid:1}) return n

Creating an edge

match (n:user{userid:15}),(m:movie{movieid:10}) create (n)-[r:rate]->(m)

Deleting a vertex

match (n:user{userid:1}) delete n

Modifying labels

match (n:user{userid:1}) set n:movie return n

Modifying properties

match (n:user{userid:1}) set n.userid=2 return n

Compatibility to Cypher

  1. Cypher clauses

    Cypher implements a couple of clauses. You can combine clauses to implement different query semantics, including vertex and edge filtering, multi-hop query, sorting and deduplication, and grouping and aggregation.

    Currently, GES supports the Cypher clauses listed in the following table.

    Table 1 Supported Cypher clauses

    Clause

    Support

    Example

    match

    Partially supported

    match (n:movie) return n

    optional match

    Partially supported

    optional match (n)-->(m) where id(n)='1' return m

    return

    Supported

    return [1,2,3] as p

    with

    Supported

    match (n) with labels(n) as label, count(*) as count

    where count > 10 return *

    where

    Supported

    match (n:movie) where n.movieid > 10 return n

    order by

    Supported

    match (n:movie) return n order by n.genres

    skip

    Supported

    match (n:movie) return n order by n.genres skip 5

    limit

    Supported

    match (n:movie) return n order by n.genres skip 5 limit 10

    create

    Supported

    create (n:user{_ID_: 'Jack' }) return n

    delete

    Supported

    match (n:movie)<-[r]-(m:user) delete r

    set

    Supported

    match (n:user{userid:0}) set n.gender='M' return n

    call procedures

    Supported

    call db.schema()

    unwind

    Supported

    unwind [1, 2, 3] as p return p

    union

    Supported

    match (n:movie) return id(n) union match (n:user) return id(n)

    NOTE:

    Union is available for graphs smaller than 10 billion edges only.

    1. Currently, merge and foreach operations are not supported. Cypher statements cannot add or delete indexes.
    2. GES metadata is not schema-free, and the vertex and edge label properties are strictly restricted. Therefore, the remove operation is not supported.
    3. The order by clause does not support sorting of the list type. When Cardinality of the property value is not single, the sorting result is unknown.
    • Available items for the match clause

      Item

      Description

      Example Clauses

      Earliest Version Required

      Vertex pattern

      Patterns for matching vertex with specified labels, properties, and IDs.

      match (n:movie{title:'hello'})

      match (n) where id(n)='xx'

      2.2.16

      Edge pattern

      Patterns for matching directional and non-directional edges with specified labels and properties. Specified IDs of both start and end vertices are supported.

      match (n)-[r] -> (m)

      match (n)-[r]- (m)

      match (n)-[r:rate{Rating:1}] - (m)

      match (n)-[r]- (m) where id(n)='x'and id(m)='y'

      2.2.16

      Path

      Anonymous paths

      match (n)-[r]->(m)-->(s)

      2.2.16

      Named paths

      match p=(n)-[r]->(m)-->(s)

      2.2.19

      Multiple patterns

      You can enter multiple patterns after match and separate them with commas (,).

      match (n)-[r]->(m), (m)-->(s)

      2.2.16

      Multi-match

      You can enter multiple match clauses. You can use with to connect multiple clauses.

      match (n)-[r]->(m) with m match (m)-->(s)

      2.2.16

      Variable-length path pattern

      Patterns for matching variable-length paths starting with a specified vertex.

      match p=(n)-[r*1..3]->(m) where id(n)='xx'return p

      match p=(n{title:'name'})-[r*1..3]->(m) return p

      2.2.19

      Traversal conditions for matching variable-length paths.

      match p=(n)-[r*1..3]->(m) where id(n)='xx'and all (x in nodes(p) where x.prop='value1') return p

      2.2.28

      Both start vertex and end vertex of a variable-length path can be specified.

      match p=(n)-[r*1..3]->(m) where id(n)='xx' and id(m)='y' return p

      2.3.9

      Deduplication by end vertex is supported:

      match p=(n)-[r*1..3]->(m) where id(n)='xx' return distinct m

      2.3.17

  2. Parameterized queries

    Cypher supports parameterized queries. Numeric and string values in a query statement are extracted and converted to parameters for faster compilation, improving the query speed.

    There are some examples of parameterized queries:

    • Example 1
      POST http://{SERVER_URL}/ges/v1.0/{project_id}/graphs/{graph_name}/action?action_id=execute-cypher-query
      {
               "statements": [{
                         "statement": " match (n:user) where n.occupation = $occupation return n",
                         "parameters": {
                               "occupation" : "artist"
                          },
                         "resultDataContents": ["row"]
               }]
      }
    • Example 2
      POST http://{SERVER_URL}/ges/v1.0/{project_id}/graphs/{graph_name}/action?action_id=execute-cypher-query
      {
               "statements": [{
                         "statement": " match (n:user {`Zip-code`:'98133'}) set n = $props return n",
                         "parameters": {
                                 "props": {
                                 "gender": "M",
                                 "age": "56+"
                              }
                          },
                         "resultDataContents": ["row"]
               }]
      }

    There are some scenarios where parameterized queries are not supported. The following syntax is not valid:

    1. Using $param to search by property key and value. For example, match (n) where n.$param = 'something'
    2. Using $code for vertex and edge labels. For example, match (n:user) set n:$code
  3. Supported data types

    Currently, GES supports 10 data types: char, char_array, float, double, Boolean, long, Integer, date, enum, and string. Both Boolean and numeric types are supported in the Cypher syntax. The following table lists the mapping between other types and Cypher data types.

    Table 2 Mapping between data types of GES and Cypher

    GES

    Cypher

    Description

    char

    String

    -

    char_array

    String

    -

    string

    String

    -

    enum

    String

    The Cypher syntax does not provide enum-related syntax. During Cypher query, an enum is converted to a string. When Cypher is used to set properties, values that are not in the enumeration list fail to be set.

    date

    Temporal

    Currently, Cypher dates can be converted into GES dates, but Cypher date functions cannot be used for inputting a date.

    Table 3 Special types supported by Cypher

    Type

    Supported

    Example

    Node

    Yes

    match (n) return n limit 10

    Relationship

    Yes

    match (n)-[r]->(m) return r limit 10

    List

    Yes

    return [1,2,3] as li

    Map

    Yes

    match (n)-->(m) return {start:id(n), end:id(m)}

    Path

    Yes

    match p=(n1)-[:friends*1..2]-(n2) return p limit 10

    Point, Spatial

    No

    -

    For the special types listed above, only the List type can be used to match multi-value properties in GES. Other types cannot be used in a set statement for setting the value of a property.

  4. Vertex ID compatibility
    • Cypher does not provide the syntax for setting the ID when a vertex is added. In GES, however, an ID of the string type is required to uniquely identify a vertex. To use the Cypher syntax in GES, add _ID_ to specify the ID of a vertex in the create statement. For example, the create(n{_ID_:'123456'}) statement creates a vertex whose ID is 123456.
    • If the ID is not specified, a random ID is generated for the vertex.

      The _ID_ identifier is supported only in the create statement. The match and set clauses do not support the _ID_ identifier. In the match clause, you can use the id() function to obtain the vertex ID.