Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Neo4j node property type

I'm playing around with neo4j, and I was wondering, is it common to have a type property on nodes that specify what type of Node it is? I've tried searching for this practice, and I've seen some people use name for a purpose like this, but I was wondering if it was considered a good practice or if indexes would be the more practical method?

An example would be a "User" node, which would have type: user, this way if the index was bad, I would be able to do an all-node scan and look for types of user.

like image 691
Nicholas Avatar asked Apr 20 '12 03:04

Nicholas


2 Answers

Labels have been added to neo4j 2.0. They fix this problem.

You can create nodes with labels:

CREATE (me:American {name: "Emil"}) RETURN me;

You can match on labels:

MATCH (n:American)
WHERE n.name = 'Emil'
RETURN n

You can set any number of labels on a node:

MATCH (n)
WHERE n.name='Emil'
SET n :Swedish:Bossman
RETURN n

You can delete any number of labels on a node:

MATCH (n { name: 'Emil' })
REMOVE n:Swedish

Etc...

like image 84
joslinm Avatar answered Sep 20 '22 14:09

joslinm


True, it does depend on your use case. If you add a type property and then wish to find all users, then you're in potential trouble as you've got to examine that property on every node to get to the users. In that case, the index would probably do better- but not in cases where you need to query for all users with conditions and relations not available in the index (unless of course, your index is the source of the "start"). If you have graphs like mine, where a relation type implies two different node types like A-(knows)-(B) and A or B can be a User or a Customer, then it doesn't work.

So your use case is really important- it's easy to model graphs generically, but important to "tune" it as per your usage pattern.

like image 26
Luanne Avatar answered Sep 17 '22 14:09

Luanne