【问题标题】:Using Predicate in gremlin javascript在 gremlin javascript 中使用谓词
【发布时间】:2018-06-10 10:13:12
【问题描述】:

我想使用 gremlin-javascript 来遍历远程图并获取一个顶点列表,其 id 在预定义的 id 列表中。

const gremlin = require('gremlin');
const Graph = gremlin.structure.Graph;
const GraphPredicate = gremlin.process.P;
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;

const graph = new Graph();
const g = graph.traversal().withRemote(new DriverRemoteConnection('ws://localhost:8182/gremlin'));

g.V()
    .has('id', GraphPredicate.within([414, 99999]))
    .toList()
    .then(function (result) {
        console.log(result);
    });

以上是我尝试过的代码,但它给了我一个空的顶点列表,而我希望结果中有一个 vertex(414)。

此外,当我使用下面的语句使用 gremlin-console 进行测试时,它在结果中给了我 vertex(414)。

:> g.V().hasId(within(414,99999))

所以我在这里有一些问题:

  • 为了使用 Predicate,我是否遗漏了配置中的某些内容?
  • 在javascript GraphPredicate.within([414, 99999]))的方法中是应该只是一个元素数组的参数> 或 用逗号分隔的元素列表 与 gremlin-console 的情况一样?顺便说一下,两种方法我都试过了,但我总是得到一个空的结果。

提前谢谢你,

【问题讨论】:

    标签: node.js gremlin tinkerpop tinkerpop3 gremlin-server


    【解决方案1】:

    我不能评论,但要使用id,你需要使用gremlin.process.t.id

    例子:

    在格雷姆林.by(id)
    在JS中.by(gremlin.process.t.id)

    希望对你有帮助

    【讨论】:

      【解决方案2】:

      id 是 TinkerPop 中的一个特殊属性,无法使用名称属性 "id" 检索。

      在您的情况下,按 id 检索的正确方法应该是:

      g.V().hasId(P.within(414, 99999)).toList()
        .then(result => console.log(result));
      

      此外,这可以通过删除 within() 调用来简化:

      g.V().hasId(414, 99999).toList()
        .then(result => console.log(result));
      

      【讨论】:

      • 是的,您的两种解决方案都有效。但是,当我应用它来过滤边缘的顶点时,它似乎不起作用。如果我在步骤outE('LIKE').inV() 或inE('LIKE').outV() 之后调用hasId(2, 3) 或hasId(P.within(2,3)),它总是会给我一个空的结果,不过,我确信结果中应该有顶点2、3。此外,当我只用一个 id hasId(2) 调用时,结果中会出现顶点 2
      • 我同意在查询中仅使用"id" 来引用id 是不可能的。但是,我只是碰巧知道我们可以使用它的 EnumValue 来引用它。可以从gremlin.process.t.id 访问该值。例如,g.V().has(gremlin.process.t.id, gremlin.process.P.within([2, 3])) 将给出预期的结果。
      • @CongBangDO - 在这种情况下(如您的第一条评论中所述),您可以使用as()/select()。它将允许您保存对您确实想要从遍历中返回的值的引用,并在稍后在终端步骤之前返回这些值。
      猜你喜欢
      • 2018-10-23
      • 1970-01-01
      • 2020-02-06
      • 1970-01-01
      • 2014-08-02
      • 1970-01-01
      • 1970-01-01
      • 2018-05-11
      • 1970-01-01
      相关资源
      最近更新 更多