在cypher中检索2个连接节点之间的公共属性

时间:2016-07-13 09:19:11

标签: neo4j cypher

如何在Neo4j中找到2个连接节点之间的公共属性?

例如,我有2个以下连接节点。

create(n:PERSON{userid:0, `78`:78, `127`:127, `336`:336, `346`:346, `349`:349, `55`:55, `255`:255, `61`:61, `354`:354, `356`:356, `97`:97, `361`:361, `338`:338, `217`:217, `348`:348, `53`:53, `61`:61, `92`:92, `3`:3, `210`:210, `54`:54, `352`:352, `354`:354, `209`:209, `3`:3, `210`:210, `345`:345, `72`:72, `360`:360, `361`:361, `209`:209, `340`:340, `72`:72, `352`:352, `359`:359, `97`:97, `2`:2, `344`:344, `345`:345, `255`:255, `65`:65, `67`:67, `77`:77, `344`:344, `350`:350, `92`:92, `338`:338, `337`:337, `340`:340, `55`:55, `97`:97, `14`:14, `342`:342, `255`:255, `356`:356, `357`:357, `77`:77, `335`:335, `344`:344, `351`:351, `355`:355, `360`:360, `337`:337, `348`:348, `77`:77, `3`:3, `53`:53, `352`:352, `97`:97, `209`:209, `14`:14, `338`:338, `347`:347, `348`:348, `354`:354, `209`:209, `14`:14, `338`:338, `3`:3, `343`:343, `255`:255, `354`:354, `97`:97, `360`:360, `341`:341, `72`:72, `92`:92, `338`:338, `347`:347, `61`:61, `64`:64, `65`:65, `352`:352, `353`:353, `354`:354, `355`:355, `361`:361, `2`:2, `340`:340, `343`:343, `351`:351, `355`:355, `209`:209, `335`:335, `338`:338, `339`:339, `346`:346, `348`:348, `64`:64, `71`:71, `77`:77, `78`:78, `351`:351, `72`:72, `359`:359, `337`:337, `217`:217, `53`:53, `55`:55, `351`:351, `355`:355, `356`:356, `77`:77, `217`:217, `339`:339, `348`:348, `349`:349, `53`:53, `351`:351, `67`:67, `357`:357, `77`:77, `3`:3, `335`:335, `351`:351, `352`:352, `336`:336, `347`:347, `348`:348, `55`:55, `350`:350, `255`:255, `359`:359, `209`:209, `2`:2, `53`:53, `54`:54, `72`:72, `353`:353, `2`:2, `14`:14, `342`:342, `344`:344, `255`:255, `354`:354, `355`:355, `77`:77, `97`:97, `360`:360, `361`:361, `209`:209, `336`:336, `345`:345, `348`:348, `349`:349, `65`:65, `2`:2, `3`:3, `340`:340, `342`:342, `71`:71, `72`:72, `92`:92, `97`:97, `337`:337, `347`:347, `64`:64, `65`:65, `357`:357, `360`:360, `339`:339, `342`:342, `351`:351, `361`:361, `337`:337, `342`:342, `64`:64, `358`:358, `2`:2, `342`:342, `346`:346, `348`:348, `54`:54, `350`:350, `61`:61, `64`:64, `359`:359, `92`:92, `97`:97, `360`:360, `336`:336, `341`:341, `217`:217, `53`:53, `350`:350, `353`:353, `347`:347, `349`:349, `357`:357, `3`:3, `210`:210, `336`:336, `346`:346, `349`:349, `355`:355, `356`:356, `358`:358, `353`:353, `360`:360, `348`:348, `65`:65, `71`:71, `2`:2, `341`:341, `344`:344, `350`:350, `352`:352, `353`:353, `92`:92, `360`:360, `345`:345, `64`:64, `65`:65, `2`:2, `340`:340, `341`:341, `67`:67, `359`:359, `336`:336, `345`:345, `351`:351, `255`:255, `61`:61, `217`:217, `339`:339, `55`:55, `14`:14, `61`:61, `355`:355, `357`:357, `336`:336, `340`:340, `342`:342, `354`:354, `337`:337, `348`:348, `71`:71, `97`:97, `3`:3, `217`:217, `55`:55, `350`:350, `351`:351, `352`:352})

create(n:PERSON{userid:1, `77`:77, `127`:127, `337`:337, `338`:338, `346`:346, `348`:348, `356`:356, `209`:209, `335`:335, `337`:337, `348`:348, `53`:53, `67`:67, `335`:335, `346`:346, `67`:67, `77`:77, `53`:53, `67`:67, `2`:2, `339`:339, `346`:346, `348`:348, `53`:53, `55`:55, `71`:71, `353`:353})

我知道这两个节点已连接。它们也有共同的特性。我想知道如何使用cypher检索这些常见属性。

1 个答案:

答案 0 :(得分:1)

// Get two persons:
MATCH (P1:PERSON {userid:0}), (P2:PERSON {userid:1})

// For each take an array of property:
WITH P1, P2, keys(P1) as KP1, keys (P2) as KP2

// Use a list comprehension to return only the common properties:
// http://neo4j.com/docs/developer-manual/current/#_list_comprehension
RETURN [p in KP1 WHERE p IN KP2] as commonProperties

<强> 更新 : 值得研究的方法可能更像是图形:在各个节点中存储属性。

例如,您的数据:

UNWIND [
    {userid: 0, prop: [358, 2, 210, 357, 349, 53, 64, 344, 338, 342, 3, 359,
                       354, 352, 92, 345, 355, 347, 54, 97, 350, 72, 353,
                       340, 348, 337, 14, 341, 339, 65, 71, 360, 343, 351, 
                       346, 127, 67, 78, 335, 77, 336, 217, 209, 255, 361,
                       55, 356, 61] },
    {userid: 1, prop: [71, 348, 337, 353, 2, 346, 127, 339, 53, 335, 338,
                       77, 55, 356, 209, 67] }
] as userData
     // Create user
     MERGE (User:PERSON {userid: userData.userid}) WITH User, userData
     UNWIND userData.prop as prop
     // Create a node named for properties
     MERGE (Prop:PERSONPROP {name: prop})
     // Associate users with the property
     MERGE p = (User)-[:hasProp]->(Prop)
RETURN p

我们得到以下图片:

enter image description here

所需的查询非常简单:

// Get two persons common properties:
MATCH commonProp = (P1:PERSON {userid:0})-[:hasProp]->
                   (:PERSONPROP)
                   <-[:hasProp]-(P2:PERSON {userid:1})
RETURN commonProp

enter image description here