NicoGranelli
NicoGranelli

Reputation: 680

How could I write this queries in neo4j?

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:

Upvotes: 7

Views: 5168

Answers (1)

Michael Hunger
Michael Hunger

Reputation: 41706

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.

Upvotes: 13

Related Questions