Gremlin:有没有办法根据字符串的索引找到字符?

Gremlin: Is there a way to find the character based on the index of a string?

我在 OrientDB 上有顶点 "office" 和 属性 "name"。我想按名称查找名称中没有“-”作为字符串的第三个字符的办公室。我想这将需要 gremlin query.This 中的一些 java 代码是我最好的尝试,但它会导致办公室名称实际上有一个“-”作为他们的第三个字符。

g.V().hasLabel('office') .where(values('name').map{it.get().charAt(2)}.is(neq('-'))) .project('Office Name') .by(values('name'))

由于 Gremlin 不支持字符串操作(如 splitcharAt 等),您唯一的机会就是 lambda。好像你已经想通了,但你的解决方案对我来说太复杂了。您可以使用更简单的东西,例如:

g.V().hasLabel('office').
  has('name', filter {it.get()[2] != '-'}).
  project('Office Name').
    by('name')

但是请注意,如果办公室名称少于 3 个字符,此过滤器将抛出异常。因此,您最好检查 String 是否足够长:

g.V().hasLabel('office').
  has('name', filter {it.get().length() > 2 && it.get()[2] != '-'}).
  project('Office Name').
    by('name')

...或使用 RegEx 模式匹配(这在 Groovy 中非常好用且简单):

g.V().hasLabel('office').
  has('name', filter {it.get() ==~ /.{2}-.*/}).
  project('Office Name').
    by('name')

虽然您的遍历不起作用的主要原因是 charAt returns 然后将 CharacterString -,因此每个办公室名称都将通过 neq 过滤器。