ArangoDB 如何使用“公共邻居”样式的查询展开保存在边中的数据?

iibxawm4  于 2022-12-09  发布在  Go
关注(0)|答案(1)|浏览(98)

我有一个简单的模型,其中包含一个A文档集合

[{ _key: 'doc1', id: 'a/doc1', name: 'Doc 1' }, { _key: 'doc2', id: 'a/doc2', name: 'Doc 2' }]

以及单个B Edge集合,该集合将文档A与每个边上保留的weight整数结合起来。

[{ _key: 'xxx', id: 'b/xxx', _from: 'a/doc1', _to: 'a/doc2', weight: 256 }]

我尝试做一个“公共邻居”样式的查询,它将2个文档作为输入,并生成这些输入的公共邻居,沿着(每一边的)各自的权重。
例如用doc1doc26输入,下面是要达到的目标:

[
  { _key: 'doc6', weightWithDoc1: 43, weightWithDoc26: 57 },
  { _key: 'doc12', weightWithDoc1: 98, weightWithDoc26: 173 },
  { _key: 'doc21', weightWithDoc1: 3, weightWithDoc26: 98 },
]

我成功地从一个单一的目标开始:

FOR associated, association
  IN 1..1
  ANY ${d1}
  ${EdgeCollection}
  SORT association.weight DESC
  LIMIT 20
  RETURN { _key: associated._key, weight: association.weight }

然后成功地继续了文档的INTERSECTION逻辑

FOR proj IN INTERSECTION(
  (FOR associated, association
    IN 1..1
    ANY ${d1}
    ${EdgeCollection}
    RETURN { _key: associated._key }),
  (FOR associated, association
    IN 1..1
    ANY ${d2}
    ${EdgeCollection}
    RETURN { _key: associated._key })
)
LIMIT 20
RETURN proj

但是我现在很难提取每一边的weight,因为在内部的RETURN子句上展开它会使它们在交集上互斥;因此不返回任何内容。
问题:
1.有没有办法做出某种“选择性的INTERSECTION“,在过程中对一些字段进行分组?
1.是否有INTERSECTION的替代方案来实现我的目标?
附加问题:
1.理想情况下,在成功提取weightWithDoc1weightWithDoc26后,我希望按weightWithDoc1 + weightWithDoc26提取SORT DESC

8hhllhi2

8hhllhi21#

我自己找到了一个可以接受的答案

FOR associated IN INTERSECTION(
  (FOR associated
    IN 1..1
    ANY ${doc1}
    ${EdgeCollection}
    RETURN { _key: associated._key }),
  (FOR associated
    IN 1..1
    ANY ${doc2}
    ${EdgeCollection}
    RETURN { _key: associated._key })
)
LET association1 = FIRST(FOR association IN ${EdgeCollection}
  FILTER association._from == CONCAT(${DocCollection.name},'/',MIN([${doc1._key},associated._key])) AND association._to == CONCAT(${DocCollection.name},'/',MAX([${doc1._key},associated._key]))
  RETURN association)
LET association2 = FIRST(FOR association IN ${EdgeCollection}
  FILTER association._from == CONCAT(${DocCollection.name},'/',MIN([${doc2._key},associated._key])) AND association._to == CONCAT(${DocCollection.name},'/',MAX([${doc2._key},associated._key]))
  RETURN association)
SORT (association1.weight+association2.weight) DESC
LIMIT 20
RETURN { _key: associated._key, weight1: association1.weight, weight2: association2.weight }

我认为相交后重新选择并不理想,也不是最佳的解决方案,所以我暂时不做决定,等待更好的答案。

相关问题