2

The Entity-Attribute-Value (EAV) model is really powerful, but complex to implement using SQL, so people often look for alternatives to EAV. It seems like the perfect candidate for graph databases. I understand how to build a movie database where you have nodes with the Neo4j label "Movie" with the property "release_date" right on the node. How would you make this more generic, such that movies have the Neo4j label "Entity" following the general EAV model?

Logical Fallacy
  • 3,017
  • 5
  • 25
  • 41

2 Answers2

1

I've thought a lot about this, but I'm not confident I have a good solution. I'll take a stab at it anyway. Here's the most basic model:

<node>                         <relationship>    <node>
Attribute                  --> :VALUE        --> Entity
name="Label",type="string" --> value="Movie" --> name="The Matrix"

With this model, you can write code for how to display and edit Attribute.type. For example, maybe all labels have a text field with finite options on the front-end and all dates have a date-picker. You could break Attribute.type out into its own node, Type, if that was preferable (particularly would make sense for handling composite types). In that case, you have the relationship TYPE between Attribute and Type nodes.

This becomes a problem if entities have multiple relationships, as is the case for reviews or if you want to relate the value to something else, such as the user who assigned the value. Now, I think, the relationship "VALUE" has to be it's own node of type "Value" (i.e. has the Neo4j label, "Value") with an incoming relationship from both Attribute and User nodes.

The full form has Type nodes, Attribute nodes, User nodes, Value nodes, and Entity nodes, where the relationships have basically no properties on them.

enter image description here

Logical Fallacy
  • 3,017
  • 5
  • 25
  • 41
1

Why do you need it in the first place?

I always thought that EAV was just a workaround for relational databases not being schema free.

Neo4j as other nosql databases is schema free, so you can just add the attributes that you want to both nodes and relationships.

If you need to you can also record the EAV model in a meta-schema within the graph but in most cases it is good enough if the meta-schema lives within the application that creates and uses your attributes.

Usually I treat labels as roles which in a certain context provide certain properties and relationships. A node can have many labels each of which representing one of those roles.

E.g. for the same node
:Person(name)-[:LIVES_IN]->(:City)
:Employee(empNo)-[:WORKS_AT]->(:Company)
:Developer()-[:HAS_SKILL]->(:CompSkill)
...

So in your case :Entity would just be a label that implies the name property.

And :Movie is a label that implies a release_date property and e.g. ACTED_IN relationships.

Michael Hunger
  • 41,339
  • 3
  • 57
  • 80
  • My naive understanding EAV is that it can allow the user to create arbitrary entities and attributes. Without that, would it be good practice to allow the user to create arbitrary relationships, properties, and labels? For example, say user MichaelHunger creates a "Batman" entity with a ComicBook label and a WRITTEN_BY relationship to another entity. How do you then interact with that arbitrary set of entities and relationships, not knowing ahead of time what they are? – Logical Fallacy Feb 16 '15 at 18:47
  • You would still have the meta-information stored either in your application or in the graph (as meta-graph) but the real data wouldn't be denormalized but just be part of the property graph model – Michael Hunger Feb 19 '15 at 01:39
  • You can even infer the meta-graph from the actual graph, here an simplistic example: http://gist.neo4j.org/?dropbox-14493611%2Fmeta_graph.adoc – Michael Hunger Feb 19 '15 at 01:39