GraphDB: Node as a property of a relationship

蹲街弑〆低调 提交于 2021-02-10 14:13:59

问题


I am trying to analyze graphDB as an alternative to RDBMS for a problem domain. Here is the analogy of a problem I am trying to solve.

P:Michael and P:Angela r:like_to_eat G:Apple and G:Bread. G:Apple and G:Bread are r:available_in S:Walmart and S:Whole Foods. So far it's straightforward. Here is an image that I think best expresses the graph.

The problem is when I try to specify that Angela likes Apples from Whole Foods and Bread from Walmart. And Michael likes to eat Apples from Walmart and Bread from Whole Foods. How can I represent something like that in a graph? It sounds like I need the concept of a hypergraph to be able to solve this problem, but I have also heard that any hypergraph problem can be solved with property graph too. Can this be solved using standard graph solutions like Neo4j or CosmosDB? Can someone please help me with this


回答1:


You can "reify" the 3-way relationship (between Person, Grocery, and Store) in a Preference node (say), resulting in a data model like this:

In neo4j, you can use this Cypher query to represent "Angela likes Apples from Whole Foods and Bread from Walmart":

MERGE (angela:Person {name: 'Angela'})
MERGE (apple:Grocery {name: 'Apple'})
MERGE (bread:Grocery {name: 'Bread'})
MERGE (wf:Store {name: 'Whole Foods'})
MERGE (wm:Store {name: 'Walmart'})
CREATE
  (angela)-[:LIKES]->(pref1:Preference),
  (pref1)-[:ITEM]->(apple),
  (pref1)-[:AT_STORE]->(wf),
  (angela)-[:LIKES]->(pref2:Preference),
  (pref2)-[:ITEM]->(bread),
  (pref2)-[:AT_STORE]->(wm)



回答2:


Another alternative is to represent "Whole Foods" from "Angela likes Apples from Whole Foods" as a property of the edge "likes to eat", which becomes a real "property graph". Here is the data model:

In Nebula Graph (which is a graph database slution), you can use the following nGQL query for modelling:

// Define the schema
create tag person(name string)
create tag grocery(name string)
create tag store(name string)
create edge likes(storeID int)
create edge sells()

// Insert the vertices
INSERT VERTEX person(name) VALUES 100:("Michael");
INSERT VERTEX person(name) VALUES 101:("Angela");
INSERT VERTEX grocery(name) VALUES 200:("Apple");
INSERT VERTEX grocery(name) VALUES 201:("Bread");
INSERT VERTEX store(name) VALUES 300:("Walmart");
INSERT VERTEX store(name) VALUES 301:("Whole Foods");


// Insert the edges
INSERT EDGE likes(storeID) VALUES 101->200:(301);
INSERT EDGE likes(storeID) VALUES 101->201:(300);
INSERT EDGE sells() VALUES 300->200:();
INSERT EDGE sells() VALUES 300->201:();
INSERT EDGE sells() VALUES 301->200:();
INSERT EDGE sells() VALUES 301->201:();

To find which store's apples Angela likes

> GO FROM 101 OVER likes where likes._dst==200 YIELD likes.storeID as storeID | FETCH PROP ON store $-.storeID

To find how many groceries that Angela likes at Walmart

> GO FROM 101 OVER likes WHERE likes.storeID = 300

Hope that helps.



来源:https://stackoverflow.com/questions/61395285/graphdb-node-as-a-property-of-a-relationship

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!