图形数据库模式设计 - 这适用于neo4j吗?

时间:2022-12-10 12:54:02

Scenario: a simple address book where a user can create his own contacts and organize them by adding them in groups. A contact may have multiple addresses.

场景:一个简单的地址簿,用户可以创建自己的联系人,并通过在组中添加它们来组织它们。联系人可能有多个地址。

I have created the following diagram: ![schema-design][1]

我创建了以下图表:[schema-design] [1]

I want to query all the contacts who are placed in group x and live in country y.

我想查询放在x组中并居住在y国的所有联系人。

Is this schema design good enough for those purposes (I want to use the neo4j database)?

这种架构设计是否足以满足这些目的(我想使用neo4j数据库)?

2 个解决方案

#1


3  

It looks like the notion of country should be a first class citizen in your graph since your query depends on it. Graph model design typically gets influenced a lot by your query patterns.

看起来国家的概念应该是图表中的一等公民,因为您的查询依赖于它。图形模型设计通常会受到查询模式的影响。

So I suggest to have a node labeled Country for each country and connect the Address node with :LOCATED_IN relationships to the country. (consequently drop the country property from the address nodes).

所以我建议为每个国家/地区设置一个标记为Country的节点,并将Address节点与:LOCATED_IN关系连接到该国家/地区。 (从而从国家/地区节点中删除国家/地区属性)。

With that change your query is as easy as:

通过该更改,您的查询就像以下一样简单:

MATCH (:Group{name:'family'})<-[:placed_in_group]-(contact)-[:lives-at]->()-[:LOCATED_IN]->(:Country{name:'US'})
RETURN contact

#2


2  

One option is to have another node from address for country, as pointed out by stefan Armbruster. If you do not want to change the data structure, just add an index to the field "country" of Address. Then you can have a query

如stefan Armbruster所指出的,一种选择是从国家地址获得另一个节点。如果您不想更改数据结构,只需在Address的“country”字段中添加索引即可。然后你可以有一个查询

MATCH (:Group{name:'family'})<-[:placed_in_group]-(contact)-[:lives-at]->(:Address{country:'US'})

#1


3  

It looks like the notion of country should be a first class citizen in your graph since your query depends on it. Graph model design typically gets influenced a lot by your query patterns.

看起来国家的概念应该是图表中的一等公民,因为您的查询依赖于它。图形模型设计通常会受到查询模式的影响。

So I suggest to have a node labeled Country for each country and connect the Address node with :LOCATED_IN relationships to the country. (consequently drop the country property from the address nodes).

所以我建议为每个国家/地区设置一个标记为Country的节点,并将Address节点与:LOCATED_IN关系连接到该国家/地区。 (从而从国家/地区节点中删除国家/地区属性)。

With that change your query is as easy as:

通过该更改,您的查询就像以下一样简单:

MATCH (:Group{name:'family'})<-[:placed_in_group]-(contact)-[:lives-at]->()-[:LOCATED_IN]->(:Country{name:'US'})
RETURN contact

#2


2  

One option is to have another node from address for country, as pointed out by stefan Armbruster. If you do not want to change the data structure, just add an index to the field "country" of Address. Then you can have a query

如stefan Armbruster所指出的,一种选择是从国家地址获得另一个节点。如果您不想更改数据结构,只需在Address的“country”字段中添加索引即可。然后你可以有一个查询

MATCH (:Group{name:'family'})<-[:placed_in_group]-(contact)-[:lives-at]->(:Address{country:'US'})