Question

I am extremely new to neo4j cypher query language. I have created a simplistic graph between nodes Client -> Portfolio -> Trade. Here are the cypher statements:

create (c:Client {id:1, name:"Yezdi"}) return c;
create (p:Portfolio {id:1, name:"Yezdi-1", cId:1}) return p;
create (p:Portfolio {id:2, name:"Yezdi-2", cId:1}) return p;
create (p:Portfolio {id:3, name:"Yezdi-3", cId:1}) return p;
create (t:Trade {id:1, qty:100, prc:23.44, pId:1}) return t;
create (t:Trade {id:2, qty:102, prc:23.44, pId:1}) return t;
create (t:Trade {id:3, qty:103, prc:23.44, pId:1}) return t;
create (t:Trade {id:4, qty:104, prc:23.44, pId:2}) return t;
create (t:Trade {id:5, qty:105, prc:23.44, pId:3}) return t;
create (t:Trade {id:6, qty:106, prc:23.44, pId:3}) return t;

match (c:Client),(p:Portfolio) where c.id=p.cId create unique (c)-[:HAS_PORT]->(p);
match (p:Portfolio), (t:Trade) where p.id = t.pId create unique (p)-[:HAS_TRADE]->(t)

When i added the 7th Trade node

create (t:Trade {id:7, qty:107, prc:23.44, pId:2}) return t;

it did not connect itself to "Yezdi-2" portfolio. What am i missing. Any help would be much appreciated. Thanks.

Was it helpful?

Solution 2

I think for higher volumes you want to split it up.

I don't think you need the id's when the other identifiers/names are enough.

  1. Create index / constraints per client, portfolio, trade separately
  2. Create clients separately.
  3. Create portfolios and connection to client
  4. pass in a collection of trades, use FOREACH to create the trades and connect them to client and portfolios

Create client

create (c:Client {name:{name}})

Create Portfolio

MATCH (c:Client {name:{name}})
FOREACH (p in {portfolios} |
  MERGE (c)-[:HAS_PORT]->(:Portfolio {name:p.name})

Create Trades

FOREACH (t in {trades} |
  MERGE (p:Portfolio {name: t.portfolio})
  MERGE (p)-[:HAS_TRADE]->(:Trade {qty:t.quantity, prc:t.price})
)

And use parameters in cypher against the server or embedded API

OTHER TIPS

I'd suggest looking at MERGE instead of CREATE UNIQUE since you're using Cypher 2.

Anyway, if you added the 7th trade note AFTER you ran those two CREATE UNIQUE statements above, you're going to have to re-run that second CREATE UNIQUE statement (again, though, since you're blanketing everything in that MATCH, try changing CREATE UNIQUE to MERGE). MERGE will be sure to only create a node/relationship if it doesn't already exist (i.e. none can be matched in the MERGE pattern).

Also, it looks as though you're creating things in a very RDBMS kind of style, which is a habit you should try to get out of. It's ok to specify nodes and relationships at creation time, e.g.

MERGE (c:Client {name: "Yezdi"})-[:HAS_PORT]->(p:Portfolio {name: "Yezdi-2"};
MERGE (t:Trade {qty:107, prc:23.44})<-[:HAS_TRADE]-(p);
MERGE (t:Trade {qty:100, prc:23.44})<-[:HAS_TRADE]-(p);
...

Try not to worry so much about "joins" in the RDBMS sense. You likely don't need to worry too much about the individual node IDs, unless there's an external purpose in your code for some reason (and even, I wouldn't worry too much).

HTH

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top