cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Neo4J and Java (how to return a list)

Hello,

I am trying to modify the Java hello world example found here https://neo4j.com/developer/java/.

I changed the query to a query such as the one below (I tested that it works):

MATCH (me:User{id:'220'})-[r:RATED]-(m)
WITH me, avg(r.rating) AS average
MATCH (me)-[r1:RATED]->(m:Movie)<-[r2:RATED]-(other:User)-[r3:RATED]->(m2:Movie)
WHERE r1.rating > average AND r2.rating > average AND r3.rating > average AND NOT (me)-[:RATED]->(m2)
RETURN distinct m2 AS recommended_movie, count(*) AS score
ORDER BY score DESC
LIMIT 15

 

When I run the query in the browser I get a result that has two columns; recommended movie and score. I would like my query in Java to return the list of recommended movies. However, the hello world example only uses:

return result.single().get(0).asString();

 

How can I modify this such that I get the list of recommended movies?

 

Thanks in advance!

2 REPLIES 2

ameyasoft
Graph Maven

Try this:

WITH distinct m2 AS recommended_movie, count(*) AS score
WITH recommended_movie, score ORDER BY score DESC
RETURN COLLECT(recommended_movie) as movies, COLLECT(score) as scores

You can approach it in two ways. You can either return a list from the query and retrieve the list in the java client, or return a rows and create the list in the java client.

Option 1:

First, alter the query to return a list of movies. You can do this by replacing the current ‘return’ line with the following:

RETURN collect(distinct m2) AS recommended_movies

Next, change your 'return' statement to the following:

return result.single().get(”recommended_movies”).asList(Value::asString);

Note, you could use 'get(0)' instead of 'get("recommended_movies")', but I don't like using index references.

Option 2:

You can leave the 'return' statement as is, but you are not processing the 'score' variable, so you should probably remove it. 

Next, change your 'return' statement to the following:

return result.list(v->v.get("recommended_movies").asString());