1

I am performing below steps using cypher query. I am getting error in step 3. I have listed all steps below. Please help me to achieve expected output.

Step 1- Load data and define properties of nodes relationship



LOAD CSV WITH HEADERS FROM 'file://nodes_1Jan22_full_v2.csv' AS row
CREATE (n: Organisation {id: row.organisation, esg_index: toFloat(row.tone)});


LOAD CSV WITH HEADERS FROM 'file://edges_1Jan22_full_v2.csv' AS row
MERGE (src: Organisation {id: row.src})
MERGE (dst: Organisation {id: row.dst})
MERGE (src)-[:RELATE {freq: toInteger(row.relationship), sentiment: toFloat(row.avg_tone)}]->(dst);

Sample query and table structure

MATCH p=()-[r:RELATE]->() RETURN p LIMIT 1

{
  "start": {
"identity": 18862,
"labels": [
      "Organisation"
    ],
"properties": {
"id": "american university",
"esg_index": -3.005288932058546
    }
  },
  "end": {
"identity": 20048,
"labels": [
      "Organisation"
    ],
"properties": {
"id": "duke university",
"esg_index": -1.6810932825414502
    }
  },
  "segments": [
    {
      "start": {
"identity": 18862,
"labels": [
          "Organisation"
        ],
"properties": {
"id": "american university",
"esg_index": -3.005288932058546
        }
      },
      "relationship": {
"identity": 0,
"start": 18862,
"end": 20048,
"type": "RELATE",
"properties": {
"sentiment": -4.367701625823974,
"freq": 250
        }
      },
      "end": {
"identity": 20048,
"labels": [
          "Organisation"
        ],
"properties": {
"id": "duke university",
"esg_index": -1.6810932825414502
        }
      }
    }
  ],
  "length": 1.0
}

Step 2- Create graph projection

CALL gds.graph.project(
    'gdelt-analytics',
    'Organisation',
    'RELATE',
    {
        relationshipProperties: 'freq'
    }
)

MATCH (org:Organisation {id: 'public health'})
CALL gds.pageRank.stream('gdelt-analytics', {
    maxIterations: 100,
    dampingFactor: 0.85,
    sourceNodes: [org],
    relationshipWeightProperty: 'freq'
})
YIELD nodeId, score
RETURN *

Current Output

enter image description here

Step 3- Attempt to color node based on property "esg_index" and edges based on property "sentiment" (Query that is throwing error)

CALL apoc.create.addLabels(n.esg_index, [apoc.text.upperCamelCase(n.id)]) YIELD node
RETURN *

Neo.ClientError.Procedure.ProcedureCallFailed Failed to invoke procedure apoc.create.addLabels: Caused by: org.neo4j.internal.kernel.api.exceptions.EntityNotFoundException: Unable to load NODE with id -2.

Expected Output

Graph with nodes and edges colored. Nodes colored based on esg_index and edges colored based on sentiment

Carpe Diem
  • 73
  • 1
  • 1
  • 12

1 Answers1

1

The APOC addLabels function takes either a list of nodes or their id, which can be found using ID(n), as its input. You are passing esg_index, that's why you might be getting this error:

Try this:

CALL apoc.create.addLabels(n, [apoc.text.upperCamelCase(n.id)]) YIELD node
RETURN *

It should work. Documentation link.

Update:

To add the label using esg_index, I think apoc.do.case function should do the trick for you. You can try something like this:

CALL apoc.do.case([
  n IS NOT NULL AND n.esg_index = -5,
  'SET n:DARK_RED RETURN n AS node',
  n IS NOT NULL AND n.esg_index = 1,
  'SET n:GREEN RETURN n AS node'
  ],
  'RETURN n AS node',{n: n})
YIELD value
RETURN value.node AS node;
Charchit Kapoor
  • 8,934
  • 2
  • 8
  • 24
  • I would like to color the nodes based on property value for example if esg_index is -5 then node color should be dark red as compared to nodes with esg_index value being -3 or -2. If node value is positive then color should be green to dark green based on value. I would also like to color edges based on same concept but with different property "sentiment" – Carpe Diem May 30 '22 at 14:39
  • I am getting error ---------------> Neo.ClientError.Procedure.ProcedureCallFailed Failed to invoke procedure `apoc.do.case`: Caused by: java.lang.NullPointerException – Carpe Diem May 30 '22 at 15:13
  • Can you share the complete stack trace here? – Charchit Kapoor May 30 '22 at 15:58
  • MATCH(n:Organisation) CALL apoc.do.case([ n.esg_Index = -5, 'SET n:DARK_RED RETURN n AS node', n.esg_Index = 1, 'SET n:GREEN RETURN n AS node' ], 'RETURN n AS node',{n: n}) YIELD value RETURN value.node AS node; – Carpe Diem May 30 '22 at 16:03
  • 1
    I have updated the case function if any error exception comes up. Please paste the whole exception here. – Charchit Kapoor May 30 '22 at 16:10
  • I am getting same error and window only shows that much error. – Carpe Diem May 30 '22 at 16:19
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/245186/discussion-between-charchit-kapoor-and-carpe-diem). – Charchit Kapoor May 30 '22 at 16:22
  • Please can you help me finish it with edges part as well. MATCH p=()-[r:RELATE]->() CALL apoc.do.case([ p.sentiment <0, 'SET p:DARK_RED RETURN p AS edges', p.sentiment > 0, 'SET p:GREEN RETURN p AS edges' ], 'RETURN n AS edges',{p: p}) YIELD value RETURN value.edges AS edges; – Carpe Diem May 30 '22 at 17:11
  • I am facing small issue in setting color property of "relationship". I have pasted in chat. Can you please look? – Carpe Diem Jun 03 '22 at 16:09
  • The query os working fine on my system. I have pasted a screenshot – Charchit Kapoor Jun 04 '22 at 05:45