simplePath()中断查询

时间:2018-10-26 20:40:21

标签: gremlin tinkerpop3

<head> <meta charset="utf-8" /> <script data-require="jquery" data-semver="2.1.4" src="http://code.jquery.com/jquery-2.1.4.min.js"></script> <script> const allFields = [''] //put all your field ids here $("#tableAdd").click(function () { // Set all your fields back to optional allFields.forEach(function(field) { $( "#" + field ).removeAttr('required'); }); // Set specific fields as required, 'manufacturer_date' being the field 'table_add' requires var requiredFields = ['manufacturer_date']; requiredFields.forEach(function(field) { $( "#" + field ).attr('required', ''); }); // Then trigger the hidden input field to submit the form and send the POST request $( "#table_add" ).trigger( "click" ); }); </script> </head> <body> <input id="manufacturer_date" type="date"> <input type="button" id="tableAdd" value="Add"> <input type="submit" name="table_add" id="table_add" hidden> </body>内添加simplePath()时,我的查询不再返回结果。

该查询尝试查找某种涉及三个特定人员的事件(例如“图形数据库会议”)。

  • “驴友”参加了举办活动的学校。
  • “鲍勃”是活动中的热狗摊贩。
  • “ marko”为事件提供了安全性。

我正在使用match()查找这三个人的融合之处。如果有更好的方法,请提出建议。谢谢!刚开始学习gremlin。

Ascii艺术:

match()

有效的查询

            alice --[enrolled-in]-> gremlin 101 --[offered-by]-> graph db school --[hosted]--------------
                                                                                                        |
                                                                                                        v
bob --[works-for]-> hot dogs r awesome --[subcontractor-of]-> best event planner --[planned]----> graph conference
                                                                                                        ^
                                                                                                        |
                                            marko --[works-for]-> super security --[secured]-------------

请注意,某些顶点不止一次出现(而且我们甚至还没有添加任何循环!)

当我将g.V().match( __.as('alice').hasLabel('person').has('name', 'alice').repeat(__.out()).until(__.hasLabel('event')).as('event'), __.as('event').repeat(__.in()).until(__.hasLabel('person').has('name', 'bob')).as('bob'), __.as('event').repeat(__.in()).until(__.hasLabel('person').has('name', 'marko')).as('marko')). path() ==>[v[0],v[0],v[2],v[5],v[21],v[21],v[13],v[10],v[8],v[21],v[18],v[16],[bob:v[8],alice:v[0],event:v[21],marko:v[16]]] 添加到.simplePath() any 中时,查询什么都不会返回。例如,在第一个repeat()

repeat()

gremlin控制台:

g.V().match(
  __.as('alice').hasLabel('person').has('name', 'alice').repeat(__.out().simplePath()).until(__.hasLabel('event')).as('event'),
  __.as('event').repeat(__.in()).until(__.hasLabel('person').has('name', 'bob')).as('bob'),
  __.as('event').repeat(__.in()).until(__.hasLabel('person').has('name', 'marko')).as('marko')).
  path()

1 个答案:

答案 0 :(得分:1)

match()simplePath()几乎永远无法很好地协同工作。如果match()产生一条简单的路径,那么match()确实是毫无意义的。要找到所有匹配的事件,您可以执行以下操作:

gremlin> g.V().has("person", "name", within("alice","bob","marko")).as("p").
......1>   repeat(out().simplePath()).
......2>     until(hasLabel("event")).
......3>   group().
......4>     by("name").
......5>     by(group().
......6>          by(select("p").by("name")).
......7>          by(path().by("name").fold())).unfold().
......8>   filter(select(values).count(local).is(3)).
......9>   select(keys)
==>graph conference

如果您也对每个人到事件的路径感兴趣:

gremlin> g.V().has("person", "name", within("alice","bob","marko")).as("p").
......1>   repeat(out().simplePath()).
......2>     until(hasLabel("event")).
......3>   group().
......4>     by("name").
......5>     by(group().
......6>          by(select("p").by("name")).
......7>          by(path().by("name").fold())).unfold().
......8>   filter(select(values).count(local).is(3)).
......9>   select(values).unfold().
.....10>   select(values)
==>[[bob,hot dogs r awesome,best event planner,graph conference]]
==>[[alice,gremlin 101,graph db school,graph conference]]
==>[[marko,super security,graph conference]]

请注意,每一行都是路径数组;这是因为-从理论上讲-每个人都可以通过多种方式与特定事件建立联系。如果您仅对人与事件之间的任何关联感兴趣,则可以从嵌套的fold()步骤中删除group()步骤。

gremlin> g.V().has("person", "name", within("alice","bob","marko")).as("p").
......1>   repeat(out().simplePath()).
......2>     until(hasLabel("event")).
......3>   group().
......4>     by("name").
......5>     by(group().
......6>          by(select("p").by("name")).
......7>          by(path().by("name"))).unfold().
......8>   filter(select(values).count(local).is(3)).
......9>   select(values).unfold().
.....10>   select(values)
==>[bob,hot dogs r awesome,best event planner,graph conference]
==>[alice,gremlin 101,graph db school,graph conference]
==>[marko,super security,graph conference]