用于匹配边属性的 ArangoDB AQL FILTER 图

ArangoDB AQL FILTER graph for matching edge attributes

我有一个 words 合集和一个 word_relations 边缘合集。单词与其他单词有关系,例如 synonymhasPartpartOfhasType typeOf

我正在尝试弄清楚如何 FILTER AQL 图查询,以便遵循所有路径,但具有多个边的路径包含 relation_type.[=23 的相同边属性=]

例如,我想要这个结果(所有返回的路径,无论是一条还是多条,都包含一个 relation_type 属性值):

[
  {
    "edges": [
      {
        "_from": "library/parent",
        "_to": "library/organism",
        "relation_type": "typeOf"
      }
    ],
    "vertices": [
      {
        "_key": "parent",
        "word": "parent"
      },
      {
        "_key": "organism",
        "word": "organism"
      }
    ]
  },
  {
    "edges": [
      {
        "_from": "library/parent",
        "_to": "library/organism",
        "relation_type": "typeOf"
      },
      {
        "_from": "library/organism",
        "_to": "library/scheme",
        "relation_type": "typeOf"
      }
    ],
    "vertices": [
      {
        "_key": "parent",
        "word": "parent"
      },
      {
        "_key": "organism",
        "word": "organism"
      },
      {
        "_key": "scheme",
        "word": "scheme"
      }
    ]
  },
  {
    "edges": [
      {
        "_from": "library/parent",
        "_to": "library/stepparent",
        "relation_type": "hasType"
      },
      {
        "_from": "library/stepparent",
        "_to": "library/stepmother",
        "relation_type": "hasType"
      }
    ],
    "vertices": [
      {
        "_key": "parent",
        "word": "parent"
      },
      {
        "_key": "stepparent",
        "word": "stepparent"
      },
      {
        "_key": "stepmother",
        "word": "stepmother"
      }
    ]
  }
]

不是这个结果(relation_type 属性值在一个路径内改变):

[
  {
    "edges": [
      {
        "_from": "library/parent",
        "_to": "library/organism",
        "relation_type": "typeOf"
      }
    ],
    "vertices": [
      {
        "_key": "parent",
        "word": "parent"
      },
      {
        "_key": "organism",
        "word": "organism"
      }
    ]
  },
  {
    "edges": [
      {
        "_from": "library/parent",
        "_to": "library/organism",
        "relation_type": "typeOf"
      },
      {
        "_from": "library/organism",
        "_to": "library/zygote",
        "relation_type": "hasCategories"
      }
    ],
    "vertices": [
      {
        "_key": "parent",
        "word": "parent"
      },
      {
        "_key": "organism",
        "word": "organism"
      },
      {
        "_key": "zygote",
        "word": "zygote"
      }
    ]
  },
  {
    "edges": [
      {
        "_from": "library/parent",
        "_to": "library/family_unit",
        "relation_type": "memberOf"
      },
      {
        "_from": "library/family_unit",
        "_to": "library/sibling",
        "relation_type": "hasMembers"
      }
    ],
    "vertices": [
      {
        "_key": "parent",
        "word": "parent"
      },
      {
        "_key": "family_unit",
        "word": "family unit"
      },
      {
        "_key": "sibling",
        "word": "sibling"
      }
    ]
  },
]

relation_type 属性值有很多种。我不想过滤到特定值,我不关心初始值是什么,我只希望所有后续属性值都与第一个相同。

获得第二个(不希望的)结果的 AQL 如下所示:

FOR v, e, p IN 1..2 OUTBOUND 'words/parent' word_relations
  RETURN p

我尝试了几种不同的过滤器,none 其中有效:

0 个结果:

FOR v, e, p IN 1..2 OUTBOUND 'library/parent' library_relations
  FILTER p.edges[*].relation_type ALL == p.edges[*].relation_type
  RETURN p

只有 returns 2 条路径,b/c FILTER 需要第二条存在:

FOR v, e, p IN 1..2 OUTBOUND 'library/parent' library_relations
  FILTER p.edges[0].relation_type == p.edges[1].relation_type
  RETURN p

感谢您的帮助!

以下也可用于较大的 MAX 值:

FOR v, e, p IN 1..2 OUTBOUND 'library/parent' library_relations
  FILTER p.edges[*].relation_type ALL == e.relation_type
  RETURN p

不幸的是,AQL 优化器目前严重受损(请参阅 https://github.com/arangodb/arangodb/issues/3979#issuecomment-350686061),因此您很可能必须解决其限制,例如通过在上面加上:

FOR tmpV, tmpE  IN 1..1 OUTBOUND 'library/parent’ library_relations

然后使用 tmpE.relation_type 而不是 e.relation_type