添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
暂无图片
暂无图片
暂无图片
暂无图片

再续前缘,Neo4j的高级语法

六只栗子 2022-11-17
315

哈啰, 各位小伙伴们,这里是每天进步一点点的花栗鼠小K

上期小K介绍了Neo4j的基础语法,本期小K想带领各位小伙伴上一个台阶,所以在这一期,我们将继续从Neo4j讲起,来介绍一些高级语法。

CQL 高级语法

  1. case 语句

    case
    条件表达式有两种,一种类似于 if/else
    ,另一种类似于 case/when

    if/else

    return
    case
    when 1=2 then 1
    when 2=2 then 2
    when 3=3 then 3
    else 4
    end

    做区间判断或者需要比较不同的字段的时候使用

    case/when 型

    return
    case 3
    when 2 then 2
    when 3 then 3
    when 4 then 4
    else 0
    end

    在使用的时候,我们常常通过match找到某个节点,对某个属性字段做不同处理

  2. 数学函数

    ABS( expression )
    返回expression得到的数值的绝对值

    ROUND( expression )
    取整函数:返回小于等于expression得到的数值的最大整数(还是返回离expression得到的数值最近的整数??)

    SQRT( expression )
    返回expression得到的数值的平方根

    SIGN( expression )
    符号函数:如果expression得到的数值,为0则返回0;为负数则返回-1;为正数则返回1

  3. 集合函数

    NODES( path )
    返回一个路径的所有节点

    MATCH (catelyn:Character {name: "Catelyn"}), (drogo:Character {name: "Drogo"}),p=shortestPath((catelyn)-[INTERACTS*]-(drogo))
    RETURN nodes(p)

    RELATIONSHIPS( path )
    返回一个路径的所有关系

    MATCH (catelyn:Character {name: "Catelyn"}), (drogo:Character {name: "Drogo"}),p=shortestPath((catelyn)-[INTERACTS*]-(drogo))
    RETURN RELATIONSHIPS(p)

    EXTRACT( identifier in collection | expression )
    返回一个结果集合:对集合(collection)的所有元素执行expression的操作得到的结果

    MATCH (catelyn:Character {name: "Catelyn"}), (drogo:Character {name: "Drogo"}),p=shortestPath((catelyn)-[INTERACTS*]-(drogo))
    with nodes(p) as x
    RETURN extract(xi in x | xi.islive=false)

    FILTER(identifier in collection WHERE predicate)
    返回集合(collection)中所有满足断言(predicate)的元素组成的集合

    MATCH (catelyn:Character {name: "Catelyn"}), (drogo:Character {name: "Drogo"}),p=shortestPath((catelyn)-[INTERACTS*]-(drogo))
    with nodes(p) as x

    RETURN extract(xi in x where xi.islive=false)

    ALL(identifier in collection WHERE predicate)
    当集合(collection)中所有元素满足断言(predicate) ,返回true

    MATCH (catelyn:Character {name: "Catelyn"}), (drogo:Character {name: "Drogo"}),p=shortestPath((catelyn)-[INTERACTS*]-(drogo))
    with nodes(p) as x
    with all(xi in x where xi.islive=false) as result
    RETURN result

    catelyn、drogo分别对应name为Catelyn、Drogo的节点的别名

    ANY(identifier in collection WHERE predicate)
    当集合(collection)中任意一个元素满足断言(predicate) ,返回true

    MATCH (catelyn:Character {name: "Catelyn"}), (drogo:Character {name: "Drogo"}),p=shortestPath((catelyn)-[INTERACTS*]-(drogo))
    with nodes(p) as x
    with any(xi in x where xi.islive=false) as result
    RETURN result

    NONE(identifier in collection WHERE predicate)
    当集合(collection)中没有一个元素满足断言(predicate) ,返回true

    MATCH (catelyn:Character {name: "Catelyn"}), (drogo:Character {name: "Drogo"}),p=shortestPath((catelyn)-[INTERACTS*]-(drogo))
    with nodes(p) as x
    with none(xi in x where xi.islive=false) as result
    RETURN result

    SINGLE(identifier in collection WHERE predicate)
    如果集合(collection)里的只有一个元素满足断言(predicate)则返回true

    MATCH (catelyn:Character {name: "Catelyn"}), (drogo:Character {name: "Drogo"}),p=shortestPath((catelyn)-[INTERACTS*]-(drogo))
    with nodes(p) as x
    with SINGLE(xi in x where xi.islive=true) as result
    RETURN result

    TAIL( expression )
    返回集合中除了第一个之外的所有元素

    RANGE( start, end [, step] )
    返回从start开始,end结束(闭区间)内步长为step(非0)的所有整数数字

  4. 列表

    CQL
    语言中的列表和 Python 中的列表很相似都使用 []
    ,都可以包含不同的元素,直接使用 return
    创建

  • range

    用法 range(start, end, step)

  • 列表索引

    下标索引:RETURN[[1,2,3,4][1] ][] 返回2

    范围索引:RETURN [[1,2,3,4]][[1..3][1..3]  //返回[2,3],注意返回的是列表,不包括4

    范围索引:RETURN [1,2,3,4][-2..]   返回[3,4]

    越界索引:RETURN [1,2,3,4][2..9]  //只返回[3,4]

    越界索引:RETURN [1,2,3,4][7..9]  //返回一个空列表 []

    越界索引:RETURN [1,2,3,4][9]      //单个元素越界索引,返回 null

  • 列表推导

    列表推导是 Cypher 基于已有列表创建新的列表,可以和 where 等语句一起使用。功能类似于 Python 的 map 操作,也类似于 lambda 表达式,形式看起来和 linux 管道符号 |
    一样,作用也类似,前面的输出作为后面的输入。

    RETURN [x in range(1,3) | x^2]

  • 列表函数

    reverse
    :将list进行反转

    tail
    :列表的第一个元素默认为 head, tail
    函数输出除了头意外的其余函数

  • 特殊查询

    • 省略关系

      match(n)-->(m)

      两个 -
      表示省略任意关系

      match(a:Man)-->(b:Woman)
      return a,b

    • 匹配多种关系类型

      match(n)->[:A|:B]->(m)

      匹配A或者B类型的关系

      match(a:Man)-[:I_says|:Marriage]->(b:Woman)
      return a,b

    • 带有空格的关系

      match(n)-[ A B
      ]->(m)

      match(a:Man)-[`I_says Marriage`]->(b:Woman)
      return a,b

    • 变长关系

      match(n)-[r:Relationship*a..b]-(m)

      match(n:Character{name:"Eddard"})-[r:INTERACTS*1..2]-(m:Character)
      RETURN m.name

    • 最短路径

      match(n),(m),p=shortestPath((n)-[*..N]-(m))  return p

      查找 n,m 之间的最短路径,路径最大长度为N

      MATCH (catelyn:Character {name: "Catelyn"}), (drogo:Character {name: "Drogo"}), p=shortestPath((catelyn)-[INTERACTS*..3]-(drogo))
      RETURN p

      MATCH (catelyn:Character {name: "Catelyn"}), (drogo:Character {name: "Drogo"}),p=shortestPath((catelyn)-[INTERACTS*]-(drogo))
      RETURN p

    • 所有最短路径

      match(n),(m),p=allShortestPaths((n)-[*]-(m))

      查找n,m之间所有最短路径

      MATCH (catelyn:Character {name: "Catelyn"}), (drogo:Character {name: "Drogo"}),p=allShortestPaths((catelyn)-[*]-(drogo))
      RETURN p

    • id 查询

      match(n) where id(n)=1 return n

    • OPTIONAL MATCH

      可选择的 match

      match(n:Person{name:"p1"}),(m:Person{name:"p2"})   return n,m

      MATCH (catelyn:Character {name: "Catelyn"}), (drogo:Character {name: "Drogo"})
      return catelyn,drogo


      MATCH (catelyn:Character {name: "Catelyn"}), (drogo:Character {name: "Loken"})
      return catelyn,drogo

      加入 OPTIONAL
      之后

      MATCH (catelyn:Character {name: "Catelyn"})
      optional match (drogo:Character {name: "Loken"})
      return catelyn,drogo

      但还是有一定的要求, match
      必须有东西

      optional MATCH (catelyn:Character {name: "Loken"})
      optional match (drogo:Character {name: "Drogo"})
      return catelyn,drogo

    • where 标签过滤

      match(n:Person) return n