Jagged Result Array Gremlin Query



请帮我写一个查询,返回遍历中的每个源顶点及其关联的边和顶点,作为每个源顶点上的数组?简而言之,我需要一个结果集,它包括一个3元组的数组,每个元组的项1是源顶点,项2和3是相关联的数组。

谢谢!

第1版:对图形数据进行了扩展,并添加了我当前的问题查询。第2版:改进的Gremlin示例图代码(抱歉,没想到会有人真正运行它。(

示例图形

g.addV("blueprint").property("name","Mall").
addV("blueprint").property("name","HousingComplex").
addV("blueprint").property("name","Airfield").
addV("architect").property("name","Tom").
addV("architect").property("name","Jerry").
addV("architect").property("name","Sylvester").
addV("buildingCategory").property("name","Civil").
addV("buildingCategory").property("name","Commercial").
addV("buildingCategory").property("name","Industrial").
addV("buildingCategory").property("name","Military").
addV("buildingCategory").property("name","Resnameential").
V().has("name","Tom").addE("designed").to(V().has("name","HousingComplex")).
V().has("name","Tom").addE("assisted").to(V().has("name","Mall")).
V().has("name","Jerry").addE("designed").to(V().has("name","Airfield")).
V().has("name","Jerry").addE("assisted").to(V().has("name","HousingComplex")).
V().has("name","Sylvester").addE("designed").to(V().has("name","Mall")).
V().has("name","Sylvester").addE("assisted").to(V().has("name","Airfield")).
V().has("name","Sylvester").addE("assisted").to(V().has("name","HousingComplex")).
V().has("name","Mall").addE("classification").to(V().has("name","Commercial")).
V().has("name","HousingComplex").addE("classification").to(V().has("name","Resnameential")).
V().has("name","Airfield").addE("classification").to(V().has("name","Civil"))

请注意,以上是我们数据的简化呈现。

需要的查询结果

我需要将每个蓝图顶点作为基础,并将其关联的每个边/顶点作为数组。

我当前的解决方案

目前,我做这个非常麻烦的查询,获取蓝图并分配一个标签,获取架构师并分配标签,然后选择两个标签。解决方案是可以的;然而,当我需要包括边或需要获得蓝图分类顶点(工业、军事、住宅、商业等(时,它会变得一团糟。实际上,我需要为每个蓝图提取的相关数据越多,我的解决方案就越麻烦。

我当前的查询如下:

g.V().hasLabel("blueprint").as("blueprints").
outE().or(hasLabel("designed"),hasLabel("assisted")).inV().as("architects").
select("blueprints").coalesce(out("classification"),constant()).as("classifications").
select("blueprints","architects","classifications")

以上内容产生了大量重复。如果:blueprint的数量是b,architects的数量是a,classifications的数量是c,则结果集包括b*a*c个结果。我想要一个蓝图,它有一个相关的架构师数组和一个相关分类数组(如果有的话(。

并发症

我试图在一个查询中完成这项工作,这样我就可以从图中获得所有蓝图数据,以填充过滤列表。一旦我有了包含所有顶点、边及其属性的列表,用户就可以单击指向Blob的链接、浏览到项目网站等。因此,我需要考虑分页和过滤,我更喜欢每次获得新页面或过滤器更改时都去服务器一次。

我找到了一个答案;然而,它使查询的计算费用增加了四倍。不确定是否可以进一步优化。

g.V().hasLabel("blueprint").
project("blueprints","architects").
by().
by(outE().or(hasLabel("designed"),hasLabel("assisted")).inV().dedup().fold())

我刚刚为蓝图和架构师解决了问题,但分类只需要另一个by(…遍历…(和投影标签。

我可能只需要在一个查询中获取蓝图,在并行查询中获取它们的每个关联项,然后将其全部放在API中。对于API数据层来说,这将是非常糟糕的设计,但出于性能原因,这可能是必要的。

最新更新