Neo4j node property type

2019-03-25 11:06发布

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.

7条回答
兄弟一词,经得起流年.
2楼-- · 2019-03-25 11:25

I'd say it's common practice. As an example, this is exactly how Spring Data Neo4j knows of which entity type a certain node is. Each node has "type" property that contains the qualified class name of the entity. These properties are automatically indexed in the "types" index, thus nodes can be looked up really fast. You could implement your use case exactly like this.

查看更多
迷人小祖宗
3楼-- · 2019-03-25 11:34

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.

查看更多
beautiful°
4楼-- · 2019-03-25 11:35

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...

查看更多
Emotional °昔
5楼-- · 2019-03-25 11:35

IMHO you shouldn't have to put a type property on the node. Instead, a common way to reference all nodes of a specific "type" is to connect all user nodes to a node called "Users" maybe. That way starting at the Users node, you can very easily find all user nodes. The "Users" node itself can be indexed so you can find it easily, or it can be connected to the reference node.

查看更多
在下西门庆
6楼-- · 2019-03-25 11:36

I think it's really up to you. Some people like indexed type attributes, but I find that they're mostly useful when you have other indexed attributes to narrow down the number of index hits (search for all users over age 21, for example).

That said, as @Luanne points out, most of us try to solve the problem in-graph first. Another way to do that (and the more natural way, in my opinion) is to use the relationship type to infer a practical node type, i.e. "A - (knows) -> B", so A must be a user or some other thing that can "know", and B must be another user, a topic, or some other object that can "be known".

查看更多
狗以群分
7楼-- · 2019-03-25 11:39

For client APIs, modeling the element type as a property makes it easy to instantiate the right domain object in your client-side code so I always include a type property on each node/vertex.

The "type" var name is commonly used for this, but in some languages like Python, "type" is a reserved word so I use "element_type" in Bulbs ( http://bulbflow.com/quickstart/#models ).

This is not needed for edges/relationships because they already contain a type (the label) -- note that Neo4j also uses the keyword "type" instead of label for relationships.

查看更多
登录 后发表回答