12

It's possible to label the nodes in the new Neo4J 2.0.1, but does that same labelling work on relationships? If I have a relationship of the type :TO, can I create an index on its "statement" property like that?

 CREATE INDEX ON :TO(statement)

Thank you!

Aerodynamika
  • 7,883
  • 16
  • 78
  • 137

1 Answers1

9

a) you cannot have labels on relationships. A relationship has one type (which can be thought of kind of label). If you need multiple labels, you just create multiple relationships with different types

b) schema indexes on relationships are not possible. A graph query typically starts at nodes and not at relationships since nodes are the "things" in your domain. Starting at a relationship is IMHO often a indication to rethink your graph model. If you're still convinced you need relationship indexing, you can go with legacy indexing.

Stefan Armbruster
  • 39,465
  • 6
  • 87
  • 97
  • 9
    Hi Stefan, is there a plan to add both labels and schema indexes to relationships? We have a scenario where I think it does make sense to search on relationships and for them to have multiple labels. We are using nodes for the "things" in our domain (person, address etc) but the relationships contain metadata which is the "evidence" that we have for that relationship e.g. what type of document did it come from, document date, etc. We have a hierarchy of different document types. For now we can use multiple legacy indexes but it would be nicer to have labels and schema indexes. Thanks – Damon Horrell Mar 17 '14 at 00:51
  • 3
    I agree, limiting your users to have to rethink their graph model because they want to have metadata within their relationships is a bit too imposing. For instance, in my case, I need this data to be able to track 1) who created the relationship, 2) which context it was created in, 3) when it was created, 4) what was the statement within which it was created. What do you propose I do in that case? Thanks! – Aerodynamika Mar 17 '14 at 02:04
  • @Damon: treat the connection between your "things" using a node with ia IN and OUT relationship. Put labels and schema indexes on the "connection nodes". – Stefan Armbruster Mar 17 '14 at 09:45
  • @demmeetree: same principle as my previous comment: the relationships is refactored into a node and then you can relate it to context, statement and all other things. What you refer to as a relationship is in fact a thing on its own, so it should become a node. – Stefan Armbruster Mar 17 '14 at 09:47
  • 8
    @StefanArmbruster the thing is that what I refer to as a relationship IS in fact a relationship. Calling it a node is abstracting the already existing abstraction (nodes, relationship) even further and in this case the whole point of using Neo4J is gone for me, because I can just make 2 tables in some other database and represent that information there. That's why I don't want to change my graph model. Can you think of any other solution? Thank you! – Aerodynamika Mar 17 '14 at 14:11
  • 4
    I'm in agreement with deemeetree. With our current model we have meaningful relationships like Person--bought-->Property or Person--is a shareholder of-->Company. If we create intermediate nodes between these then we lose all meaning and everything just becomes Entity--appears in-->Document. I guess that we could add that meaning back in at query time though by a layer that sits on top of Neo4j. Is that your recommended approach to this problem? Surely it must be common to want to add metadata to relationships, particulary who created it and when, and to use that in queries. Thanks – Damon Horrell Mar 17 '14 at 20:40
  • I guess you're saying that the intermediate nodes BECOME the relationships so e.g. we'd have (Person)--[?]--(bought)--[?]-->(Property) instead of just (Person)--[bought]-->(Property) i.e. there would be node labels for all the "things" in our model plus labels for all the "real" relationships in our model. To me this just seems like a workaround though. It would be better if relationships had the same labels and querying capability as nodes do. – Damon Horrell Mar 17 '14 at 20:48
  • 2
    I too would like to see the ability to label relationships. A relationship can be one of many types, just as a node can be one of many types. Just as a node can be a person, who is an actor, I can think of the need to apply multiple labels to a single relationship. For example, the action of buying a house could be labeled as an investment, a purchase, a life decision or a simple transaction. The ability to apply labels to relationships allows us, among other things, to abstract predicates and query across a network based on varying context. The suggested alternatives make this difficult. – WildBill May 06 '14 at 02:42
  • Has anyone looked at altering the neo4j code to do this? If not, I'd be interested in doing this as it may be applicable in the pursuit of my PHD. – WildBill May 06 '14 at 02:43
  • @WildBill, any progress on this? – Daniel Krizian Aug 19 '14 at 11:06
  • No progress but I am about to start looking into this. Of course I am open to a discussion with someone on the opposing side that might try and convince me why this is a bad idea. I can provide a lengthy example of why I think this is a good idea and will gladly listen to why the alternative might be better. – WildBill Aug 26 '14 at 05:34
  • One possible solution I have considered is to use a lookup table that defines sets of relationships according to a possible label. For example, 'yells at', 'screams at', and 'talked to' could all be relationships that show how one entity comminucates with another, but the first 2 could be considered 'aggressive' in nature. If I wanted to query all nodes that exhibited an 'aggressive' communicative method I could have a macro or lookup table that grouped relationships accordingly. Of course the downside to this is I am merging my results outside of the DB and breaking the CMV paradigm. – WildBill Aug 26 '14 at 05:38
  • This answer is outdated. Relationships now allow labels and properties. – Jeff K Apr 05 '19 at 14:53
  • 1
    @JeffK I'm not so sure it's outdated. While you can have labels and properties on relationships, you still can't index relationship properties. I'm curious if labels are inherently indexed, though. – Adrian Keister Jun 22 '20 at 16:27