ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

图数据库 py2neo driver 使用笔记

2022-01-22 18:00:19  阅读:47  来源: 互联网

标签:node name py2neo driver 数据库 nd cates append result


1、Node

py2neo.data – Graph data types — py2neo 2021.1

2、relationships

py2neo.data – Graph data types — py2neo 2021.1

3、path

py2neo.data – Graph data types — py2neo 2021.1

        1、路径查询

根据起始节点与目标节点查询关系图

        cypher语句

MATCH p=(n)-[*..3]->(m) 
where n.name = '韩国' and m.name = '中国'
return p limit 10

        查询结果

        查询处理

def pathFinding(kgName, sourceName, targetName):
    graph = graphClient
    cates, filtEntity, filtRela = [], [], []
    result = {}
    entityList, relationList = [], []
    query = ''' MATCH p=(n)-[*..3]-(m) where n.name contains('{}') and m.name contains("{}")  
    return p limit 10 '''.format(sourceName.strip(), targetName.strip())
    print('''====关系查询''', query)
    try:
        res = graph.run(query).data()
        print(type(res), len(res))
        for i in res:
            i = i['p']
            for r in i.relationships:
                print(type(r), r)
                print(r['name'])
                print(r.identity)
                print(type(r.start_node), type(r.end_node))
                print(r.start_node, r.end_node)
                ids = r.start_node['objId']
                if ids not in filtEntity:
                    nd = {}
                    nd['id'] = r.start_node['objId']
                    nd['name'] = r.start_node['name']
                    if sourceName in nd['name']:
                        nd['category'] = '起始节点'
                        cates.append('起始节点')
                    elif targetName in nd['name']:
                        nd['category'] = '目标节点'
                        cates.append('目标节点')
                    else:
                        nd['category'] = r.start_node['category']
                        cates.append(nd['category'])
                    entityList.append(nd)
                    filtEntity.append(ids)
                ide = r.end_node['objId']
                if ide not in filtEntity:
                    nd = {}
                    nd['id'] = r.end_node['objId']
                    nd['name'] = r.end_node['name']
                    if sourceName in nd['name']:
                        nd['category'] = '起始节点'
                        cates.append('起始节点')
                    elif targetName in nd['name']:
                        nd['category'] = '目标节点'
                        cates.append('目标节点')
                    else:
                        nd['category'] = r.start_node['category']
                        cates.append(nd['category'])
                    entityList.append(nd)
                    filtEntity.append(ide)
                idr = r.identity
                if idr not in filtRela:
                    link = {}
                    link['id'] = str(idr)
                    link['source'] = r.start_node['objId']
                    link['target'] = r.end_node['objId']
                    link['name'] = r['shortName']
                    relationList.append(link)
                    filtRela.append(idr)
        result['entityList'] = entityList[:]
        result['relationList'] = relationList[:]
        cates = [{'name': i} for i in list(set(cates))]
        result['cates'] = sorted(cates, key=lambda e: e.__getitem__('name'))
        print("====所有分类排序", result['cates'])
        return result
    except Exception as e:
        print("=====error 路径查询错误", e)
        result['entityList'] = entityList[:]
        result['relationList'] = relationList[:]
        result['cates'] = cates
        return result

标签:node,name,py2neo,driver,数据库,nd,cates,append,result
来源: https://blog.csdn.net/qq_41617060/article/details/122640875

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有