Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

neo4j - labels vs properties vs relationship + node

Any rule of thumb on where to use label vs node property vs relationship + node.

Let's have an example, say I have a store and I want to put my products in neo4j. Their identifier is the product sku, and I also want to have a categorization on them like this one is for clothes, food, electronics, and you get the idea. I'll be having a free search on my graph, like the user can search anything, and I'd return all the things related to that search string.

Would it be better to use:

  1. I have a node with sku 001, and I'll tag it a label of Food.
  2. I have a node with sku 001, and have property on this node called category:"Food"
  3. I have a node with sku 001, and I'll create another node for the Food, and will create a relationship of "category" to relate them.

I have read that if you'll be looking up a property, it's better off as a relationship + node, as traversing is much faster than looking up properties of node.

TIA

like image 415
lorraine Avatar asked Mar 12 '14 01:03

lorraine


People also ask

What is the role of building blocks like nodes relationships properties and labels in Neo4j?

Label associates a common name to a set of nodes or relationships. A node or relationship can contain one or more labels. We can create new labels to existing nodes or relationships. We can remove the existing labels from the existing nodes or relationships.

What are labels in Neo4j?

Neo4j recently introduced the concept of labels and their sidekick, schema indexes. Labels are a way of attaching one or more simple types to nodes (and relationships), while schema indexes allow to automatically index labelled nodes by one or more of their properties.

What are nodes and relationships in Neo4j?

The Neo4j property graph database model consists of: Nodes describe entities (discrete objects) of a domain. Nodes can have zero or more labels to define (classify) what kind of nodes they are. Relationships describes a connection between a source node and a target node.

Can relationships have properties in Neo4j?

The graphs in the Neo4j Graph Data Science Library support properties for relationships. We provide multiple operations to work with the stored relationship-properties in projected graphs. Relationship properties are either added during the graph projection or when using the mutate mode of our graph algorithms.


1 Answers

Whether you should use a property, a label or a node for the category depends on how you will be querying the data.

(I'll assume here that you have a fairly small, fairly fixed set of categories.)

Use a property if you won't be querying by category, but just need to return the category of a node that has been found by other means. (For example: what is the category of the item with sku 001?)

Use a label if you need to query by category. (For example: what are all the foods costing less than $10?)

Use a node if you need to traverse the category without knowing what it is. (For example: what are the ten most popular items in the same category as one that the user has chosen?)

like image 125
Ben Butler-Cole Avatar answered Sep 27 '22 18:09

Ben Butler-Cole