Basic question on relationship

36 views
Skip to first unread message

Newbie

unread,
Jan 6, 2012, 3:58:42 PM1/6/12
to Neo4j
Here is an example of what I am trying to do:

I have a user, the user has a photo and a book. And the user creates a
tag node called "scenary". The user needs to tag both the photo and
book with this tag node. Can I have a single relationship type of
"taggedWith" that connect a photo to a tag as well as a book to a tag?

Thanks,

Peter Neubauer

unread,
Jan 6, 2012, 4:22:58 PM1/6/12
to ne...@googlegroups.com
Hi there,
no, this is a case that others would describe a HyperEdge. That is, an
edge that connects more than two nodes. However, this is not too hard
to model in Neo4j, see
http://docs.neo4j.org/chunked/snapshot/cypher-cookbook.html#_hyperedges_and_cypher
for an example.

There is experimental code for building an API that can handle
Hyperedges natively, but I wouldn't recommend it just yet.

Cheers,

/peter neubauer

Google:    neubauer.peter
Skype:     peter.neubauer
Phone:     +46 704 106975
LinkedIn:  http://www.linkedin.com/in/neubauer
Twitter:    @peterneubauer
Tungle:    tungle.me/peterneubauer

brew install neo4j && neo4j start
heroku addons:add neo4j

Xia @ Internet

unread,
Jan 6, 2012, 4:54:09 PM1/6/12
to ne...@googlegroups.com
Hi Peter,

Thanks for the reply.

I got the HyperEdge part. Just try to confirm that a relationship type is unique for a unique combination of two node types, including two node types that are of the same type. I was thinking that one could use the same relationship type to link node type A to node type B and also to link node type C to node type B and distinguish the two via a "type" attribute on the relationship type.

Peter Neubauer

unread,
Jan 6, 2012, 4:57:15 PM1/6/12
to ne...@googlegroups.com
Yes,
that is another, maybe easier way to do it, depending on your modeling
and query requirements.

Cheers,

/peter neubauer

Google:    neubauer.peter
Skype:     peter.neubauer
Phone:     +46 704 106975
LinkedIn:  http://www.linkedin.com/in/neubauer
Twitter:    @peterneubauer
Tungle:    tungle.me/peterneubauer

brew install neo4j && neo4j start
heroku addons:add neo4j

Xia @ Internet

unread,
Jan 6, 2012, 5:06:12 PM1/6/12
to ne...@googlegroups.com
Thanks Peter.

I will try both ways. I may try to use generics with spring-data-neo4j and this pseudo polymorphism via the type attribute on the relationship. Really not sure how that going to work out :)


On Jan 6, 2012, at 4:22 PM, Peter Neubauer wrote:

Mattias Persson

unread,
Jan 9, 2012, 7:25:36 AM1/9/12
to ne...@googlegroups.com
Based on your description I see:

  (photo)-[:taggedWith]->(tag node)<-[:taggedWith]-(user)

is that correct? Then in that case the answer is yes you can.

2012/1/6 Newbie <xia.in...@gmail.com>



--
Mattias Persson, [mat...@neotechnology.com]
Hacker, Neo Technology
www.neotechnology.com

xia.in...@gmail.com

unread,
Jan 9, 2012, 9:44:55 AM1/9/12
to ne...@googlegroups.com
Hi Mattias,

That's exactly right. It is great to hear that it is doable.

My followup question would be if this can be modeled by spring-data-neo4j? I doubt it as spring-data modeling requires a starting node and an ending node. I might be able to use generics. Anybody has insight on this?

Thanks,

Mattias Persson

unread,
Jan 9, 2012, 9:46:57 AM1/9/12
to ne...@googlegroups.com
In my example there are three nodes: photo, tag node, user and two [taggedWith] relationships. Were you thinking about modeling this with one relationship?

xia.in...@gmail.com

unread,
Jan 9, 2012, 10:08:34 AM1/9/12
to ne...@googlegroups.com
I am not sure what is the best way to go with that. I am thinking to model it with one relationship with an attribute of type. So, this will introduce two types: photo, user for taggedWith relation.

I think we shall be good if using native Neo4J APIs with some custom code around it to handle different types.

With SDN, is it possible to define a generic relationship as follows:



@RelationshipEntity(type="taggedWith")
public class TaggedWith<T>
{
   @GraphId
   public Long id;

   @StartNode
   private T startNode;

   @EndNode
   private Tag tag;

   …
}

I probably should try it before I post here. Still want to ask, does anyone see any issue with this approach?

Thanks,
Reply all
Reply to author
Forward
0 new messages