以下是用节点和边表示的复杂的家庭成员关系。
person
节点的成员marriage
节点代表两个person
节点之间的婚姻son_of
和daughter_of
将人连接到marriage
节点。root_person
是第一代john
和Brenda
是第二代mark
是第三代mary
是第四代john
有前妻和妻子mark
和他的前妻mark_ex_wife
是堂兄弟。这是因为mark
的父亲和mark_ex_wife
的母亲是兄弟姐妹。我的要求是以分层树格式获取所有节点。从root_person
开始,他的儿子john
和他的妻子以及女儿brenda
'和她的丈夫应返回。然后是下一代等等...
请回答此模型是否可以满足我的要求,或修改此模型以获取所有节点。 CosmosDB数据浏览器返回的带有key
和value
的JSON是开始。然后,我可以解析查询返回的JSON,以自定义格式创建父母和孩子的分层树,这种树更易于理解和打印。
g.V().drop()
g.E().drop()
g.addV('person').property(id, 'root_person').property('name', 'Root Person').property('title', 'male')
g.addV('person').property(id, 'root_person_wife').property('name', 'root_person_wife').property('title', 'female')
g.addV('person').property(id, 'john').property('name', 'john').property('title', 'male')
g.addV('person').property(id, 'john_wife').property('name', 'john_wife').property('title', 'female')
g.addV('person').property(id, 'john_ex_wife').property('name', 'john_ex_wife').property('title', 'female')
g.addV('person').property(id, 'brenda').property('name', 'brenda').property('title', 'female')
g.addV('person').property(id, 'brenda_husband').property('name', 'brenda_husband').property('title', 'male')
g.addV('person').property(id, 'mark').property('name', 'mark').property('title', 'male')
g.addV('person').property(id, 'mark_ex_wife').property('name', 'mark_ex_wife').property('title', 'female')
g.addV('person').property(id, 'mark_wife').property('name', 'mark_wife').property('title', 'female')
g.addV('person').property(id, 'mary').property('name', 'mary').property('title', 'female')
g.addV('person').property(id, 'mary_husband').property('name', 'mary Husband').property('title', 'male')
g.addV('marriage').property(id, 'root_person-root_person_wife').property('marriage_name', 'root_person-root_person_wife')
g.addV('marriage').property(id, 'john-john_wife').property('marriage_name', 'john-john_wife')
g.addV('marriage').property(id, 'john-john_ex_wife').property('marriage_name', 'john-john_ex_wife')
g.addV('marriage').property(id, 'brenda-brenda_husband').property('marriage_name', 'brenda-brenda_husband')
g.addV('marriage').property(id, 'mark-mark_ex_wife').property('marriage_name', 'mark-mark_ex_wife')
g.addV('marriage').property(id, 'mark-mark_wife').property('marriage_name', 'mark-mark_wife')
g.addV('marriage').property(id, 'mary-mary_husband').property('marriage_name', 'mary-mary_husband')
g.V('root_person').addE('married_in').to(g.V('root_person-root_person_wife'))
g.V('root_person_wife').addE('married_in').to(g.V('root_person-root_person_wife'))
g.V('john').addE('married_in').to(g.V('john-john_wife'))
g.V('john_wife').addE('married_in').to(g.V('john-john_wife'))
g.V('john').addE('married_in').to(g.V('john-john_ex_wife'))
g.V('john_ex_wife').addE('married_in').to(g.V('john-john_ex_wife'))
g.V('brenda').addE('married_in').to(g.V('brenda-brenda_husband'))
g.V('brenda_husband').addE('married_in').to(g.V('brenda-brenda_husband'))
g.V('mark').addE('married_in').to(g.V('mark_#mark_wife'))
g.V('mark_wife').addE('married_in').to(g.V('mark-mark_wife'))
g.V('mark').addE('married_in').to(g.V('mark-mark_ex_wife'))
g.V('mark_ex_wife').addE('married_in').to(g.V('mark-mark_ex_wife'))
g.V('mary').addE('married_in').to(g.V('mary-mary_husband'))
g.V('mary_husband').addE('married_in').to(g.V('mary-mary_husband'))
g.V('john').addE('son_of').to(g.V('root_person-root_person_wife')).property('son_of', 'john--root_person-root_person_wife')
g.V('brenda').addE('daughter_of').to(g.V('root_person-root_person_wife')).property('daugter_of', 'brenda--root_person-root_person_wife')
g.V('mark').addE('son_of').to(g.V('john-john_wife')).property('son_of', 'mark--john-john_wife')
g.V('mark_ex_wife').addE('daughter_of').to(g.V('brenda-brenda_husband')).property('daugter_of', 'mark_ex_wife--brenda-brenda_husband')
g.V('mary').addE('daughter_of').to(g.V('mark-mark_wife')).property('daugter_of', 'mary--mark-mark_wife') }
更新:
我在cosmosdb
查询浏览器中尝试了以下查询,但没有引入所有节点。
g.V('root_person').repeat(out('married_in')).emit().repeat(__.in('son_of', 'daughter_of')).emit().tree()
查询产生以下json。您会注意到它在第二代停止。
[
{
"root_person": {
"key": {
"id": "root_person",
"label": "person",
"type": "vertex",
"properties": {
"name": [
{
"id": "f1e8327e-add4-454b-bbb2-6f076a29ea49",
"value": "Root Person"
}
],
"title": [
{
"id": "42fc9c2b-b613-4011-9a0a-183d907a96ac",
"value": "male"
}
]
}
},
"value": {
"root_person-root_person_wife": {
"key": {
"id": "root_person-root_person_wife",
"label": "marriage",
"type": "vertex",
"properties": {
"marriage_name": [
{
"id": "30f428a9-e657-4284-b59a-b836d9f04887",
"value": "root_person-root_person_wife"
}
]
}
},
"value": {
"john": {
"key": {
"id": "john",
"label": "person",
"type": "vertex",
"properties": {
"name": [
{
"id": "64e4653d-6298-479d-ba0f-eb3a9ede2383",
"value": "john"
}
],
"title": [
{
"id": "b99dacb3-e38d-4da8-b087-42860d2b28c0",
"value": "male"
}
]
}
},
"value": {}
},
"brenda": {
"key": {
"id": "brenda",
"label": "person",
"type": "vertex",
"properties": {
"name": [
{
"id": "1d88e8a5-7340-4b6d-a2b9-8b3c325e7bf6",
"value": "brenda"
}
],
"title": [
{
"id": "9b856caa-0b84-413c-8229-8e2c1e0cb0d3",
"value": "female"
}
]
}
},
"value": {}
}
}
}
}
}
}
]
答案 0 :(得分:2)
一棵树将代表与存储在图中的顶点相同的层次结构中的顶点。因此,配偶在树中不会出现在同一级别。我可能错了,但我认为您想要的是每一代人的名单。
gremlin> g.V("root_person").
union(identity(),
out("married_in").in("married_in")).dedup().
aggregate("x").
group("m").
by(constant(-1)).
by(id).
repeat(out("married_in").
union(__.in("married_in"),
__.in("daughter_of","son_of").
union(identity(),
out("married_in").in("married_in"))).dedup().
where(without("x")).
aggregate("x").
group("m").
by(loops()).
by(id)).
cap("m").unfold().
order(local).
by(keys).
select(values)
==>[root_person,root_person_wife]
==>[brenda,brenda_husband,john,john_wife,john_ex_wife]
==>[mark_ex_wife,mark,mark_wife]
==>[mary,mary_husband]