Home > front end >  Use 2 CALLS in the same query
Use 2 CALLS in the same query

Time:12-30

I need to get the return of the first CALL, keep it in a variable and use it for a second CALL. For example, I want to know how many laws were proposed at the 114th Congress, and how many of these are currently active?

`

MATCH (b:Bill)
CALL{
    MATCH (b)-[:PROPOSED_DURING]->(c:Congress)
    WHERE c.number = '114'
    RETURN b as bills
}
WITH bills
CALL {
        MATCH (bills)
        WHERE bills.active = 'True'
        RETURN bills as activeBills
}

return count(bills), count(activeBills)

`

Bill are the laws, which are proposed during a congress. Do you know why this is not working properly as it just remains loading without ending?

CodePudding user response:

The query is doing a triple cartesian product on the number of bills, number of bills proposed in 144th congress and active bills on that session. That is why the query is just loading endlessly.

n(Bills) x n(bills in 114th Congress) x n(active bills in 114th Congress) = big number

I would propose an alternative solution without using CALL function.

MATCH (b:Bill)-[:PROPOSED_DURING]->(c:Congress) WHERE c.number = '114'
WITH collect(b) as bills
RETURN size(bills) as billCounts, size([b in bills where b.active = 'True'|b]) as activeBillCounts

Going back to your query, below is the fix on your query and it is quite wordy. The problem was within the CALL function, you need to add another line WITH b so that the bills will be passed within the CALL function. Without it (With b inside the CALL function), it will do a cartesian product on ALL bills x Bills in 114th congress. On the 2nd CALL function, you need to collect the bills and UNWIND it inside 2nd CALL function. This is because using the same variable bills from 1st CALL function to 2nd CALL function will filter the bills and you will get incorrect count. This is because using the same variables between two CALLS will also remove Inactive bills on the 2nd CALL.

MATCH (b:Bill)
CALL{
    WITH b   // <- please add this line
    MATCH (b)-[:PROPOSED_DURING]->(c:Congress)
    WHERE c.number = '114'
    RETURN b as bills
}
WITH collect(b) as bills   // <- please add this line
CALL {  WITH bills    // <- please add this line
        UNWIND bills as bill  // <- please add this line
        MATCH (bill)
        WHERE bill.active = 'True'    
        RETURN bill as activeBills
}
return size(bills), count(activeBills)
  • Related