Updated on 2023-03-03 GMT+08:00

Cypher Query

Scenario

Cypher is a declarative graph query language. You can use Cypher statements to obtain query result and modify data in GES.

Procedure

  1. Access the GES graph editor. For details, see Accessing the GES Graph Editor.
  2. Use label-based vertex and edge indexes during Cypher query.
    If this is your first time using Cypher, click Create Index in the upper right corner of the result display area. You do not need to perform this operation in subsequent operations.
    Figure 1 Result display area
  3. In the graph data query area, enter the query statement and press Enter.

Cypher Statements

The following are typical query statements.

  • Querying a vertex

    match (n:movie) return n: Query the vertex whose label is movie.

    match (n) return n limit 100: Query details about 100 vertices.

    match (n{Occupation:'artist'}) return id(n), n.Gender limit 100: Query the first 100 vertices whose Occupation is artist, and return their IDs and genders.

    match (n) where id(n)='Vivian' return n: Query the vertex whose ID is Vivian.

    match (n) return n skip 50 limit 100: Query all vertices of a graph. Skip the first 50 vertices, and return a total of 100 vertices.

  • Querying an edge

    match (n)-[r]->(m) return r, n, m: Query all edges. Return the edges and vertices at both ends.

    match (n)-[r:rate]->(m) return r, n, m: Query the edges whose label is rate.

    match (n)-[r:rate|:friends]-(m) where id(n)='Vivian' return n,r,m: Query all edges whose start vertex is Vivian and edge label is rate or friends.

  • Searching by path

    match p=(n:user)--(m1:user)--(m2:movie) return p limit 100: Query the paths whose start vertex is user, first-hop end vertex is user, and second-hop end vertex is movie. Returns the first 100 paths.

  • Aggregating and deduplicating based on groups

    match (n) return count(*): Query the number of all vertices in a graph.

    match (n:user) return n.Gender, count(n): Collect statistics on the number of user vertices in every gender.

    match (n:user) return distinct n.Occupation: Return deduplicated occupations of all user vertices.

  • Sorting

    match (n:user) return id(n) as name order by name: Change IDs of all user vertices to name, and sort the vertices by name.

  • Creating a vertex

    create(n:movie{_ID_:'The Captain', Year:2019})return n: Create a vertex whose ID is The Captain, label is movie, and Year is 2019. Return the vertex.

    create(n:movie{_ID_:'The Captain', Year:2019})-[r:rate]-> (m:movie{_ID_:'The Climbers',Title: 'The Climbers', Year:2019}) return r: Create two vertices and their associated edges.

  • Creating an edge

    match (n),(m) where id(n)= 'The Captain' and id(m)= 'Lethal Weapon' create (n)-[r:rate]->(m) return r : Create an edge whose label is rate between two vertices with specified IDs. (You are advised to use this query in 2.2.21 and later versions.)

  • Modifying properties

    match (n) where id(n)= 'The Captain' set n.Title= 'The Captain' return n: Search for the vertex whose ID is The Captain and change the attribute Title to Ji Zhang.

  • Deleting a vertex

    match (n) where id(n)=' The Captain' delete n: Search and delete the vertex whose ID is The Captain.

    match (n) where id(n)=' "detach delete n": Search for the vertex whose ID is The Captain. Delete the vertex and its edges.

  • Querying a schema

    If you call db.schema() independently, only the schema metadata of the vertices is returned. Multiple isolated vertices are displayed on the canvas.

  1. You can press the up and down arrow keys in the text box to view historical query commands.
  2. When you enter a syntax keyword, the system automatically displays historical statements with the same keyword.
    Figure 2 Historical queries
  3. Keywords in the text box are displayed in different colors.
    • Reserved words in gray

      Note: A reserved word is predefined in the syntax system of a programming language. Reserved words vary depending on programming languages.

    • String values in orange
    • Key-value pairs in purple. They are of the non-string type in the key:value format.
    • Delimiters in red. Regular delimiters including square brackets [], curly brackets {}, parenthesis (), commas (,), and semicolons (;).
    • Variables in green
    Figure 3 Cypher keywords