0

i have question on cyper.I don't know how to execute

Find a list of books no-one likes.

i wrote the following query

MATCH (b1:Book),(R:Reader)
WITH R,b1
OPTIONAL MATCH (b1)--(L:LIKES)--(R)
WHERE L IS NULL
RETURN b1.title

I am getting some result but i think it is wrong.

when i execute the below query, i can see the relation in the graph

MATCH (b1:Book),(R:Reader) WITH R,b1 OPTIONAL MATCH (b1)--(L:LIKES)--(R) WHERE L IS NULL RETURN b1,R

enter image description here

2)Find all pairs of people that have no liked books in common.

i wasnt able to solve this question.

3 Answers 3

1

If you are just looking for the books no one likes then you don't need to look at people at all, just books. Here is a query to return the list of books nobody likes.

MATCH (b:Book) 
WHERE NOT (b)<--(:LIKES)
RETURN b

If nobody likes a book isn't that your entire community of people that don't like the book?

Here are all the Readers that have not liked a book.

MATCH (r:Reader) 
WHERE NOT (r)--(:LIKES)
RETURN r
Sign up to request clarification or add additional context in comments.

1 Comment

the question says Find all pairs of people that have no liked books in common. shouldnt the answer contain pair of people. i mean two people
0
  1. MATCH (b:book) WHERE NOT ((:Reader)-[:LIKES]->(b:book)) RETURN b

    1. MATCH (R1:Reader), (R2:Reader) WITH R1, R2 OPTIONAL MATCH (R1)--(b:book)--(R2) WHERE b IS NULL RETURN R1.name, R2.name

Comments

0

You actually have 2 questions. These answers may work for you:

(1) Find all Books that no Reader LIKES:

MATCH (b:Book)
WHERE NOT EXISTS((b)<-[:LIKES]-(:Reader))
RETURN b;

(2) Find all Reader pairs that do not like any of the same Books:

MATCH (r:Reader)
OPTIONAL MATCH (r)-[:LIKES]->(b:Book)
WITH r, COLLECT(b) AS books
WITH COLLECT({r: r, books: books}) AS data
RETURN REDUCE(s = [], i1 IN RANGE(0, SIZE(data)-2) |
  s + REDUCE(t = [], i2 IN RANGE(i1+1, SIZE(data)-1) |
    CASE WHEN SIZE(apoc.coll.intersection(data[i1].books, data[i2].books)) = 0
      THEN t + [data[i1], data[i2]]
      ELSE t END
  )
) AS dissimilar_pairs

OPTIONAL MATCH is used so that Readers that like no books will also be considered. The APOC function apoc.coll.intersection is used to get the intersection of 2 lists.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.