1

Is it ok if we create self-referential nodes in neo4j? I need to use it in a big data environment so the performance really matters. I found an example here but need to know more opinions about this. Generally which one is better ? to use self reference nodes or to break them down to separate nodes with different labels and relations? My scenario is to create ecommerce tag groups and tags. A TAG_GROUP has TAGs , this is clear. However some Tags might have subtags. So there can be 2 solutions for this :

1) To use TAG-[has]-TAG self-referencial tag with some properties like {parentID,isSubTag,...}, the good thing about this is that I can go more than one sub-Tag level. However I am not sure how the performance will be.

2)To use TAH-[has]-SUB_TAG. Well maybe this one is simpler and easier to understand. But what if the SUB_TAG has a SUB_SUB tag itself?

Any help would be appreciated.

P.S.I am going to use neo4j 3.0.1 standalone server and Spring-Data-neo4j 4.1.1

Lina
  • 1,217
  • 1
  • 15
  • 28
  • I'm not sure this question is a good fit for the site since there is no one answer. I would recommend taking the approach that is cleanest and best fits your data model, only worrying about performance if you have problems in practice. I cannot discern why the two approaches you describe would perform differently; self-referencing or not should make no difference. – Mikesname May 10 '16 at 12:51

1 Answers1

2

Speaking outside of the use case of SDN, what you describe is a hierarchy, which can definitely be naturally modelled in Neo4J.

The notion of parent/child just have to be done with relationships, no need for properties.

(Tag)-[:CHILD]->(Tag)-[:CHILD]->(Tag)

I assume you would have products with associated tags, a Tag can then tags a product:

(Tag)-[:TAGS]->(Product)

Finding the associated tag for a product is as easy as :

MATCH (product:Product {id: 1})
OPTIONAL MATCH (product)<-[:TAGS]-(tag)
RETURN product, collect(tags) as tag

The OPTIONAL MATCH takes into account products not having tags without breaking the query

If you want the complete hierarchy of tags per tag tagging the product

MATCH (product:Product {id: 1})
OPTIONAL MATCH (product)<-[:TAGS]-()<-[:CHILD*0..]-(tag)
RETURN product, collect(tag) as tags

Here the 0 is taking into account the case where the tags doesn't have parents and will embed the () in the tags collection.

I suggest you the following reads for the modelling and Cypher parts, I'm sure someone can add an answer for the SDN side of this.

http://graphaware.com/neo4j/2013/10/11/neo4j-bidirectional-relationships.html

http://graphaware.com/graphaware/2015/05/19/neo4j-cypher-variable-length-relationships-by-example.html

Christophe Willemsen
  • 19,399
  • 2
  • 29
  • 36
  • Thanks Christopher. (Tag)-[:CHILD]->(Tag)-[:CHILD]->(Tag) is exactly the case that I needed to clarify. BTW links were very very useful ! Thanks for that ! – Lina May 11 '16 at 07:31