7

I'm very new to neo4j and to graph database in general. I'm prototyping an app, and I don't know how should i write these queries

I've this domain:

User Restaurant Review TypeOfFood

So a Restarurant have one or many TypeOfFood, the User leaves reviews about restaurants. The User have some preferred foods, matching the TypeOfFood a restaurant sell. Also Users are related to each other with the typically friend relationship.

Some of the queries I'm trying to write:

  • Give me all the restaurants that my friends have rated with 3 or more stars that make the kind of food I like (exclude those restaurants that I already reviewed)

  • Suggest me friends I may know (I guess this should be something like "all the friends that are friends of my friends but no yet mine, order by something)

NicoGranelli
  • 670
  • 7
  • 14

1 Answers1

13

Using Neo4j's Cypher query language you could write your queries like this:

Selecting the top-20 best rated restaurants, sorted by stars and number of reviews

start user=(users,name,'Nico')
match user-[:FRIEND]->friend-[r,:RATED]->restaurant-[:SERVES]->food,
      user-[:LIKES]->food,user-[:RATED]->rated_by_me
where r.stars > 3
return restaurant.name, avg(r.stars), count(*)
order by avg(r.stars) desc, count(*) desc 
limit 20

Friends of a Friend

start user=(users,name,'Nico')
match user-[:FRIEND]->friend->[:FRIEND]->foaf
return foaf, foaf.name

You can execute these cypher queries in the Neo4j Webadmin Console on your dataset, but also in the neo4j-shell, remotely via the Cypher-Rest-Plugin via Spring Data Graph.

There is also a screencast discussing similar queries in cypher.

You can also use Gremlin, Neo4j-Traversers or manual traversing via getRelationships if you'd like.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
Michael Hunger
  • 41,339
  • 3
  • 57
  • 80
  • 1
    Thanks Michael. I went to your SO profile to find a way to contact you, expecting a normal coder, but you're a rock star :) Do you do consulting? or maybe you can put me in contact with someone? – NicoGranelli Aug 07 '11 at 04:22
  • 1
    Thanks for the praise. You can contact me by subscribing to the Neo4j mailing list. We at Neo Technology also do consulting. – Michael Hunger Aug 14 '11 at 09:42
  • Hi @MichaelHunger, I have posted one query (http://stackoverflow.com/questions/17721173/neo4j-user-suggestion-with-mutual-count/17831172?noredirect=1#comment26066269_17831172), can you help me out for this? and How do I subscribe you in mailing list. I want to know about your consulting and it's charges. Thanks – Manish Sapkal Jul 26 '13 at 07:27