neo4j使用详解(十、cypher空间及向量函数——最全参考)

请添加图片描述


Neo4j系列导航:
neo4j安装及简单实践
cypher语法基础
cypher插入语法
cypher插入语法
cypher查询语法
cypher通用语法
cypher函数语法
neo4j索引及调优


8.空间函数

用于指定point坐标参考系 (CRS) 中的 2D 或 3D 值并计算两个point值之间的测地距离。

8.1.point.distance()函数

返回同一坐标参考系 (CRS) 中两点之间的测地距离 point.distance(point1, point2)

  • 注意:

    • 如果point值采用笛卡尔CRS(2D 或 3D)格式,则返回距离的单位将与使用毕达哥拉斯定理计算的点的单位相同。
    • 如果point值采用WGS-84 CRS (2D),则返回距离的单位将为米,基于球形地球近似的半正矢公式。
    • 如果point值采用WGS-84 CRS (3D),则返回距离的单位将为米。
  • 笛卡尔*CRS中两个 2D 点之间的距离:

    with point({x: 2.3, y: 4.5, crs: 'cartesian'}) AS p1, point({x: 1.1, y: 5.4, crs: 'cartesian'}) AS p2 point.distance(p1,p2) as dist //返回值1.5

  • WGS 84 CRS中两个 3D 点之间的距离:

    with point({longitude: 12.78, latitude: 56.7, height: 100}) as p1, point({latitude: 56.71, longitude: 12.79, height: 100}) as p2 return point.distance(p1, p2) as dist //返回值1269.9148706779097

  • 具体两个位置的距离:

    match (t:TrainStation)-[:TRAVEL_ROUTE]->(o:Office) with point({longitude: t.longitude, latitude: t.latitude}) as trainPoint, point({longitude: o.longitude, latitude: o.latitude}) as officePoint return round(point.distance(trainPoint, officePoint)) as travelDistance //返回值27842.0

8.2.point.withinBBox()函数

查看点是否在边框(边界)内。如果提供的点包含在边界框中(包含边界),则返回值为 true,否则返回值为 false point.withinBBox(point, lowerLeft, upperRight)

  • 检查笛卡尔CRS中的点是否包含在边界框中:

    with point({x: 0, y: 0, crs: 'cartesian'}) as lowerLeft, point({x: 10, y: 10, crs: 'cartesian'}) as upperRight return point.withinBBox(point({x: 5, y: 5, crs: 'cartesian'}), lowerLeft, upperRight) as result //返回值true

  • 查找边界框中包含的点:

    with point({longitude: 12.53, latitude: 55.66}) as lowerLeft, point({longitude: 12.614, latitude: 55.70}) as upperRight match (t:TrainStation) where point.withinBBox(point({longitude: t.longitude, latitude: t.latitude}), lowerLeft, upperRight) return count(t) //返回值1

  • 穿过 180 号子午线的边界框:

    with point({longitude: 179, latitude: 55.66}) as lowerLeft, point({longitude: -179, latitude: 55.70}) as upperRight return point.withinBBox(point({longitude: 180, latitude: 55.66}), lowerLeft, upperRight) as result //返回值true

8.3.point()- WGS 84 2D

返回WGS 84 CRS中对应给定坐标值的2D点 point({longitude | x, latitude | y [, crs][, srid]})

  • 注意:

    • latitude如果使用和指定坐标longitude,则crs或srid字段是可选的并推断为’WGS-84’( srid:4326)。
    • x如果使用和指定坐标y,则如果需要地理 CRS,则需要crs或srid字段
  • 实例:

    return point({longitude: 56.7, latitude: 12.78})//返回值point({srid:4326, x:56.7, y:12.78})
    return point({x: 2.3, y: 4.5, crs: 'WGS-84'}) //返回值point({srid:4326, x:2.3, y:4.5})
    match (p:Office) return point({longitude: p.longitude, latitude: p.latitude}) //返回值point({srid:4326, x:12.994341, y:55.611784})

