当其中一个节点已知时返回其他相关节点

时间:2017-07-08 20:32:53

标签: neo4j cypher

我的数据格式为

(n:Node)-[HAS_ADDRESS]->(r:Address{name:'Goa'}),
(n:Node)-[HAS_ADDRESS]->(r:Address{name:'India'}),
(n:Node)-[HAS_ANCHOR]->(k:Keyword{name:'accounting'}),
(n:Node)-[HAS_ANCHOR]->(k:Keyword{name:'Keyword 2'}),
(n:Node)-[HAS_ANCHOR]->(k:Keyword{name:'Keyword 3'}),
(n:Node)-[HAS_PHOTO]->(ph:Photo{name:'photo1'}),
(n:Node)-[HAS_PHOTO]->(ph:Photo{name:'photo2'}),
etc...

节点以以下格式存储

(n:Node{name:'',rating:'',international_phone_number:''})
(ph:Photo{id:''})
(k:Keyword{name:''})

我的价值是' k'和' n'所以我用了

MATCH (k:Keyword { name: 'accounting'})<-[:HAS_ANCHOR]-(p)-[:HAS_ADDRESS]->(l:Address‌​),(p)-[:HAS_PHOTO]->‌​(ph)
WHERE ph.crawl=1 AND l.name = 'Goa' 
WITH p, l
MATCH (p)-[:HAS_ADDRESS]->(other_r:Address)
return p.name as name, p.rating as rating, p.formatted_address as address,p.international_phone_number as international_phone_number,collect(ph.photo_reference) as photos, l as locations, other_r as other_addresses
ORDER BY p.rating DESC

我对如何获取具有所有细节的节点一无所知。

1 个答案:

答案 0 :(得分:1)

由于您只知道“节点1”,因此您需要从第一场比赛中获取“n”并使用它来获取的其他同伴“R”即可。

// your original query that matches n and r based on the known node r
MATCH (n:Node)->[k:HAS_ANCHOR]->(r:Node) 
WHERE r.name = 'Node 1'

// use the result of n from the first query to find peers of r 
WITH n, r
MATCH (n)-[:HAS_ANCHOR]->(other_r:Node) 
RETURN n, r, other_r

<强>更新 根据评论,这有用吗?

// match the anchor based on the keyword
MATCH (k:Keyword { name: 'accounting'})<-[:HAS_ANCHOR]-(p)
WHERE (p)-[:HAS_ADDRESS]->(:Address‌​ {name: 'Goa'})

// optionally match and collect the photos
WITH p
OPTIONAL MATCH (p)-[:HAS_PHOTO]->‌​(ph)
WHERE ph.crawl=1  

//match and collect all of the addresses 
WITH p, l, collect(ph.photo_reference) as photos
MATCH (p)-[:HAS_ADDRESS]->(r:Address)
RETURN p.name as name, 
   p.rating as rating, 
   p.formatted_address as address,
   p.international_phone_number as international_phone_number,
   photos,
   collect(r) as addresses 
ORDER BY p.rating DESC