我有一个存储不同类型实体的图形数据库,并且正在构建一个API以从图形中获取实体。但是,这有点复杂,因为对于每种类型的实体,都有一组规则用于获取相关实体以及原始实体。为此,我使用了聚合步骤将要提取的所有相关实体聚合到集合中。
另一个要求是获取一批实体(和相关实体)。我将通过更改has
获取实体以使用该步骤P.within
并将聚合映射到每个找到的实体的步骤来执行此操作。如果我继续获取单个实体,则此方法有效,但是如果我要获取两个实体,则我的结果集对于第一个实体将是正确的,但是第二个的结果集将包含第一个实体的结果及其自身的结果。我认为这是因为第二个集合将简单地从第一个集合添加到集合集合中,因为聚合键是相同的。我还没有找到清除第一个和第二个之间的集合的任何方法,也没有找到具有动态聚合副作用键的任何方法。
码:
return graph.traversal().V()
.hasLabel(ENTITY_LABEL)
.has("entity_ref", P.within(entityRefs)) // entityRefs is a list of entities I am looking for
.flatMap(
__.aggregate("match")
.sideEffect(
// The logic that applies the rules lives here. It will add items to "match" collection.
)
.select("match")
.fold()
)
.toStream()
...
结果应为实体列表的列表,其中外部列表中的第一个实体列表包含中的第一个实体的entityRefs
结果,第二个实体列表包含中的第二个实体的结果entityRefs
。
例如:我要为实体裁判取顶点A
和B
及其相关实体。假设我期望结果为[[A, C], [B, D, E]]
,但得到结果[[A, C], [A, C, B, D, E]]
(第二个结果包含第一个结果)。
问题:
entityRef
?编辑:这是一个示例,是该问题的缩影。该图的设置如下:
g.addV('entity').property('id',1).property('type', 'customer').as('1').
addV('entity').property('id',2).property('type', 'email').as('2').
addV('entity').property('id',6).property('type', 'customer').as('6').
addV('entity').property('id',3).property('type', 'product').as('3').
addV('entity').property('id',4).property('type', 'subLocation').as('4').
addV('entity').property('id',7).property('type', 'location').as('7').
addV('entity').property('id',5).property('type', 'productGroup').as('5').
addE('AKA').from('1').to('2').
addE('AKA').from('2').to('6').
addE('HOSTED_AT').from('3').to('4').
addE('LOCATED_AT').from('4').to('7').
addE('PART_OF').from('3').to('5').iterate()
我想获取一批实体,给定它们的ID并获取相关实体。应该返回哪些相关实体是原始实体类型的函数。我当前的查询是这样的(在此示例中稍作修改):
g.V().
hasLabel('entity').
has('id', P.within(1,3)).
flatMap(
aggregate('match').
sideEffect(
choose(values('type')).
option('customer',
both('AKA').
has('type', P.within('email', 'phone')).
sideEffect(
has('type', 'email').
aggregate('match')).
both('AKA').
has('type', 'customer').
aggregate('match')).
option('product',
bothE('HOSTED_AT', 'PART_OF').
choose(label()).
option('PART_OF',
bothV().
has('type', P.eq('productGroup')).
aggregate('match')).
option('HOSTED_AT',
bothV().
has('type', P.eq('subLocation')).
aggregate('match').
both('LOCATED_AT').
has('type', P.eq('location')).
aggregate('match')))
).
select('match').
unfold().
dedup().
values('id').
fold()
).
toList()
如果我仅获取一个实体,我将获得正确的结果。因为id: 1
我得到[1,2,6]
和id: 3
我得到[3,5,4,7]
。但是,当我两者都获取时,我得到:
==>[3,5,4,7]
==>[3,5,4,7,1,2,6]
第一个结果正确,但第二个结果包含两个ID的结果。
您可以利用(记录得不好,说实话,但遍历步骤看似强大)group().by(key).by(value)
。
这样一来,您就可以删除aggregate()
引起麻烦的副作用步骤。作为一种选择,可以将与某些遍历匹配的多个顶点收集到我使用的列表中union()
。
使用您发布的图表的示例(为简便起见,我仅包括“客户”选项):
g.V().
hasLabel('entity').
has('id', P.within(1,3)).
<String, List<Entity>>group()
.by("id")
.by(choose(values("type"))
.option('customer', union(
identity(),
both('AKA').has('type', 'email'),
both('AKA').has('type', within('email', 'phone')).both('AKA').has('type', 'customer'))
.map((traversal) -> new Entity(traversal.get())) //Or whatever business class you have
.fold() //This is important to collect all 3 paths in the union together
.option('product', union()))
.next()
这种遍历具有明显的缺点,即代码更加冗长。它声明它将两次从客户那里跳过“ AKA”。您的遍历只声明了一次。
但是,它确实使步骤的by(value)
一部分group()
在不同的键之间是分开的。这就是我们想要的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句