各社区PageRank计算:Cypher语句类型不匹配错误求助
解决Cypher执行PageRank时的类型不匹配错误
问题描述
执行用于计算各社区PageRank的Cypher语句时,触发错误:
Type mismatch: expected String but was Map (line 3, column 26 (offset: 96)) "CALL gds.pageRank.stream({"
原语句如下:
MATCH (i:exp_amd_cif) WITH distinct i.louvain_community as community CALL gds.pageRank.stream({ nodeQuery:"MATCH (i:exp_amd_cif) WHERE i.louvain_community = $community RETURN id(i) as id", relationshipQuery: "MATCH (acs:exp_amd_cif)-[:transfer_to]->(act:exp_amd_cif) RETURN id(acs) as source, id(act) as target", parameters:{community:community}}) YIELD nodeId, score WITH community, nodeId, score ORDER BY score DESC RETURN community, collect(gds.util.asNode(nodeId).name)[..5] as top_5_representatives
错误原因
当前使用的Neo4j GDS版本不支持直接传入配置Map作为gds.pageRank.stream的参数,该版本要求第一个参数为已投影的图名称(字符串),而非配置Map,因此触发类型不匹配错误。此外原语句的关系查询未过滤社区范围,会导致计算结果基于全图关系,不符合社区内PageRank的计算预期。
解决方案
方案1:适配GDS 2.x+ 直接投影语法
如果环境使用GDS 2.x及以上版本,可使用直接投影配置Map的语法,同时修正关系过滤逻辑:
MATCH (i:exp_amd_cif) WITH distinct i.louvain_community as community CALL gds.pageRank.stream({ nodeProjection: { exp_amd_cif: { label: 'exp_amd_cif', filter: 'n.louvain_community = $community' } }, relationshipProjection: { transfer_to: { type: 'transfer_to', orientation: 'NATURAL', filter: 'startNode(r).louvain_community = $community AND endNode(r).louvain_community = $community' } }, parameters: {community: community} }) YIELD nodeId, score WITH community, nodeId, score ORDER BY score DESC RETURN community, collect(gds.util.asNode(nodeId).name)[..5] as top_5_representatives
方案2:兼容GDS 1.x 图投影+调用方式
针对旧版GDS,需先为每个社区创建临时图投影,计算完成后再删除投影:
MATCH (i:exp_amd_cif) WITH distinct i.louvain_community as community // 创建当前社区的临时图投影 CALL gds.graph.project( 'temp-community-' + toString(community), {exp_amd_cif: {label: 'exp_amd_cif', filter: 'n.louvain_community = $community'}}, {transfer_to: {type: 'transfer_to', filter: 'startNode(r).louvain_community = $community AND endNode(r).louvain_community = $community'}}, {parameters: {community: community}} ) YIELD graphName // 计算社区内PageRank CALL gds.pageRank.stream(graphName) YIELD nodeId, score WITH community, nodeId, score, graphName // 删除临时图释放资源 CALL gds.graph.drop(graphName) WITH community, nodeId, score ORDER BY score DESC RETURN community, collect(gds.util.asNode(nodeId).name)[..5] as top_5_representatives
内容的提问来源于stack exchange,提问作者Zayyed Akhmed Assidqie
相关产品推荐
相关产品推荐

