Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Query by cypher

Avatar for asako asako
October 20, 2016

Query by cypher

Query cypher with Neo4j 3.0.6
After loading livedoor grourmet dataset, issued various queries.
refer: https://speakerdeck.com/asakoraven/load-csv-by-cypher

Avatar for asako

asako

October 20, 2016

More Decks by asako

Other Decks in Programming

Transcript

  1. MATCH (c:Comment) - [rate:RATE] -> (restaurant:Restaurant) WITH restaurant, (rate.total +

    rate.service + rate.food + rate.atmosphere) as rate_sum WHERE rate_sum > 15 RETURN restaurant.name, rate_sum ORDER BY rate_sum DESC LIMIT 25 ;
  2. MATCH p = (u:User) <- [vote:VOTED_BY] - (c:Comment) - [rate:RATE]

    -> (r:Restaurant) RETURN p, r.name AS restaurant, COUNT(c) AS comment_count, COLLECT(c) AS comments ORDER BY comment_count DESC LIMIT 25 ;
  3. MATCH (r:Restaurant) <- [:RATE] - (c:Comment) OPTIONAL MATCH (c) -

    [v:VOTED_BY] -> (u:User) WITH c, r, v RETURN r.name AS restaurant, COUNT(DISTINCT c) AS comment_count, SIZE(COLLECT(DISTINCT v)) AS like_count, (COUNT(DISTINCT c) * 10 + SIZE(COLLECT(DISTINCT v))) AS score ORDER BY score DESC LIMIT 25 ;
  4. Best restaurant? 1. comment ͕͍ͭͨ͜ͱΛείΞ10ͱ਺͑Δ 2. comment ʹ͍ͭͨʮ͍͍Ͷʂʯ(VOTEDʣΛείΞ1ͱ ਺͑Δ 3.

    rate.total ͕ 3ҎԼͷcomment͸ධՁର৅ͱ͠ͳ͍ 3. 1.ͱ2. Λ଍͠߹ΘͤͨείΞͷ্ҐΛbest restaurant ͱ ͢Δ
  5. MATCH (r:Restaurant) <- [rate:RATE] - (c:Comment) OPTIONAL MATCH (c) -

    [v:VOTED_BY] -> (u:User) WITH c, r, v, (rate.total + rate.service + rate.food + rate.atmosphere) as rate_sum WHERE rate.total > 3 RETURN r.name AS restaurant, COUNT(DISTINCT c) AS comment_count, SIZE(COLLECT(DISTINCT v)) AS like_count, (COUNT(DISTINCT c) * 10 + SIZE(COLLECT(DISTINCT v))) AS score ORDER BY score DESC LIMIT 25 ;
  6. User similarity? 1. ᅂ޷ͷ͍ۙϢʔβΛϐοΫΞοϓ͍ͨ͠ 2. rate.total, atmosphere, service, food ͷڑ཭Λܭࢉ͢Δ

    3. ڞ௨ͰධՁ͍ͯ͠Δrestaurant ͕ 10ҎԼͷuser ಉ࢜͸ ධՁ͠ͳ͍ 4. ڑ཭͕͍ۙuser ͕ ᅂ޷͕͍ۙϢʔβͱ͢Δ
  7. MATCH (u1:User) <- [:COMMENTED_BY] - (c1:Comment) - [r1:RATE] -> (r:Restaurant)

    <- [r2:RATE] - (c2:Comment) - [:COMMENTED_BY] -> (u2:User) WITH u1, u2, COUNT(r) AS restaurant_count, SQRT(SUM((r1.total - r2.total)^2 + (r1.atmosphere - r2.atmosphere)^2 + (r1.service - r2.service)^2 + (r1.food - r2.food)^2)) AS distance WHERE restaurant_count > 10 RETURN u1, u2, restaurant_count, distance, (1 / (1+distance)) AS similarity ORDER BY distance LIMIT 50 ;
  8. MATCH (u1:User) <- [:COMMENTED_BY] - (c1:Comment) - [r1:RATE] -> (r:Restaurant)

    <- [r2:RATE] - (c2:Comment) - [:COMMENTED_BY] -> (u2:User), (u1) <- [v1:VOTED_BY] - (c2), (u2) <- [v2:VOTED_BY] - (c1) WITH u1, u2, COUNT(r) AS restaurant_count, SQRT(SUM((r1.total - r2.total)^2 + (r1.atmosphere - r2.atmosphere)^2 + (r1.service - r2.service)^2 + (r1.food - r2.food)^2)) AS distance WHERE distance < 10 AND restaurant_count > 10 RETURN u1, u2, distance ORDER BY distance LIMIT 50
  9. MATCH (u1:User) <- [:COMMENTED_BY] - (c1:Comment) - [r1:RATE] -> (r:Restaurant)

    <- [r2:RATE] - (c2:Comment) - [:COMMENTED_BY] -> (u2:User), (u1) <- [v1:VOTED_BY] - (c2), (u2) <- [v2:VOTED_BY] - (c1) WITH u1, u2, COLLECT(r.name) AS restaurants, COUNT(r) AS restaurant_count, SQRT(SUM((r1.total - r2.total)^2 + (r1.atmosphere - r2.atmosphere)^2 + (r1.service - r2.service)^2 + (r1.food - r2.food)^2)) AS distance WHERE distance < 10 AND restaurant_count > 10 RETURN u1, u2, restaurants, distance ORDER BY distance LIMIT 50