Micky
Micky

Reputation: 57

cypher query not getting result

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.

Upvotes: 0

Views: 370

Answers (3)

cybersam
cybersam

Reputation: 66989

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.

Upvotes: 0

user12388332
user12388332

Reputation: 1

  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

Upvotes: 0

Dave Bennett
Dave Bennett

Reputation: 11216

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

Upvotes: 1

Related Questions