当找不到值时,Gremlin 查询不显示常量错误消息

Gremlin query does not show constant error message when values not found

我有一个 Gremlin 查询,如果找到传递的参数值,我几乎需要在每个步骤中检查,如果是,则继续下一步,否则退出查询并显示错误消息,该错误消息在每个步骤中都不同.

我的示例数据可以在这里找到https://gremlify.com/wczryzotbf/1

员工 ID、经理姓名和职称可以作为参数传递。我的查询尚未采用我尚未编写的指定参数。

如果传递了无效的员工 ID(如 100),以下查询不会 return 任何错误消息。如果我在这种情况下传递像 1 这样的有效员工 ID,它会按预期工作。

但是我传递了正确的员工 ID 但错误的经理姓名,错误消息 找不到经理 return 按预期编辑。

g.V().
  has('emp', 'emp_id', 100).as('empview').
  choose(
    select('empview').count(local).is(eq(0)),
    constant("Employee Id not found"),
    choose(
      outE().
      hasLabel('emp-mgr').
      has('start_date', gte(1643702400)).
      inV().
      has('manager', 'manager_name', 'manager2'),
      __.outE().
      hasLabel('emp-mgr').as('edgeview').
      has('start_date', gte(1643702400)).
      inV().
      has('manager', 'manager_name', 'manager2').as('managerview').
      select('empview', 'edgeview', 'managerview').
        by(
          valueMap(
            'emp_id',
            'emp_name',
            'start_date',
            'end_date',
            'manager_name')),
      constant("Manager not found")))

现在,如果我将查询更改为在选择步骤的第二行中写入 has 子句,它会显示无效 ID 的错误消息,但会针对每个员工顶点执行查询。

  g.V()
  choose(not(has('emp','emp_id', 1)),
    constant("Employee Id not found"),......

如果找不到员工 ID,如何显示错误消息?

此外,一旦我从员工顶点遍历到经理顶点,我需要从经理顶点收集数据,然后我需要返回到员工顶点,使用“emp-desig”将员工遍历到指定顶点" 边缘并在那里执行相同的验证步骤。我怎样才能回到一个节点并从那里遍历不同的路径?

我正在使用 AWS Neptune 数据库。我的实际查询将用 gremlin python.

编写

您看到的行为是正常的。出于演示目的考虑对遍历的第一部分进行一些修改(即我添加了第二个 constant() 以指示 else 条件):

g.V().
  has('emp', 'emp_id', 100).as('empview').
  choose(
    select('empview').count(local).is(eq(0)),
    constant("Employee Id not found"),
    constant("Found!!"))

如果你运行你会发现if和else条件都没有执行:

gremlin> g.V().has('emp', 'emp_id', 100).as('empview').
......1>   choose(select('empview').count(local).is(eq(0)),
......2>          constant("Employee Id not found"),
......3>          constant("Found!!"))
gremlin> 

原因是如果初始 has() 过滤器找不到任何东西,则没有遍历器触发 choose() 的执行。剩下的遍历什么都不做。

而不是 choose() 我认为你应该使用 coalesce():

g.V().has('emp', 'emp_id', 3).fold().
  coalesce(unfold().as('empview').
           coalesce(outE('emp-mgr').as('edgeview').
                    has('start_date', gte(2483257600)).
                    inV().
                    has('manager', 'manager_name', 'manager2').as('managerview').
                    select('empview','edgeview','managerview').
                      by(valueMap('emp_id','emp_name','start_date','end_date','manager_name')),
                    constant('Manager not found')),
           constant("Employee Id not found"))