Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • 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)

like image 252
NicoGranelli Avatar asked Aug 05 '11 06:08

NicoGranelli


People also ask

How does query work in Neo4j?

If the query is not already in the Execution Plan Cache, the query is compiled into an execution plan in the Neo4j DBMS. The execution plan executes in the Neo4j DBMS to retrieve data. The Page Cache is used to hold the data in memory.

What is query on graph in Neo4j?

Cypher is Neo4j's graph query language that lets you retrieve data from the graph.

What query language is used with a Neo4j database?

Cypher is a graph query language that is powerful and easy to use, and a unique feature of Neo4j.


1 Answers

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.

like image 71
Michael Hunger Avatar answered Oct 02 '22 05:10

Michael Hunger