背景:需要对neo4j 查询结果,这里返回的是path做解析,返回json给前端,前端展示到页面,类似neo4j浏览器方式。

给出一个实体标签,及实体name,查询与该节点有一度关系的节点m,并且限制m的条数。

分析

  1.需要查询一度关系

  2.限制返回条数

  3.返回结果如何将path解析为json数据,方便前端展示

   

  cql: MATCH path =(n:`{en_label}`)--(m) WHERE n.{name}='{en_name}' with m.type as mtype,collect(path)[..{limit}] as paths return paths;

  使用占位符

    {en_label}:实体标签,如: 地点

    {en_name}:实体name,如:北京

    {limit}:m的条数

      with……这块是按 m.type(我这里定义的type就是m的实体标签),分组,然后每一组返回m条数据,比如 地点与公司和保险有关系,那公司和保险实体各返回m条path.

  解析path    # path=[[A-[r1]->B],[A-[r2]->C]]

 

data_array = []

for item in data:
    path_array = []
    for p in paths:
        startnode = {"properties": p.start_node, "identity": p.start_node.identity,"label": str(p.start_node['type'])}
     endnode = {"properties":p.end_node, "identity": p.end_node.identity,"label": str(p.end_node['type'])}
     relation = p.relationships[0]  
     rel = {"properties": relation, "startnode": relation.start_node.identity,"endnode": relation.end_node.identity}
     path_array.append({"start": startnode, "end": endnode, "relationship": rel})      
    data_array.extend(path_array)

返回结果

data_array:[{"start":{"properties":节点的属性,"identity":节点的唯一标识,"label":节点标签},"end":{"properties":节点的属性,"identity":节点的唯一标识,"label":节点标签},

        ,"relationship":{"properties":关系的属性,"startnode":关系的开始节点与identity 对应,来判断关系的方向,start和end本身不代表方向,"endnode":关系的结束节点,与identity对应与identity },

        {"start":{"properties":节点的属性,"identity":节点的唯一标识,"label":节点标签},"end":{"properties":节点的属性,"identity":节点的唯一标识,"label":节点标签},

        ,"relationship":{"properties":关系的属性,"startnode":关系的开始节点与identity 对应,来判断关系的方向,start和end本身不代表方向,"endnode":关系的结束节点,与identity对应与identity }]

 

 

如果是path=[[A-[r1]->B-[r2]->C],[A1-[r1]->B1-[r2]

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-12
猜你喜欢
  • 2022-12-23
  • 2021-12-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-19
相关资源
相似解决方案