Gremlin 查询重叠日期时间



我有以下图形数据库。链接到图形图像

这是加载数据的脚本

//Use case for datetimes working at the same company
//only for tinkerpop
graph=TinkerGraph.open()
g=graph.traversal()
//from here on common between neptune and tinkerpop
//Setup started
//create vertexes
//create a company Coca Cola
g.addV('company').property(id,'c1')
g.V('c1').property('name','Coca Cola')
g.V('c1').property('questId','10')
//create a person Alice
g.addV('person').property(id,'p1')
g.V('p1').property('questId','1')
g.V('p1').property('name','Alice')

//create a person Bob
g.addV('person').property(id,'p2')
g.V('p2').property('questId','2')
g.V('p2').property('name','Bob')

//create a person Chris
g.addV('person').property(id,'p3')
g.V('p3').property('questId','3')
g.V('p3').property('name','Chris')

//create a person David
g.addV('person').property(id,'p4')
g.V('p4').property('questId','4')
g.V('p4').property('name','David')
//create a person Emma
g.addV('person').property(id,'p5')
g.V('p5').property('questId','5')
g.V('p5').property('name','Emma')
//create edges
//Coca Cola employes Alice
g.addE('employeed').from(g.V('c1')).to(g.V('p1')).property(id,'c1p1')
g.E('c1p1').property('fromDate','2009-01-10').next()
g.E('c1p1').property('toDate','2018-12-13').next()
//create edges
//Coca Cola employes Bob
g.addE('employeed').from(g.V('c1')).to(g.V('p2')).property(id,'c1p2')
g.E('c1p2').property('fromDate','2015-11-13').next()
//create edges
//Coca Cola employes Chris
g.addE('employeed').from(g.V('c1')).to(g.V('p3')).property(id,'c1p3')
g.E('c1p3').property('fromDate','2017-01-10').next()
g.E('c1p3').property('toDate','2019-01-10').next()
//create edges
//Coca Cola employes Chris
g.addE('employeed').from(g.V('c1')).to(g.V('p4')).property(id,'c1p4')
g.E('c1p4').property('fromDate','2008-01-10').next()
g.E('c1p4').property('toDate','2016-01-10').next()
//create edges
//Coca Cola employes Emma
g.addE('employeed').from(g.V('c1')).to(g.V('p5')).property(id,'c1p5')
g.E('c1p5').property('fromDate','2016-01-01').next()

我想知道在哪里可以运行以下查询吗?

  1. 我们需要知道我们可以向谁寻求爱丽丝的参考。
  2. 推荐只能由在同一家公司与 Alice 合作过一段时间的人提供。
  3. 他们在一起的最后一份工作应该发生在从今天开始的最长时间之前
  4. 重叠的时间量是查询中的输入。
  5. 从今天开始的时间量是查询的输入。

结果应该是

