Gary,
I understand you don't understand my match to CableInformation node upfront. Its my fault because I test the query with declaring each time the CableInformation. Fully understand your remarks.
Again here is some learning curve.
By the way I didn't test the query as well so that's why its confusing as well.
My original question is can I reduce some code clauses. I'm doubting it will be.
Because of the query structure the "Main" and "Local" are the same. Just wondering if I can reduce some clauses. Depeding on the cable.cableType some variables (main or local) will be calculated.
- The correct query should be:
// Count and define length all Main cables which are identified as Non Case-insensitive SPARE
MATCH (cables:Cables)
// Non Case-insensitive
WHERE cables.Omschrijvingen =~ '(?i).SPARE.' AND cables.cableType = "Main"
WITH count(cables) AS nrOfSpareCablesMain,
sum(cables.Lengte) AS lengthSpareCablesMain,
cableInfo
SET cableInfo.nrOfSpareCablesMain = nrOfSpareCablesMain,
cableInfo.lengthSpareCablesMain = lengthSpareCablesMain;
// Count and define length all Local cables which are identified as Non Case-insensitive SPARE
MATCH (cables:Cables)
// Non Case-insensitive
WHERE cables.Omschrijvingen =~ '(?i).SPARE.' AND cables.cableType = "Local"
WITH count(cables) AS nrOfSpareCablesLocal,
sum(cables.Lengte) AS lengthSpareCablesLocal,
cableInfo
SET cableInfo.nrOfSpareCablesLocal = nrOfSpareCablesLocal,
cableInfo.lengthSpareCablesLocal = lengthSpareCablesLocal;
With your advice I have changed the code into:
//Example
// 01. Delete all
MATCH (all) DETACH DELETE all;
// Delete record cableInfo first, to be sure we have only one record.
MATCH (cableInfo:CableInformation) DETACH DELETE cableInfo;
CREATE (cable1:Cables {length:100, cableType:"Main", description:"[Lt-Supply][P]LP7421 Supply 2 Lighting"});
CREATE (cable2:Cables {length:100, cableType:"Main", description:"[Crew-Lt][P]Art Light (Spare)"});
CREATE (cable3:Cables {length:100, cableType:"Main", description:"[RZ][1]24VDC GMDSS Light Red (SPARE)"});
CREATE (cable4:Cables {length:100, cableType:"Main", description:"[Tech-Lt][P]Power Socket"});
CREATE (cable1:Cables {length:100, cableType:"Local", description:"[Lt-Supply][P]LP7421 Supply 2 Lighting"});
CREATE (cable2:Cables {length:100, cableType:"Local", description:"[Crew-Lt][P]Art Light (Spare)"});
CREATE (cable3:Cables {length:100, cableType:"Local", description:"[RZ][1]24VDC GMDSS Light Red (SPARE)"});
CREATE (cable4:Cables {length:100, cableType:"Local", description:"[Tech-Lt][P]Power Socket"});
// Create a local node to store data
// In my opnion easy to use in a dashboard for instance NeoDash
MERGE (cableInfo:CableInformation {nrOfCablesMain: 0,
lengthCablesMain: 0,
nrOfCablesLocal: 0,
lengthCablesLocal: 0,
nrOfSpareCablesMain: 0,
lengthOfSpareCablesMain: 0,
nrOfSpareCablesLocal: 0,
lengthOfSpareCablesLocal: 0
}
);
MATCH (cables:Cables)
WHERE cables.cableType IN ["Main", "Local"] // AND cables.description =~ '(?i).SPARE.'
WITH cables.cableType as type,
cables,
count(cables) AS nrOfSpareCablesMain,
sum(cables.length) AS lengthOfSpareCablesMain,
count(cables) AS nrOfSpareCablesLocal,
sum(cables.length) AS lengthOfSpareCablesLocal
CALL apoc.do.when(type="Main",
"count(cables) AS nrOfSpareCablesMain
sum(cables.length) AS lengthOfSpareCablesMain
RETURN 1 as result",
"count(cables) AS nrOfSpareCablesLocal
sum(cables.length) AS lengthOfSpareCablesLocal
RETURN 2 as result",
{cables:cables}) YIELD value
MERGE (cableInfo:CableInformation{type: type})
WITH cables.cableType as type,
nrOfSpareCablesMain,
lengthOfSpareCablesMain,
nrOfSpareCablesLocal,
lengthOfSpareCablesLocal,
value,
cableInfo
CALL apoc.do.when(value.result = 1,
"cableInfo.nrOfSpareCablesMain = nrOfSpareCablesMain
cableInfo.lengthOfSpareCablesMain = lengthOfSpareCablesMain
RETURN 1 as result",
"cableInfo.nrOfSpareCablesLocal = nrOfSpareCablesLocal
cableInfo.lengthOfSpareCablesLocal = lengthOfSpareCablesLocal
RETURN 2 as result",
{cableInfo:cableInfo}) YIELD value1
RETURN value1.result
I don't know if this query is the right one. As far I know something is wrong with "YIELD value1". I don't understand why.
The other remark is, I comment out "AND cables.description =~ '(?i).SPARE.'" because it doesn't work. Don't understand because the query at 1) is working correctly.
Can you please help me.
Thanks in advance.