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

时间:2017-12-07 17:21:49

标签: graph-databases arangodb aql

我有一个words集合和一个word_relations边集合。单词与其他单词有关系,例如同义词 hasPart partOf hasType typeOf 等等。

我试图找出如何{A}图查询FILTER以便遵循所有路径,但是具有多个边的路径包含relation_type的相同边缘属性。

,例如,我想要这个结果(所有返回的路径,无论是一个还是多个,都包含一个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

我尝试了一些不同的过滤器,但都不起作用:

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

仅返回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

感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

以下内容也可用于较大的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