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.

Unexpected Model Behavior

renatospaka
Graph Voyager

Hello guys.

I'm modeling a health graph and got an unexpected behavior in my model.

There are three nodes - Test, TestResult and EMR. If you execute the following commands, you will see this schema;

// EMR
MERGE (e:EMR {id:toInteger(6700001)}) ON CREATE SET e.id=toInteger(6700001), e.observation='PEP';

// Test
MERGE (t:Test {id:toInteger(16000001)}) ON CREATE SET t.id=toInteger(16000001), t.description='Glucose';

// TestResult
MERGE (t:TestResult {id:toInteger(85000001)}) ON CREATE SET t.id=toInteger(85000001), t.description='Glucose', t.value=toFloat(96), t.measurementUnit='mg/dL', t.collectDate=date('2015-08-27'), t.source='serum';

// Test x TestResult - BELONGS_TO
MATCH (e:Test), (t:TestResult) WHERE t.id=85000001 AND e.id=16000001 MERGE (t)-[r:BELONGS_TO]->(e);

So far, so good.
Now I want that all Test nodes to belong to the EMR node, which is my wallet for medical tests. For that you may execute the following command:

// EMR x Test - BELONGS_TO
MATCH (e:EMR), (t:Test) WHERE t.id=16000001 AND e.id=6700001 MERGE (t)-[r:BELONGS_TO]->(e) SET r.releaseDate=date('2015-08-27');

I expected something like this as result of the command:

But what I've got is this:

However, if I just query for Test nodes, I get this (which is exactly what I want to.):

What I missed? What I did wrong? I really don't get why (in the schema visualization) there is a self-relation in Test Node and why there is a relation between TestResult and EMR if I didn't explicitly ask for it. In fact, I'm not sure if the graph would behave like the query or like the schema in the future when there will be millions of nodes and hundreds of millions of relations.

1 ACCEPTED SOLUTION

Could you send the table results for the query that has that extra relationship? Looks like there's more data, and probably some other merge/create statements that are causing the trouble here.

There's some cleanup to do...

  1. In your initial merge, you do not need to set that property again. The Merge will create a matching node, with the property, if it can't find one.
MERGE (e:EMR {id:toInteger(6700001)})
ON CREATE SET e.observation='PEP';
  1. You could greatly simplify all of your creation Cypher.
  2. Be very careful with your variable names, especially when using shorthand single-letter variables. Make your variables self-descriptive, and your life easier.
    You
MERGE (emr:EMR {id:toInteger(6700001)}) ON CREATE SET emr.observation='PEP'
MERGE (test:Test {id:toInteger(16000001)}) ON CREATE SET test.description='Glucose'
MERGE (result:TestResult {id:toInteger(85000001)}) ON CREATE SET result.description='Glucose', result.value=toFloat(96), result.measurementUnit='mg/dL', result.collectDate=date('2015-08-27'), result.source='serum'
MERGE (result)-[:BELONGS_TO]->(test)
MERGE (test)-[rel:BELONGS_TO]->(emr) ON CREATE SET rel.releaseDate=date('2015-08-27')

There's likely some other data and merge/create commands somewhere that are creating that extra relationship.
http://console.neo4j.org/?id=9z0wvl
2X_b_b80c8c5f268bb5f46a33e9484d9e92248ebcd890.png

View solution in original post

7 REPLIES 7

Interesting, my first guess, there might be a bug in db.schema.visualization?

I usually use

call apoc.meta.graph()

and here we see the expected model

2X_0_0df26d0b5ea6f9d4d1fada984886961bbb302bef.png

Joel.

Thanks for this apoc function. I really don't know much about Neo4j. And the best part is that the schema seems to display correctly now, take a look:

About db.schema.visualization, I cannot say if there is a bug in there. I guess not, but only a guess.

Thanks for your support.

I did a little more poking around at it, and YES, there is a bug in there.

After changing the relationship labels from :BELONGS_TO to :RES_TEST and :TEST_EMR, the result was as expected.

2X_9_961dbb4719f57986016544c6ae1867f8b591fd23.png

Hi @tony.chiboucas

Now I just use the procedure apoc.meta.graph , it is working fine. But it is very strange the schema.visualization() not working in that scenario.

I appreciate the effort on helping me to solve this.

Well, it is good news. But, for me, it continues not working.

Could you send the table results for the query that has that extra relationship? Looks like there's more data, and probably some other merge/create statements that are causing the trouble here.

There's some cleanup to do...

  1. In your initial merge, you do not need to set that property again. The Merge will create a matching node, with the property, if it can't find one.
MERGE (e:EMR {id:toInteger(6700001)})
ON CREATE SET e.observation='PEP';
  1. You could greatly simplify all of your creation Cypher.
  2. Be very careful with your variable names, especially when using shorthand single-letter variables. Make your variables self-descriptive, and your life easier.
    You
MERGE (emr:EMR {id:toInteger(6700001)}) ON CREATE SET emr.observation='PEP'
MERGE (test:Test {id:toInteger(16000001)}) ON CREATE SET test.description='Glucose'
MERGE (result:TestResult {id:toInteger(85000001)}) ON CREATE SET result.description='Glucose', result.value=toFloat(96), result.measurementUnit='mg/dL', result.collectDate=date('2015-08-27'), result.source='serum'
MERGE (result)-[:BELONGS_TO]->(test)
MERGE (test)-[rel:BELONGS_TO]->(emr) ON CREATE SET rel.releaseDate=date('2015-08-27')

There's likely some other data and merge/create commands somewhere that are creating that extra relationship.
http://console.neo4j.org/?id=9z0wvl
2X_b_b80c8c5f268bb5f46a33e9484d9e92248ebcd890.png

@tony, thanks for these suggestions, I rewrote my queries based on that.

Regarding the table results, there were just those 3 nodes in the entire database, so what you see is what you get. But, when I executed apoc.meta.graph, the schema was displayed correctly. Who figured...