是否可以在repeat()。until()上排除顶点?

时间:2019-01-28 15:56:03

标签: apache gremlin tinkerpop3

我正在尝试提出一个gremlin查询,在这里我可以推断出谁知道杰克,但排除了直接来自员工的信息。

谁可以给杰克提供参考? 无法从该主题的其他雇员或亲戚那里获得参考。

查询应返回:

  • Jim认识Mary认识杰克

  • Jim知道Mary受雇于IBM受雇于John受雇于可口可乐受雇于Jack

  • Jerry认识John雇用可口可乐雇用了Jack

但是它不应该包括:

  • Jerry认识Jacks的Borther Family Jack

  • 可瑞可乐公司雇员杰里

  • Jeane Degree_from普林斯顿学位_from杰克

请参见下图: enter image description here

我想出了以下查询,但是我找不到从路径中排除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)

2 个答案:

答案 0 :(得分:0)

如果我正确理解了您的规则,那么这应该是您要查找的查询:

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]

但是,这与您的预期结果不符,因此,我认为我们仍然需要澄清(一旦弄清答案,我将更新答案)。

更新(路径中必须包含未由杰克的任何雇员雇用的人员):

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]

答案 1 :(得分:-1)

这是我想出的正确答案

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)