【问题标题】:Query to identify vertices of Label "A" that have edges to multiple vertices of Label "B"查询以识别标签“A”的顶点与标签“B”的多个顶点有边
【发布时间】:2021-05-25 22:42:08
【问题描述】:

我有一个带有标签“组织”顶点的图,其中边缘到标签“social_account”的顶点。在这种一对多的关系中,一个组织顶点在每个不同的社交平台上应该只有一个 social_account 顶点。也就是说,一个组织可以有一个“Twitter”social_account 顶点、一个“Medium”social_account 顶点和一个“Instagram”social_account 顶点的边,但它不应该有多个“Twitter”social_account 顶点的边,如图所示这里:

Toy data diagram

我想要一个标识:

  1. organization 具有多个 social_account 顶点的给定社交平台的顶点
  2. 应删除的关联 social_account 顶点的 ID,基于它们的 updated_at 值。

我有以下查询,它返回组织 ID 和组织顶点具有边缘的 social_account 顶点的关联 ID。

g.V().hasLabel("organization").as("org", "social_ids").select("org", "social_ids").by(id).by(out().hasLabel('social_account').id().fold())

给定一个输入“Twitter”,我希望看到与上述玩具数据类似的内容:

[org: Org-2, to_drop: [Twitter-39]]

以及对应于social_account“社交”属性的一般输入的类似输出。

我对 Gremlin 比较陌生,因此非常感谢有关有效地将这些结果过滤为所需输出的建议。

【问题讨论】:

    标签: gremlin


    【解决方案1】:

    当您提供构建图的遍历时会很有帮助,因此我们可以为您提供经过测试的答案。

    g.addV('Organization').property(id, 'Org-1').as('org1').
      addV('social_account').property(id, 'Twitter-01').property('social', 'Twitter').property('updated_at', 1234).as('twitter01').
      addE('has_social_account').from('org1').to('twitter01').
      addV('social_account').property(id, 'Medium-34').property('social', 'Medium').property('updated_at', 1345).as('medium34').
      addE('has_social_account').from('org1').to('medium34').
      addV('social_account').property(id, 'Insta-55').property('social', 'Instagram').property('updated_at', 4567).as('insta55').
      addE('has_social_account').from('org1').to('insta55').
      addV('Organization').property(id, 'Org-2').as('org2').
      addV('social_account').property(id, 'Twitter-39').property('social', 'Twitter').property('updated_at', 1111).as('twitter39').
      addE('has_social_account').from('org2').to('twitter39').
      addV('social_account').property(id, 'Twitter-60').property('social', 'Twitter').property('updated_at', 2345).as('twitter60').
      addE('has_social_account').from('org2').to('twitter60').
      addV('social_account').property(id, 'Insta-19').property('social', 'Instagram').property('updated_at', 4567).as('insta19').
      addE('has_social_account').from('org2').to('insta19')
    

    这里是遍历拥有多个 Twitter 帐户的组织以及需要删除的 Twitter 帐户的 ID。

    g.V().hasLabel('Organization').where(out('has_social_account').has('social', 'Twitter').count().is(gt(1))).
      project('org', 'to_drop').
        by(id).
        by(out('has_social_account').has('social', 'Twitter').
          order().by('updated_at', desc).
          range(1, -1).id().fold())
    

    where 步骤按拥有多个 Twitter 帐户的组织过滤。而project 步骤获取这些组织的 ID 及其需要删除的 Twitter 帐户。

    为了获取需要删除的 Twitter 帐户,第二个 by 调制器中的嵌套遍历获取与组织关联的 Twitter 帐户,并根据“updated_at”的值(降序)对它们进行排序。然后使用range步骤跳过第一个不需要删除的推特账号。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-18
      • 1970-01-01
      • 2017-10-24
      • 2017-10-19
      • 2014-02-18
      • 1970-01-01
      • 2017-06-15
      相关资源
      最近更新 更多