是否可以排除 repeat().until() 上的顶点?
Is it possible to exclude vertex on repeat().until()?
我正在尝试提出一个 gremlin 查询,我可以在其中推断出谁认识 Jack,但不包括直接来自 Employees 的信息。
谁可以在 Jack 上寻求参考?
不能从其他员工或受试者的家庭亲属那里获得推荐信。
查询应该return:
吉姆认识玛丽认识杰克
Jim 知道 Mary 受雇于 IBM 受雇于 John 受雇于 Coca Cola 受雇于 Jack
Jerry Knows John Employed Coca Cola employedd Jack
但不应该包括:
Jerry Knows Jacks 的兄弟家族 Jack
Jerry 雇用可口可乐雇用 Jack
珍妮Degree_from普林斯顿Degree_From杰克
见下图:
我提出了以下查询,但无法获得从路径中排除 Jerry 或 jane 的部分。
g.V().has('isSSEmployee',true).repeat(bothE('knows','employeed').otherV().simplePath()).until(has('name','Jack')).has('isSSEmployee',false).path().by('name').by(label)
加载数据的脚本
//only for tinkerpop
graph=TinkerGraph.open()
g=graph.traversal()
//from here on common between neptune and tinkerpop
//Setup started
//create vertexes
//create a person John
g.addV('person').property(id,'p1')
g.V('p1').property('questId','123456')
g.V('p1').property('name','John')
//create a person Jack
g.addV('person').property(id,'p2')
g.V('p2').property('questId','123457')
g.V('p2').property('name','Jack')
//create a person Mary
g.addV('person').property(id,'p3')
g.V('p3').property('questId','123458')
g.V('p3').property('name','Mary')
//create a person Jim and mark him as ssemployee
g.addV('person').property(id,'p4')
g.V('p4').property('name','Jim')
g.V('p4').property('isSSEmployee',true)
g.V('p4').property('questId','1234569')
//create a person Jerry and mark him as ssemployee
g.addV('person').property(id,'p5')
g.V('p5').property('name','Jerry')
g.V('p5').property('questId','12345700')
g.V('p5').property('isSSEmployee',true)
//create a person Jack's Brother
g.addV('person').property(id,'p6')
g.V('p6').property('name',"Jack's Brother")
g.V('p6').property('questId','1234')
//create Jeane our employee
g.addV('person').property(id,'p7')
g.V('p7').property('name', 'Jeane')
g.V('p7').property('questId', '1234580')
g.V('p7').property('isSSEmployee', true)
//create a company Coca Cola
g.addV('company').property(id,'c1')
g.V('c1').property('name','Coca Cola')
g.V('c1').property('questId','123456')
//create a company IBM
g.addV('company').property(id,'c2')
g.V('c2').property('name','IBM')
g.V('c2').property('questId','123457')
//create an university Princeton
g.addV('univeristy').property(id,'u1')
g.V('u1').property('name','Princeton')
//create edges
//Coca Cola employes John
g.addE('employeed').from(g.V('c1')).to(g.V('p1')).property(id,'c1p1')
g.E('c1p1').property('fromDate','2009-12-13').next()
g.E('c1p1').property('toDate','2019-12-13').next()
//Coca Cola employes Jack
g.addE('employeed').from(g.V('c1')).to(g.V('p2')).property(id,'c1p2')
g.E('c1p2').property('fromDate','2006-12-13').next()
g.E('c1p2').property('toDate','2009-12-12').next()
//Coca Cola employed Jerry our employee
g.addE('employeed').from(g.V('c1')).to(g.V('p5')).property(id,'c1p5')
g.E('c1p5').property('fromDate','2007-12-13').next()
g.E('c1p5').property('toDate','2008-12-13').next()
//IBM employees John
g.addE('employeed').from(g.V('c2')).to(g.V('p1')).property(id,'c2p1')
g.E('c2p1').property('fromDate','2006-12-13').next()
g.E('c2p1').property('toDate','2009-12-13').next()
//IBM employes Mary
g.addE('employeed').from(g.V('c2')).to(g.V('p3')).property(id,'c2p3')
g.E('c2p3').property('fromDate','2006-10-11').next()
g.E('c2p3').property('toDate','2009-10-11').next()
//Jim our employee knows Mary
g.addE('knows').from(g.V('p4')).to(g.V('p3')).property(id,'p4p3')
//Jerry our employee knows John
g.addE('knows').from(g.V('p5')).to(g.V('p1')).property(id,'p5p1').next()
//Mary knows Jack
g.addE('knows').from(g.V('p3')).to(g.V('p2')).property(id,'p3p2')
//Jerry our employee knows Jack's Brother
g.addE('knows').from(g.V('p5')).to(g.V('p6')).property(id,'p5p6')
//Jack's Brother is Jack's Brother (family relation)
g.addE('family').from(g.V('p6')).to(g.V('p2')).property(id,'p6p2')
//Jeane our employee got a degree from Princeton
g.addE('degree_from').from(g.V('p7')).to(g.V('u1')).property(id,'p7u1')
g.E('p7u1').property('class_of',1989)
//Jack got a degree from Princeton
g.addE('degree_from').from(g.V('p2')).to(g.V('u1')).property(id,'p2u1')
g.E('p2u1').property('class_of',1989)
g.V().has('isSSEmployee',true).
repeat(bothE().otherV().simplePath()).
until(has('name','Jack')).
path().
by('name').
by(label)
如果我没有正确理解您的规则,那么这应该是您要查找的查询:
gremlin> g.V().has('person','name','Jack').
bothE('knows','employeed').otherV().
sideEffect(hasLabel('company').aggregate('e')).barrier().
repeat(bothE('knows','employeed').otherV().simplePath()).
until(has('isSSEmployee',true).and().
not(__.in('employeed').where(within('e')))).
path().
by('name').
by(label).
map {it.get().objects().reverse()}
==>[Jim,knows,Mary,knows,Jack]
==>[Jim,knows,Mary,employeed,IBM,employeed,John,employeed,Coca Cola,employeed,Jack]
==>[Jim,knows,Mary,employeed,IBM,employeed,John,knows,Jerry,employeed,Coca Cola,employeed,Jack]
但是,这与您的预期结果不符,所以我认为我们仍然需要一些说明(一旦弄清楚,我会更新答案)。
UPDATE(路径必须包含未受雇于 Jack 的任何员工的人员):
gremlin> g.V().has('person','name','Jack').
......1> sideEffect(__.in('employeed').aggregate('c')).barrier().
......2> repeat(bothE('knows','employeed').as('e').otherV().simplePath().as('p')).
......3> emit(__.and(has('isSSEmployee',true),
......4> select(all,'e').and(unfold().hasLabel('knows'),
......5> count(local).is(gt(1))))).
......6> filter(select(all, 'p').unfold().
......7> not(__.in('employeed').where(within('c')))).
......8> path().
......9> by('name').
.....10> by(label).
.....11> map {it.get().objects().reverse()}
==>[Jim,knows,Mary,knows,Jack]
==>[Jerry,knows,John,employeed,Coca Cola,employeed,Jack]
==>[Jerry,knows,John,employeed,IBM,employeed,Mary,knows,Jack]
==>[Jerry,employeed,Coca Cola,employeed,John,employeed,IBM,employeed,Mary,knows,Jack]
==>[Jim,knows,Mary,employeed,IBM,employeed,John,employeed,Coca Cola,employeed,Jack]
==>[Jim,knows,Mary,employeed,IBM,employeed,John,knows,Jerry,employeed,Coca Cola,employeed,Jack]
这是我得出的正确答案
g.V().has('person','name','Jack').
repeat(bothE().has(label,neq('family')).otherV().hasNot('isSSEmployee').simplePath()).
until(has(label,'person')).
bothE().otherV().has('isSSEmployee',true).path().
by('name').
by(label)
我正在尝试提出一个 gremlin 查询,我可以在其中推断出谁认识 Jack,但不包括直接来自 Employees 的信息。
谁可以在 Jack 上寻求参考? 不能从其他员工或受试者的家庭亲属那里获得推荐信。
查询应该return:
吉姆认识玛丽认识杰克
Jim 知道 Mary 受雇于 IBM 受雇于 John 受雇于 Coca Cola 受雇于 Jack
Jerry Knows John Employed Coca Cola employedd Jack
但不应该包括:
Jerry Knows Jacks 的兄弟家族 Jack
Jerry 雇用可口可乐雇用 Jack
珍妮Degree_from普林斯顿Degree_From杰克
见下图:
我提出了以下查询,但无法获得从路径中排除 Jerry 或 jane 的部分。
g.V().has('isSSEmployee',true).repeat(bothE('knows','employeed').otherV().simplePath()).until(has('name','Jack')).has('isSSEmployee',false).path().by('name').by(label)
加载数据的脚本
//only for tinkerpop
graph=TinkerGraph.open()
g=graph.traversal()
//from here on common between neptune and tinkerpop
//Setup started
//create vertexes
//create a person John
g.addV('person').property(id,'p1')
g.V('p1').property('questId','123456')
g.V('p1').property('name','John')
//create a person Jack
g.addV('person').property(id,'p2')
g.V('p2').property('questId','123457')
g.V('p2').property('name','Jack')
//create a person Mary
g.addV('person').property(id,'p3')
g.V('p3').property('questId','123458')
g.V('p3').property('name','Mary')
//create a person Jim and mark him as ssemployee
g.addV('person').property(id,'p4')
g.V('p4').property('name','Jim')
g.V('p4').property('isSSEmployee',true)
g.V('p4').property('questId','1234569')
//create a person Jerry and mark him as ssemployee
g.addV('person').property(id,'p5')
g.V('p5').property('name','Jerry')
g.V('p5').property('questId','12345700')
g.V('p5').property('isSSEmployee',true)
//create a person Jack's Brother
g.addV('person').property(id,'p6')
g.V('p6').property('name',"Jack's Brother")
g.V('p6').property('questId','1234')
//create Jeane our employee
g.addV('person').property(id,'p7')
g.V('p7').property('name', 'Jeane')
g.V('p7').property('questId', '1234580')
g.V('p7').property('isSSEmployee', true)
//create a company Coca Cola
g.addV('company').property(id,'c1')
g.V('c1').property('name','Coca Cola')
g.V('c1').property('questId','123456')
//create a company IBM
g.addV('company').property(id,'c2')
g.V('c2').property('name','IBM')
g.V('c2').property('questId','123457')
//create an university Princeton
g.addV('univeristy').property(id,'u1')
g.V('u1').property('name','Princeton')
//create edges
//Coca Cola employes John
g.addE('employeed').from(g.V('c1')).to(g.V('p1')).property(id,'c1p1')
g.E('c1p1').property('fromDate','2009-12-13').next()
g.E('c1p1').property('toDate','2019-12-13').next()
//Coca Cola employes Jack
g.addE('employeed').from(g.V('c1')).to(g.V('p2')).property(id,'c1p2')
g.E('c1p2').property('fromDate','2006-12-13').next()
g.E('c1p2').property('toDate','2009-12-12').next()
//Coca Cola employed Jerry our employee
g.addE('employeed').from(g.V('c1')).to(g.V('p5')).property(id,'c1p5')
g.E('c1p5').property('fromDate','2007-12-13').next()
g.E('c1p5').property('toDate','2008-12-13').next()
//IBM employees John
g.addE('employeed').from(g.V('c2')).to(g.V('p1')).property(id,'c2p1')
g.E('c2p1').property('fromDate','2006-12-13').next()
g.E('c2p1').property('toDate','2009-12-13').next()
//IBM employes Mary
g.addE('employeed').from(g.V('c2')).to(g.V('p3')).property(id,'c2p3')
g.E('c2p3').property('fromDate','2006-10-11').next()
g.E('c2p3').property('toDate','2009-10-11').next()
//Jim our employee knows Mary
g.addE('knows').from(g.V('p4')).to(g.V('p3')).property(id,'p4p3')
//Jerry our employee knows John
g.addE('knows').from(g.V('p5')).to(g.V('p1')).property(id,'p5p1').next()
//Mary knows Jack
g.addE('knows').from(g.V('p3')).to(g.V('p2')).property(id,'p3p2')
//Jerry our employee knows Jack's Brother
g.addE('knows').from(g.V('p5')).to(g.V('p6')).property(id,'p5p6')
//Jack's Brother is Jack's Brother (family relation)
g.addE('family').from(g.V('p6')).to(g.V('p2')).property(id,'p6p2')
//Jeane our employee got a degree from Princeton
g.addE('degree_from').from(g.V('p7')).to(g.V('u1')).property(id,'p7u1')
g.E('p7u1').property('class_of',1989)
//Jack got a degree from Princeton
g.addE('degree_from').from(g.V('p2')).to(g.V('u1')).property(id,'p2u1')
g.E('p2u1').property('class_of',1989)
g.V().has('isSSEmployee',true).
repeat(bothE().otherV().simplePath()).
until(has('name','Jack')).
path().
by('name').
by(label)
如果我没有正确理解您的规则,那么这应该是您要查找的查询:
gremlin> g.V().has('person','name','Jack').
bothE('knows','employeed').otherV().
sideEffect(hasLabel('company').aggregate('e')).barrier().
repeat(bothE('knows','employeed').otherV().simplePath()).
until(has('isSSEmployee',true).and().
not(__.in('employeed').where(within('e')))).
path().
by('name').
by(label).
map {it.get().objects().reverse()}
==>[Jim,knows,Mary,knows,Jack]
==>[Jim,knows,Mary,employeed,IBM,employeed,John,employeed,Coca Cola,employeed,Jack]
==>[Jim,knows,Mary,employeed,IBM,employeed,John,knows,Jerry,employeed,Coca Cola,employeed,Jack]
但是,这与您的预期结果不符,所以我认为我们仍然需要一些说明(一旦弄清楚,我会更新答案)。
UPDATE(路径必须包含未受雇于 Jack 的任何员工的人员):
gremlin> g.V().has('person','name','Jack').
......1> sideEffect(__.in('employeed').aggregate('c')).barrier().
......2> repeat(bothE('knows','employeed').as('e').otherV().simplePath().as('p')).
......3> emit(__.and(has('isSSEmployee',true),
......4> select(all,'e').and(unfold().hasLabel('knows'),
......5> count(local).is(gt(1))))).
......6> filter(select(all, 'p').unfold().
......7> not(__.in('employeed').where(within('c')))).
......8> path().
......9> by('name').
.....10> by(label).
.....11> map {it.get().objects().reverse()}
==>[Jim,knows,Mary,knows,Jack]
==>[Jerry,knows,John,employeed,Coca Cola,employeed,Jack]
==>[Jerry,knows,John,employeed,IBM,employeed,Mary,knows,Jack]
==>[Jerry,employeed,Coca Cola,employeed,John,employeed,IBM,employeed,Mary,knows,Jack]
==>[Jim,knows,Mary,employeed,IBM,employeed,John,employeed,Coca Cola,employeed,Jack]
==>[Jim,knows,Mary,employeed,IBM,employeed,John,knows,Jerry,employeed,Coca Cola,employeed,Jack]
这是我得出的正确答案
g.V().has('person','name','Jack').
repeat(bothE().has(label,neq('family')).otherV().hasNot('isSSEmployee').simplePath()).
until(has(label,'person')).
bothE().otherV().has('isSSEmployee',true).path().
by('name').
by(label)