【问题标题】:Neo4j pipe dataNeo4j 管道数据
【发布时间】:2017-11-19 15:55:42
【问题描述】:

您好,我在 neo4j 上遇到了一些麻烦

// 1. Find the most_popular_cuisine
MATCH (n:restaurants)
WITH COUNT(n.cuisine) as total
MATCH (r:restaurants)    
RETURN r.cuisine , 100 * count(*)/total as percentage
order by percentage desc
limit 1

我正在尝试通过获取顶部结果并与之匹配以获取仅具有该属性的节点,从而进一步扩展它

WITH COUNT(n.cuisine) as total
MATCH (r:restaurants)    
WITH r.cuisine as cuisine , count(*) as cnt
MATCH (t:restaurants)
WHERE t.cuisine = cuisine AND count(*) = MAX(cnt)
RETURN t

【问题讨论】:

    标签: neo4j cypher


    【解决方案1】:

    我认为你最好稍微重构一下你的模型,让:Cuisine 是一个标签,并且每个美食都有自己的节点。

    (:Restaurant)-[:OFFERS]->(:Cuisine)
    

    (:Restaurant)-[:SPECIALIZES_IN]->(:Cuisine)
    

    那么您的查询可能如下所示

    MATCH (cuisine:Cuisine)
    RETURN cuisine, size((cuisine)<-[:OFFERS]-()) AS number_of_restaurants
    ORDER BY number_of_restaurants DESC  
    

    【讨论】:

    • 是的,问题是模型是使用 doc_manager 的 mongo 实例。我作为大学作业
    【解决方案2】:

    我无法在 WITH 而不是 RETURN 语句中使用 WITH r.cuisine as cuisine , count(*) as cnt,因此我不得不采用稍微冗长的方法。

    可能有更优化的方法来做到这一点,但这也有效,

    // Get all unique cuisines in a list
    MATCH (n:Restaurants)  
    WITH COUNT(n.Cuisine) as total, COLLECT(DISTINCT(n.Cuisine)) as cuisineList
    
    // Go through each cuisine and find the number of restaurants associated with each
    UNWIND cuisineList as c
    MATCH (r:Restaurants{Cuisine:c})
    WITH total, r.Cuisine as c, count(r) as cnt
    ORDER BY cnt DESC
    WITH COLLECT({Cuisine: c, Count:cnt}) as list
    
    // For the most popular cuisine, find all the restaurants offering it
    MATCH (t:Restaurants{Cuisine:list[0].Cuisine}) 
    RETURN t
    

    【讨论】:

      猜你喜欢
      • 2021-10-16
      • 1970-01-01
      • 2022-07-03
      • 1970-01-01
      • 2018-02-13
      • 2018-10-09
      • 1970-01-01
      • 1970-01-01
      • 2011-05-24
      相关资源
      最近更新 更多