【问题标题】:Neo4j How to call different properties for the same relationshipNeo4j 如何为同一个关系调用不同的属性
【发布时间】:2018-11-21 13:10:29
【问题描述】:

我正在使用新的算法来计算相似性,algo.similarity.jaccard 和 algo.similarity.overlap 与选项 WRITE,这意味着,在关系 SIMILARITY 中我创建属性 sim_jaccard 和 sim_overlap。 当我调用这两个属性时,问题就来了,例如:

MATCH (u1:User)-[s:SIMILARITY]-> (u2:User)
WITH coalesce(s.sim_jaccard,0) AS sim_jaccard, coalesce(s.sim_overlap,0)  AS sim_overlap
RETURN sim_jaccard, sim_overlap
LIMIT 25

我明白了:

sim_jaccard  sim_overlap
0            0.8507462686567164
0            0.9253731343283582 
0            0.8208955223880597
0            0.8955223880597015

我认为这是因为属性 id 在关系中不同:

SIMILARITY <id>: 3300778 sim_overlap: 1.0
SIMILARITY <id>: 2453827 sim_jaccard: 0.6268656716417911

我想要:

SIMILARITY <id>: 3300778 sim_overlap: 1.0,  sim_jaccard: 0.6268656716417911

有什么办法解决这个问题吗?

提前致谢。

【问题讨论】:

    标签: neo4j properties relationship similarity


    【解决方案1】:

    如果您想修改图表以便将关系合并为 1,则 APOC 过程有一些 refactor procs 将关系合并在一起(只需使用给定属性的 combine 值)。

    如果您只是希望您的返回值将这些值组合成一行,那么您可以修改您的查询以将两个节点之间所有 rel 的值相加:

    MATCH (u1:User)-[s:SIMILARITY]-(u2:User)
    WHERE id(u1) < id(u2)
    WITH u1, u2, sum(coalesce(s.sim_jaccard,0)) AS sim_jaccard, sum(coalesce(s.sim_overlap,0))  AS sim_overlap
    RETURN sim_jaccard, sim_overlap
    LIMIT 25
    

    对于需要合并节点的方法,您需要在调用过程之前收集与端节点相关的关系:

    MATCH (u1:User)-[s:SIMILARITY]-(u2:User)
    WHERE id(u1) < id(u2)
    WITH u1, u2, collect(s) as rels
    CALL apoc.refactor.mergeRelationships(rels, {properties:'combine'}) YIELD rel
    RETURN count(rel)
    

    sum(coalesce(s.sim_jaccard,0)) AS sim_jaccard, sum(coalesce(s.sim_overlap,0)) AS sim_overlap 返回 sim_jaccard, sim_overlap 限制 25

    【讨论】:

    • 是的,我需要将关系合并在一起,所以 apoc.refactor.mergeRelationships([rels],{config}) 应该可以解决我的问题。拜托,你能告诉我我做错了什么吗? :调用 apoc.refactor.mergeRelationships([MATCH (u1:User)-[s:SIMILARITY]->(u2:User)],{properties:'combine'}),因为它不起作用。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多