From 56cf3c27694e50dee0b8765f562ac2f6b47e501a Mon Sep 17 00:00:00 2001 From: Jennifer Reif Date: Wed, 21 May 2025 10:01:42 -0500 Subject: [PATCH] Update Cypher syntax for PeopleService, tweak readme --- README.adoc | 11 ++++-- .../java/neoflix/services/PeopleService.java | 39 +++++++++++-------- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/README.adoc b/README.adoc index 2436728..99ce9a0 100644 --- a/README.adoc +++ b/README.adoc @@ -4,7 +4,7 @@ This repository accompanies the link:https://graphacademy.neo4j.com/courses/app-java/[Building Neo4j Applications with Java course^] on link:https://graphacademy.neo4j.com/[Neo4j GraphAcademy^]. -For a complete walkthrough of this repository, link:https://graphacademy.neo4j.com/courses/app-java/[enroll now^]. +For a complete walkthrough of this repository, link:https://graphacademy.neo4j.com/courses/app-java/[enroll now^]. == Setup @@ -16,7 +16,6 @@ sdk install java 17-open sdk use java 17-open sdk install maven mvn verify -mvn compile exec:java ---- .Connection details to your neo4j database are in `src/main/resources/application.properties` @@ -24,7 +23,7 @@ mvn compile exec:java ---- APP_PORT=3000 -NEO4J_URI=bolt://hostname-or-ip:7687 +NEO4J_URI=bolt://:7687 NEO4J_USERNAME=neo4j NEO4J_PASSWORD= @@ -32,6 +31,12 @@ JWT_SECRET=secret SALT_ROUNDS=10 ---- +.Run the application +[source,shell] +---- +mvn compile exec:java +---- + == A Note on comments You may spot a number of comments in this repository that look a little like this: diff --git a/src/main/java/neoflix/services/PeopleService.java b/src/main/java/neoflix/services/PeopleService.java index 69c099d..22faf51 100644 --- a/src/main/java/neoflix/services/PeopleService.java +++ b/src/main/java/neoflix/services/PeopleService.java @@ -76,17 +76,20 @@ public Map findById(String id) { // Get a person from the database var person = session.executeRead(tx -> { - String query = """ - MATCH (p:Person {tmdbId: $id}) - RETURN p { - .*, - actedCount: count { (p)-[:ACTED_IN]->() }, - directedCount: count { (p)-[:DIRECTED]->() } - } AS person - """; - var res = tx.run(query, Values.parameters("id", id)); - return res.single().get("person").asMap(); - }); + String query = """ + MATCH (p:Person {tmdbId: $id}) + WITH p, + count { (p)-[:ACTED_IN]->() } AS actedCount, + count { (p)-[:DIRECTED]->() } AS directedCount + RETURN p { + .*, + actedCount: actedCount, + directedCount: directedCount + } AS person + """; + var res = tx.run(query, Values.parameters("id", id)); + return res.single().get("person").asMap(); + }); return person; } } @@ -108,13 +111,17 @@ public List> getSimilarPeople(String id, Params params) { // Get a list of similar people to the person by their id var res = session.executeRead(tx -> tx.run(""" MATCH (:Person {tmdbId: $id})-[:ACTED_IN|DIRECTED]->(m)<-[r:ACTED_IN|DIRECTED]-(p) + WITH p, + count { (p)-[:ACTED_IN]->() } AS actedCount, + count { (p)-[:DIRECTED]->() } AS directedCount, + collect(m {.tmdbId, .title, type: type(r)}) AS inCommon RETURN p { - .*, - actedCount: count { (p)-[:ACTED_IN]->() }, - directedCount: count { (p)-[:DIRECTED]->() }, - inCommon: collect(m {.tmdbId, .title, type: type(r)}) + .*, + actedCount: actedCount, + directedCount: directedCount, + inCommon: inCommon } AS person - ORDER BY size(person.inCommon) DESC + ORDER BY size(inCommon) DESC SKIP $skip LIMIT $limit """,Values.parameters("id",id, "skip", params.skip(), "limit", params.limit()))