8.3.point()- WGS 84 3D

返回WGS 84 point CRS中与给定坐标值对应的3D point({longitude | x, latitude | y, height | z, [, crs][, srid]})

  • 实例:

    return point({longitude: 56.7, latitude: 12.78, height: 8}) //返回值point({srid:4979, x:56.7, y:12.78, z:8.0})

8.3.point()- 笛卡尔二维

返回笛卡尔CRS中对应给定坐标值的2D点 point({x, y [, crs][, srid]})

  • 实例:

    return point({x: 2.3, y: 4.5}) as point //返回值point({srid:7203, x:2.3, y:4.5})

8.3.point()- 笛卡尔三维

返回笛卡尔CRS中对应给定坐标值的2D点 point({x, y, z, [, crs][, srid]})

  • 实例:

    RETURN point({x: 2.3, y: 4.5, z: 2}) as point //返回值point({srid:9157, x:2.3, y:4.5, z:2.0})

9.向量函数

向量函数允许您计算向量对的相似度分数。这些向量相似度函数与 Neo4j 的向量搜索索引所使用的函数相同。低版本不支持(3.x不支持,5.x版本是支持的)

9.1.vector.similarity.euclidean()函数

返回一个float表示基于欧几里得距离的参数向量之间的相似度。有关更多详细信息,请参阅向量索引文档vector.similarity.euclidean(a, b)

  • 注意:
    • 两个向量必须具有相同的维度
    • 就欧几里德相似性而言,两个向量都必须有效

9.2. vector.similarity.cosine()函数

vector.similarity.cosine()返回一个float表示参数向量之间基于余弦的相似度的值。有关更多详细信息,请参阅向量索引文档vector.similarity.cosine(a, b)

  • 注意:
    • 两个向量必须具有相同的维度
    • 两个向量在余弦相似度方面必须有效

9.3.实例

创建向量:

create
  (:Node {id:1, vector:[1.0,4.0,2.0]}),
  (:Node {id:2, vector:[3.0,-2.0,1.0]}),
  (:Node {id:3, vector:[2.0,8.0,3.0]})

定一个参数query(此处设置为[4.0, 5.0, 6.0]),您可以通过欧几里德距离查询该查询向量的两个最近邻。这是通过匹配所有候选向量并根据相似度分数排序来实现的:

MATCH (node:Node)
WITH node, vector.similarity.euclidean($query, node.vector) AS score
RETURN node, score
ORDER BY score DESCENDING
LIMIT 2;

结果:

node score
(:Node {vector: [2.0, 8.0, 3.0], id: 3}) 0.043478261679410934
(:Node {vector: [1.0, 4.0, 2.0], id: 1}) 0.03703703731298447

相关推荐

  1. neo4j-cypher语言使用

    2024-04-09 11:04:01       55 阅读
  2. neo4j查询语言Cypher详解(五)--apoc

    2024-04-09 11:04:01       60 阅读
  3. Neo4j图形数据库查询,Cypher语言详解

    2024-04-09 11:04:01       29 阅读
  4. neo4jCypher的语法记录

    2024-04-09 11:04:01       35 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-09 11:04:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-09 11:04:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-09 11:04:01       82 阅读
  4. Python语言-面向对象

    2024-04-09 11:04:01       91 阅读

热门阅读

  1. 起飞前的准备:轻松搭建VSCode与Python开发环境

    2024-04-09 11:04:01       34 阅读
  2. qt-4.8

    2024-04-09 11:04:01       40 阅读
  3. SpringBoot使用Websocket控制评测机

    2024-04-09 11:04:01       44 阅读
  4. 好用的前端框架及插件!!!

    2024-04-09 11:04:01       36 阅读
  5. MySQL-8. mysql索引

    2024-04-09 11:04:01       35 阅读
  6. Factory模式是什么呀

    2024-04-09 11:04:01       34 阅读