[Neo4j] Modeling inheritance in Neo4J
Hi, I'm looking for best practices in modeling inheritance in Neo4J. Suppose I have a Person class. In the DB I naturally have a node for each person. I'm following the design guide, and created a PEOPLE subreference node, connected to all Person nodes (A and B in the diagram below) +--- A | REF -- PEOPLE --+ | +--- B Now suppose I introduce a User class, that extends Person. I thought about a couple of options: 1. I could just connect the user nodes to the PEOPLE node, but then it would be more difficult to traverse only users. 2. Adding another layer of sub-referencing +--- USERS +-- C | | REF -- PEOPLE --+ +-- D | +--- NONUSERS -+-- A | +-- B So there's the USERS node that is connected to all user nodes, and a NONUSERS node that is connected to all of the concrete Person nodes. This way the Person and User factories handle the connections to each subreference node. The downside of this is that as I introduce more types in the class hierarchy I need to start rewiring everything, which can be quite tedious at present and very costly in the long run. 3. Perhaps a better way is to avoid the NONUSERS node, keep the concrete People (A and B in this example) connected to the PEOPLE node [Option2]: +--- USERS +-- C | | REF -- PEOPLE --+ +-- D | +---+-- A | +-- B Of course, I'd use different relationship types so I can tell between people and and subreferences connected to PEOPLE. I'm sure these aren't the only ways, and there may be different ways to handle different cases. In any event, I'd appreciate your feedback, as well as any additional tips related to inheritance. Also, if there's any Neo4J guide for managing inheritance that I missed, it would be great if someone would point me out to it. Thanks, --- Yaniv ___ Neo4j mailing list User@lists.neo4j.org https://lists.neo4j.org/mailman/listinfo/user
Re: [Neo4j] Modeling inheritance in Neo4J
Another factor to consider is multiple inheritance, where you are grouping the people into overlapping groups. For example, you can have the users/non.users split, but also perhaps a man/woman split, and this results in four types. If you are concerned about having to restructure the graph when adding new types, then I would suggest using a tag approach. This means you forget about inheritance, keep all person nodes directly under people, and add tags to the person as and when you need to. There are two ways to add a tag: - Have a ref-tags-tag tree along side your ref-people-person tree, and then you can cross link individual tag nodes to person nodes as an when you want. This is very dynamic and easy to maintain. If your GUI allows users to add tags, you can support that also, by adding missing tags if needed. - Use lucene, by adding a tags string property to the person and indexing that. This allows for free-form tagging, but does make the whole thing less definitive. My preference, as usual with such things, is to build a graph for it :-) (ie. the tag tree). On Thu, Nov 25, 2010 at 12:49 PM, Yaniv Ben Yosef yani...@gmail.com wrote: Hi, I'm looking for best practices in modeling inheritance in Neo4J. Suppose I have a Person class. In the DB I naturally have a node for each person. I'm following the design guide, and created a PEOPLE subreference node, connected to all Person nodes (A and B in the diagram below) +--- A | REF -- PEOPLE --+ | +--- B Now suppose I introduce a User class, that extends Person. I thought about a couple of options: 1. I could just connect the user nodes to the PEOPLE node, but then it would be more difficult to traverse only users. 2. Adding another layer of sub-referencing +--- USERS +-- C | | REF -- PEOPLE --+ +-- D | +--- NONUSERS -+-- A | +-- B So there's the USERS node that is connected to all user nodes, and a NONUSERS node that is connected to all of the concrete Person nodes. This way the Person and User factories handle the connections to each subreference node. The downside of this is that as I introduce more types in the class hierarchy I need to start rewiring everything, which can be quite tedious at present and very costly in the long run. 3. Perhaps a better way is to avoid the NONUSERS node, keep the concrete People (A and B in this example) connected to the PEOPLE node [Option2]: +--- USERS +-- C | | REF -- PEOPLE --+ +-- D | +---+-- A | +-- B Of course, I'd use different relationship types so I can tell between people and and subreferences connected to PEOPLE. I'm sure these aren't the only ways, and there may be different ways to handle different cases. In any event, I'd appreciate your feedback, as well as any additional tips related to inheritance. Also, if there's any Neo4J guide for managing inheritance that I missed, it would be great if someone would point me out to it. Thanks, --- Yaniv ___ Neo4j mailing list User@lists.neo4j.org https://lists.neo4j.org/mailman/listinfo/user ___ Neo4j mailing list User@lists.neo4j.org https://lists.neo4j.org/mailman/listinfo/user
Re: [Neo4j] Modeling inheritance in Neo4J
Craig, I like the tag tree idea. Quite flexible indeed. Thanks! --- Yaniv On Thu, Nov 25, 2010 at 1:59 PM, Craig Taverner cr...@amanzi.com wrote: Another factor to consider is multiple inheritance, where you are grouping the people into overlapping groups. For example, you can have the users/non.users split, but also perhaps a man/woman split, and this results in four types. If you are concerned about having to restructure the graph when adding new types, then I would suggest using a tag approach. This means you forget about inheritance, keep all person nodes directly under people, and add tags to the person as and when you need to. There are two ways to add a tag: - Have a ref-tags-tag tree along side your ref-people-person tree, and then you can cross link individual tag nodes to person nodes as an when you want. This is very dynamic and easy to maintain. If your GUI allows users to add tags, you can support that also, by adding missing tags if needed. - Use lucene, by adding a tags string property to the person and indexing that. This allows for free-form tagging, but does make the whole thing less definitive. My preference, as usual with such things, is to build a graph for it :-) (ie. the tag tree). On Thu, Nov 25, 2010 at 12:49 PM, Yaniv Ben Yosef yani...@gmail.com wrote: Hi, I'm looking for best practices in modeling inheritance in Neo4J. Suppose I have a Person class. In the DB I naturally have a node for each person. I'm following the design guide, and created a PEOPLE subreference node, connected to all Person nodes (A and B in the diagram below) +--- A | REF -- PEOPLE --+ | +--- B Now suppose I introduce a User class, that extends Person. I thought about a couple of options: 1. I could just connect the user nodes to the PEOPLE node, but then it would be more difficult to traverse only users. 2. Adding another layer of sub-referencing +--- USERS +-- C | | REF -- PEOPLE --+ +-- D | +--- NONUSERS -+-- A | +-- B So there's the USERS node that is connected to all user nodes, and a NONUSERS node that is connected to all of the concrete Person nodes. This way the Person and User factories handle the connections to each subreference node. The downside of this is that as I introduce more types in the class hierarchy I need to start rewiring everything, which can be quite tedious at present and very costly in the long run. 3. Perhaps a better way is to avoid the NONUSERS node, keep the concrete People (A and B in this example) connected to the PEOPLE node [Option2]: +--- USERS +-- C | | REF -- PEOPLE --+ +-- D | +---+-- A | +-- B Of course, I'd use different relationship types so I can tell between people and and subreferences connected to PEOPLE. I'm sure these aren't the only ways, and there may be different ways to handle different cases. In any event, I'd appreciate your feedback, as well as any additional tips related to inheritance. Also, if there's any Neo4J guide for managing inheritance that I missed, it would be great if someone would point me out to it. Thanks, --- Yaniv ___ Neo4j mailing list User@lists.neo4j.org https://lists.neo4j.org/mailman/listinfo/user ___ Neo4j mailing list User@lists.neo4j.org https://lists.neo4j.org/mailman/listinfo/user ___ Neo4j mailing list User@lists.neo4j.org https://lists.neo4j.org/mailman/listinfo/user