Search code examples
neo4jcyphercypher-3.1

WHERE condition in neo4j | Filtering by relationship property


How does the where condition in neo4j works ? I have simple data set with following relationship =>

Client -[CONTAINS {created:"yesterday or today"}]-> Transaction -[INCLUDES]-> Item

I would like to filter above to get the items for a transaction which were created yesterday, and I use the following query -

Match
 (c:Client) -[r:CONTAINS]->  (t:Transaction), 
 (t) -[:INCLUDES]-> (i:Item)
 where r.created="yesterday"
return c,t,i

But it still returns the dataset without filtering. What is wrong ? And how does the filtering works in neo4j for multiple MATCH statements say when I want to run my query on filetered dataset from previous steps?

Thank you very much in advance.


Solution

  • Your query seems fine to me. However, there are 2 things I would like to point out here:

    1. In this case, the WHERE clause can be removed and use match by property instead.
    2. The MATCH clause can be combined.

    So, the query would be:

    MATCH (c:Client) -[r:CONTAINS {created: "yesterday"}]-> (t:Transaction) -[:INCLUDES]-> (i:Item)
    RETURN c, t, i
    

    Regarding your second question, when you want to run another query on the filtered dataset from the previous step, use WITH command. Instead of returning the result, WITH will pipe your result to the next query.

    For example, with your query, we can do something like this to order the result by client name and return only the client:

    MATCH (c:Client) -[r:CONTAINS {created: "yesterday"}]->  (t:Transaction) -[:INCLUDES]-> (i:Item)
    WITH c, t, i
    ODERBY c.name DESC
    RETURN c