更新时间:2023-11-27 GMT+08:00
分享

基本操作和兼容性

基本操作

操作名

Cypher语句

查点

match (n) return n

查边

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

查路径

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

过滤查询

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

分组聚集

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

去重

match(n:movie) return distinct n.genres

排序

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

创建点

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

创建边

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

删除点

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

更改标签

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

更改属性

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

Cypher实现的兼容性

  1. Cypher支持的子句列表

    Cypher实现了若干子句,通过对子句进行组合可以实现丰富的查询语义,进而完成点边过滤、多跳查询、排序去重、分组聚集等诸多能力。

    目前GES支持的Cypher子句如下:

    表1 Cypher支持的子句清单

    子句

    支持情况

    举例

    match

    部分支持

    match (n:movie) return n

    optional match

    部分支持

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

    return

    支持

    return [1,2,3] as p

    with

    支持

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

    where count > 10 return *

    where

    支持

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

    order by

    支持

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

    skip

    支持

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

    limit

    支持

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

    create

    支持

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

    delete

    支持

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

    set

    支持

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

    call procedures

    支持

    call db.schema()

    unwind

    支持

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

    union

    支持

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

    说明:

    union仅在百亿以下规格图中支持(不包含百亿)。

    1. 目前暂不支持merge、foreach等操作,暂不支持使用Cypher语句增删索引。
    2. 由于GES的元数据不是Schema Free的,点边label属性等有严格的限制,因此不支持Remove操作。
    3. Order by子句不支持List类型的排序,当属性值的Cardinality不为single时,排序结果未知。
    • match子句支持情况

      特性

      描述

      子句示例

      最低版本要求

      点Pattern

      支持基于label/属性过滤、id过滤的点pattern

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

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

      2.2.16

      边Pattern

      支持有方向、无方向、带label/属性过滤的边pattern,支持指定两端id进行边查询

      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

      路径

      支持匿名路径

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

      2.2.16

      支持命名路径

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

      2.2.19

      多Pattern

      支持match后输入多个pattern,以逗号隔开:

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

      2.2.16

      多Match

      支持输入多个match子句,多个match间可以使用with连接:

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

      2.2.16

      可变长路径Pattern

      支持从单点出发查询可变长路径:

      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

      支持可变长路径查询时指定遍历条件:

      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

      支持同时指定可变长路径起点和终点:

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

      2.3.9

      不支持根据终点去重:

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

      暂不支持

  2. 参数化查询支持

    Cypher支持参数化的查询。通过把查询语句中的数值、字符串等值类型提取为参数,加速查询的编译时间,提高查询速度。

    以下提供几种参数化查询的示例:

    • 参数化查询请求示例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"]
               }]
      }
    • 参数化查询请求示例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"]
               }]
      }

    参数化查询不适用于以下场景,下列查询语句均无法正常执行:

    1. 属性键值,如:match (n) where n.$param = 'something'
    2. 点边标签,如:match (n:user) set n:$code
  3. 数据类型支持

    GES目前支持char、char_array、float、double、Boolean、long、Integer、date、enum、string共10种数据类型,布尔型和数值型在Cypher语法中都能得到支持,其他类型和Cypher存在如下的映射关系,在GES内部实现了类型的转换:

    表2 GES和Cypher的类型映射关系

    GES类型

    Cypher类型

    备注

    char

    String

    -

    char_array

    String

    -

    string

    String

    -

    date

    Temporal

    目前支持Date按GES的日期格式输入和输出,暂不支持调用Cypher日期函数输入日期。

    表3 Cypher特殊类型支持情况

    类型

    支持情况

    查询举例 & 备注

    Node

    支持

    match (n) return n limit 10

    Relationship

    支持

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

    List

    支持

    return [1,2,3] as li

    Map

    支持

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

    Path

    支持

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

    Point、Spatial

    暂不支持

    -

    表 Cypher特殊类型支持情况中提到的特殊类型,其中除了List用于匹配GES中的多值属性外,其他类型均无法通过set语句设为点边上某个属性的值。

  4. 点id的兼容性
    • Cypher添加点时不提供设置id的语法,GES中添加点需要一个字符串型的id,来唯一的标识一个点。为了兼容Cypher语法当前Create语句实现中,通过使用一个特殊的标识符_ID_指定点的id。例如create (n{_ID_:’123456’} )语句,会创建一个id为’123456’的点。
    • 若创建时未指明id,则系统为此点生成一个随机id。

      标识符“_ID_”仅在create语句中支持,match、set等子句均不支持_ID_标识。Match子句中可使用函数id()获取点id。

  5. 创建多标签点

    GES持久化版本支持通过Cypher创建多标签的点,例如create (n:user:student {userid:10, studentName:’Bob’})

    所有标签的属性放在同一个大括号中。创建过程中会根据schema自动将标签与属性匹配,属性的顺序不会影响其与标签的对应关系。

分享:

    相关文档

    相关产品