重叠时间量 前的时间量(从今天开始(结果3年重叠---2年一起工作 -- 鲍勃 – 员工 - 可口可乐

我唯一能想到的是以下与要求相去甚远的查询。

//People who worked between dates with Alice
g.V().hasLabel('person').
       repeat(bothE().has('fromDate',between('2009-01-01','2020-12-31')).otherV().simplePath()).
       until(has('name','Alice')).
       path().
       by('name').
       by(label)
  [1]: https://i.stack.imgur.com/hbKUV.jpg

使用日期作为整数,您可以执行以下操作:

today = LocalDate.now()
start = today.minusYears(3)
minOverlap = 365 * 2 // 2 years
g.V().has('person','name','Alice').
  inE('employeed').as('e1').outV().
  outE('employeed').where(neq('e1')).as('e2').inV().
  project('colleague','start','end').
    by('name').
    by(union(select('e1','e2').select(values).unfold().values('fromDate'),
             constant(start.toEpochDay())).max()).
    by(union(coalesce(select('e1').values('toDate'), constant(today.toEpochDay())),
             coalesce(select('e2').values('toDate'), constant(today.toEpochDay()))).min()).
  filter(math('end-start').is(gte(minOverlap))).
  select('colleague')

这将返回BobEmma;他们都与Alice一起工作了大约3年。

下面是使用纪元天数的数据引入的改进版本。用于加载数据的脚本,但似乎不起作用。

        //Use case for datetimes working at the same company
    //only for tinkerpop
    graph=TinkerGraph.open()
    g=graph.traversal()
    //from here on common between neptune and tinkerpop//Setup started
    //create vertexes//create a company Coca Cola
    g.addV('company').property(id,'c1')
    g.V('c1').property('name','Coca Cola')
    g.V('c1').property('questId','10')//create a person Alice
    g.addV('person').property(id,'p1')
    g.V('p1').property('questId','1')
    g.V('p1').property('name','Alice')
    //create a person Bob
    g.addV('person').property(id,'p2')
    g.V('p2').property('questId','2')
    g.V('p2').property('name','Bob')
    //create a person Chris
    g.addV('person').property(id,'p3')
    g.V('p3').property('questId','3')
    g.V('p3').property('name','Chris')
    //create a person David
    g.addV('person').property(id,'p4')
    g.V('p4').property('questId','4')
    g.V('p4').property('name','David')//create a person Emma
    g.addV('person').property(id,'p5')
    g.V('p5').property('questId','5')
    g.V('p5').property('name','Emma')//create edges
    //Coca Cola employes Alice
    g.addE('employeed').from(g.V('c1')).to(g.V('p1')).property(id,'c1p1')
    g.E('c1p1').property('fromDate',14253).next()
    g.E('c1p1').property('toDate',17877).next()//create edges
    //Coca Cola employes Bob
    g.addE('employeed').from(g.V('c1')).to(g.V('p2')).property(id,'c1p2')
    g.E('c1p2').property('fromDate',16751).next()//create edges
    //Coca Cola employes Chris
    g.addE('employeed').from(g.V('c1')).to(g.V('p3')).property(id,'c1p3')
    g.E('c1p3').property('fromDate',17175).next()
    g.E('c1p3').property('toDate',17905).next()//create edges
    //Coca Cola employes Chris
    g.addE('employeed').from(g.V('c1')).to(g.V('p4')).property(id,'c1p4')
    g.E('c1p4').property('fromDate',13887).next()
    g.E('c1p4').property('toDate',16809).next()//create edges
    //Coca Cola employes Emma
    g.addE('employeed').from(g.V('c1')).to(g.V('p5')).property(id,'c1p5')
    g.E('c1p5').property('fromDate',16809).next()

这里的查询是让人们在过去两年中工作 1 年的重叠。我应该只得到鲍勃。

//Neptune
g.V().has('person','name','Alice').
  inE('employeed').as('e1').outV().
  outE('employeed').where(neq('e1')).as('e2').inV().
  project('colleague','start','end').
    by('name').
    by(union(select('e1','e2').select(values).unfold().values('fromDate'),
             constant(16877)).max()).
    by(union(coalesce(select('e1').values('toDate'), constant(17972)),
             coalesce(select('e2').values('toDate'), constant(17972))).min()).
  filter(math('end-start').is(gte(365))).
  select('colleague')

这里是实现我的用例的最终查询。

第一个用例。重叠时间 3 年
时间量 (从今天开始( 2 年结果 鲍勃 – 员工 - 可口可乐

查询

g.V().has('person','name','Alice').
  inE('employeed').as('e1').outV().
  outE('employeed').
  where(neq('e1')).as('e2').
  and(values('fromDate').is(lt(17948))).
  and(values('toDate').is(gt(17215))).
  inV().
  project('colleague','start','end').
        by('name').
        by(union(select('e2').values('fromDate'),select('e1').values('fromDate')).max()).
        by(union(select('e2').values('toDate'),select('e1').values('toDate')).min()).
  filter(math('end-start').is(gte(1095))).
  select('colleague','start','end')

结果

==>[同事:鲍勃,开始:16751,结束:17877]

最新更新