Search code examples
neo4jcyphergraphdbproperty-graph

Cypher query in a neo4j graph


I have the following graph in Neo4j:
Book: book_id, isbn, language_code, title, image_url, small_image_url, avg_ratings,
Author: name,
Reader: id,
with 3 relationships:
(Reader)-[:Rated]->(Book) that has the property {rating: value},
(Reader)-[:Recommend]->(Book),
(Author)-[:Write]->(Book).

I want to find the book that has been most recommended with a query in Cypher.
I wrote a query but I'm not too sure about it because I'm not familiar using count() and max() operators.

Here is my attempt:

MATCH (r:Reader) - [rel:recommend] -> (b:Book) 
RETURN count(rel), b 
ORDER BY count 
LIMIT 1

Solution

  • I would try this:

    MATCH (:Reader) - [:recommend] -> (b:Book) 
    RETURN 
        count(1) AS `Number of Recommendations`, 
        b AS `Book`
    ORDER BY `Number of Recommendations` DESC
    LIMIT 1
    

    I would leave off the LIMIT clause until you make sure it's doing what you want. I don't have your database, so I can't test this, but maybe this will work as a first stab at it.