Failed to get relationships with Neomodel and jexp-batch-import

1k views Asked by At

I'm using neomodel and the jexp-batch-importer(https://github.com/jexp/batch-import). My model looks like the following.

class TokenRel(StructuredRel):
    weight = IntegerProperty(default = 1)

class TokenNode(StructuredNode):
    identifier = StringProperty(unique_index = True, required = True)
    count = IntegerProperty(default = 1)
    occurence = Relationship('TokenNode', 'OCCURENCE', model = TokenRel) 

I've tried to import nodes and relationships with jexp-batch-importer to use the model given above afterwards.

My node.cvs looks like this:

 identifier:string:TokenNode    count:int
 spd    2
 cdu    3

and edge.csv:

identifier:string:TokenNode identifier:string:TokenNode occurence
spd cdu OCCURENCE
spd cdu OCCURENCE

The import to Neo4j works fine with 2 Nodes and 2 relationships. But I can't access the relationships in neomodel. See below:

spdNode = port.getNode('spd') #exists
cduNode = port.getNode('cdu') #exists

if spdNode.occurence.is_connected(cduNode): 
    print('Yes') # yes will be printed

print(spdNode.count) # 2
print(spdNode.occurence.count()) # print: 0 expected: 2
print(cduNode.occurence.count()) # 0

Is there a way to map the relationship to occurence? And is it possible to increase the weight for a edge instead of creating two edges while importing with batch-importer?

Regards.

EDIT:

I have analysed the structure which the batch-importer creates and the one from neomodel and it seems that neomodel do something strange. Insert for both two nodes and one relation between them.

Structure from Batch-Importer

nodes, id, lablel, count, identifier
1, 1 , /, / , /
2, 2, /, 2, spd
3, 3, /, 3, cdu

source target typ id label weight neo4j-relation
2, 3, direct, 1, /, 1, occurence

And here the one from neomodel:

nodes, id, label, category, count, identifier
1, 1, /, TokenNode, /, /
2, 2, /, /, 1, spd
3, 4, /, /, 1, cdu

source, target, type, id, label, weight, neo4j-relation, __instance__
1, 2, direct, 2, /, 1, Token_Node, check
1, 3, direct, 3, /, 1, Token_Node, check
3, 2, direct, 1, /, 1, occurence, unchecked 

So neomodel adds something like "category" and "instance" and have relations from node to all others. It also adds the "TokenNode" to the column "category". I think the batch-importer isn't compatible with neomodel :(

3

There are 3 answers

1
Michael Hunger On

Is there a way to map the relationship to occurence? And is it possible to increase the weight for a edge instead of creating two edges while importing with batch-importer?

It is possible in theory to update properties, but actually that would not be something to be generalized. You can do that either upfront on the csv files, or afterwards with a cypher statement.

Or you write your own variant of the batch-importer using the BatchInserter-Java-API (you can start by forking and modifying the batch-import repo).

I don't know if neomodel needs anything else on the relationship, e.g. type properties on the nodes or relationships. You can test with cypher that the import worked.

start cdu=node:TokenNode(identifier="cdu"),spd=node:TokenNode(identifier="spd")
match cdu-[r]-spd
return type(r);
0
rob On

Hi author of neomodel here.

The RelationshipManager uses CREATE UNIQUE when issuing a connect:

https://github.com/robinedwards/neomodel/blob/master/neomodel/relationship_manager.py#L119

Which means there will only ever be one relationship between two nodes of one type and direction.

However your welcome to subclass RelationshipManager to offer alternative behaviour.

Regarding batch-import, neomodel expects nodes of the same type to be categorised via a category node. In your example all your TokenNodes will need to be categorised, as a lot of the neomodel code assumes these relationships exist.

neomodel is an opinionated framework, by using it your kinda of buying into a certain way of thinkinh. However if you ping me (robsmoniker) on #neo4j on irc.freenode.org I can probably help you make the batch importer and neomodel play nice.

0
felipead On

I had the same issue. I spent hours trying to figure out if there was any bug or I simply did not understand the NeoModel API. I read the code and all of the documentation, but it was not clear to me.

After googling I finally found this question. And the NeoModel author answer it below.

It's very frustrating to not being able to create relationships to more than one node of the same class. This makes it impossible to use NeoModel with my domain.

The fact that it was not mentioned in the documentation made me lose several hours. It is also not clear when you take a look at the code. The fact there is a function called "all()" on each cardinality object is misleading.

After a day of lost work I'm dropping NeoModel and switching to pure py2neo 2.0.

I would love to find some time to help improve NeoModel though. It is the most advanced OGM for Neo4j out there.