使用 tinkerpop 3 在同一查询中输出垂直和相邻顶点

Output vertice and adjacent vertices in same query with tinkerpop3

我是 gremlin 的新手,正在尝试了解如何使用带有 GraphSON 的 Azure Cosmos DB 在同一结果中获取文章以及作者和附件。

我的图表如下所示:

[User] <- (edge: author) - [Article] - (edge: attachments) -> [File1, File2]

我想在 UI 中获取我需要的所有内容,以根据要求显示一篇文章以及作者和有关附件的信息。

我要获取的内容与此伪代码类似:

{
article: {...},
author: [{author1}],
attachment: [{file1}, {file2}]
}

我目前的尝试:

g.V().hasLabel('article').as('article').out('author', 'attachments').as('author','attachments').select('article', 'author', 'attachments')

如何编写查询来获取不同的值?

在询问有关 Gremlin 的问题时,以如下形式提供一些示例数据总是有帮助的:

g.addV('user').property('name','jim').as('jim').
  addV('user').property('name','alice').as('alice').
  addV('user').property('name','bill').as('bill').
  addV('article').property('title','Gremlin for Beginners').as('article').
  addV('file').property('file','/files/a.png').as('a').
  addV('file').property('file','/files/b.png').as('b').
  addE('authoredBy').from('article').to('jim').
  addE('authoredBy').from('article').to('alice').
  addE('authoredBy').from('article').to('bill').
  addE('attaches').from('article').to('a').
  addE('attaches').from('article').to('b').iterate()

请注意,我将您的边缘标签名称修改为更像动词,以便它们更好地将自己与类似名词的顶点标签区分开来。它倾向于沿着边缘的方向很好地阅读,如:article --authoredBy-> user

无论如何,您的问题最容易解决 project() step:

gremlin> g.V().has('article','title','Gremlin for Beginners').
......1>   project('article','authors','attachments').
......2>     by().
......3>     by(out('authoredBy').fold()).
......4>     by(out('attaches').fold())
==>[article:v[6],authors:[v[0],v[2],v[4]],attachments:[v[10],v[8]]]

在上面的代码中,请注意在 by() 步骤中使用了 fold() - 这将强制执行内部遍历的完整迭代并将其放入列表中。如果您错过了这一步,您将只会得到一个结果(即第一个)。

更进一步,我添加了 valueMap() 和下一个结果,以便您可以更好地查看上面顶点中包含的属性。

gremlin> g.V().has('article','title','Gremlin for Beginners').
......1>   project('article','authors','attachments').
......2>     by(valueMap()).
......3>     by(out('authoredBy').valueMap().fold()).
......4>     by(out('attaches').valueMap().fold()).next()
==>article={title=[Gremlin for Beginners]}
==>authors=[{name=[jim]}, {name=[alice]}, {name=[bill]}]
==>attachments=[{file=[/files/b.png]}, {file=[/files/a.png]}]