如何在cypher中通过两个条件查找节点列表?

时间:2017-03-31 08:54:59

标签: neo4j cypher

我有3种节点:标签,城市和地点。

我想写一个需要2个列表的查询

 [tag1,tag2,....]

 [city1,city2,....].

我想查找位于其中一个城市的地点列表,并从具有尽可能多的标签的地方订购它们到标签较少的地方。

 MATCH (spot:Spot)-[:located_at]->(city:City ) 
 where city.id IN ["22","23"]
 with spot as sp,city as cy
 MATCH (sp:Spot)-[rels:tagged_by]->(tag:Tag)
 where tag.id IN ["16", "10151", "21"]
 with sp as fsp, tag
 RETURN fsp, tag,    count(distinct fsp.id) AS cnt
 order by cnt desc

我尝试了该查询,但无论如何都无法列出节点及其标签。

请帮忙! 提前谢谢!

3 个答案:

答案 0 :(得分:1)

我是通过以下方式做到的。我的错误是不知道计算关系而不知道正确使用聚合。

 match (city:City)<-[]-(spot:Spot) 
 where city.id in ["22","23"] 
 match (spot)-[rel]->(tag:Tag) 
 where tag.id in ["16", "10151", "21"] 
 return spot, count(rel) as rel_count 
 order by rel_count desc limit 100

这是我想要的。

答案 1 :(得分:0)

尝试子查询apporach:

选择城市 - &gt;找到斑点 - &gt;找到该地点的标签

MATCH (city:City)
OPTIONAL MATCH (spot:Spot)-[:located_at]->(city) 
OPTIONAL MATCH (spot)-[rels:tagged_by]->(tag:Tag)
where city.id IN ["22","23"]
where tag.id IN ["16", "10151", "21"]
RETURN spot, tag, count(distinct fsp.id) AS cnt
order by cnt desc

答案 2 :(得分:0)

我如何处理这个问题是。

MATCH (spot:Spot)-[:located_at]->(city:City)
RETURN city,spot,size(spot-[:tagged_by]->()) as tags order by tags desc

所以现在你得到标签最多的所有景点。显然你可以根据需要添加过滤器

MATCH (spot:Spot)-[:located_at]->(city:City) where city.name = "London"
RETURN city,spot,size(spot-[:tagged_by]->())  as tags order by tags desc