首页 > 解决方案 > 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. 我们需要知道我们可以向谁索要 Alice 的参考资料。
  2. 只有与 Alice 在同一家公司工作了一定时间的人才能提供参考。
  3. 他们的上一份工作应该发生在从今天开始的最长时间之前
  4. 重叠时间量是查询中的输入。
  5. 从今天开始的时间量是查询的输入。

结果应该是

重叠的时间 之前的时间(从今天开始) 结果 3 年重叠 --- 2 年一起工作 -- Bob – Employeed - Coca Cola

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

//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

标签: gremlintinkerpop

解决方案


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

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年。


推荐阅读