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.

Grouping nodes of the same label based on attribute for path math

Hi, thanks for reading this question, I really need some help!
I need to query a path of report nodes, while grouping together related fact nodes to perform some math operations.

data model of the involved nodes:
(entity)-->(report)-->(fact)-->(timecontext)-->(spatialDimension)

I need to pull multiple attributes off the fact nodes, but still context bound by the report node, so that I can have a list of processed reports.

This query gets me where I need to be, but every time I add a new match the execution time goes up. This one runs at 25 seconds. pretty sure theres a cypher topic that i'm unaware of.

What is the correct way to write a query for this?

MATCH(entity:entity {uuid: "xxxx-xx-xxxx" })
WITH entity
MATCH(entity)-->(report:report)
where report.FormType = "nnn"
with *
match(report)-->(setA:fact {name: "setA"})
with *
match(report)-->(setB:fact {name: "setB"})
with *
match(report)-->(setC:fact {name: "setC"})
with *
match(report)-->(setD:fact {name: "setD"})
with *
match(report)-->(setE:fact {name: "setE"})
with
toFloat(replace(setA.value,",","")) as setA,
toFloat(replace(setB.value,",","")) as setB,
toFloat(replace(setC.value,",","")) as setC,
toFloat(replace(setD.value,",","")) as setD,
toFloat(replace(setE.value,",","")) as setE,
toFloat(replace(setF.value,",","")) as setF,
Date(report.Period) as Period
order by EffectiveDate desc
limit 5
return
Period,
setB / setA,
setD / setC,
setD - setE,
setF / setD,
setD + setA,
setA / setC

The profile of the query says i'm running a whole traversal with each match on fact. Is there a way I can collect nodes with traversing the report nodes and get all the facts in one go while keeping the placement of null value? I can query all the needed fact nodes and do the computations in pandas with just a couple hundred milliseconds, but i'd rather the database do the computation work.

1 ACCEPTED SOLUTION

UNWIND is much better solution

MATCH(entity)-->(reports:report)
where report.FormType = "nnn"
UNWIND reports as r
match(r)-->(setA:fact {name: "setA"})

View solution in original post

1 REPLY 1

UNWIND is much better solution

MATCH(entity)-->(reports:report)
where report.FormType = "nnn"
UNWIND reports as r
match(r)-->(setA:fact {name: "setA"})