查询以识别 Label "A" 的顶点,这些顶点具有到 Label "B" 的多个顶点的边
Query to identify vertices of Label "A" that have edges to multiple vertices of Label "B"
我有一个图,其中标签“organization”的顶点和边到标签“social_account”的顶点等。在这种一对多关系中,组织顶点在每个不同的社交平台上应该只有一个 social_account 顶点。也就是说,一个组织可以有到“Twitter”social_account 顶点、“Medium”social_account 顶点和“Instagram”social_account 顶点的边,但它不应该有到多个“Twitter”social_account 顶点的边,此处说明:
Toy data diagram
我想要一个标识的查询:
- 组织 具有多个 social_account 个给定社交平台顶点的顶点
- 应删除的关联 social_account 顶点的 ID,基于它们的 updated_at 值.
我有以下查询,其中 returns 组织 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 比较陌生,因此非常感谢有关有效过滤这些结果以达到所需输出的建议。
如果您提供构建图表的遍历,我们可以为您提供经过测试的答案,这会很有帮助。
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和他们需要删除的推特账号。
要获取需要删除的推特账号,在第二个by
调制器中嵌套遍历获取与组织关联的推特账号,根据“updated_at的值对它们进行排序"(降序)。然后使用range
步骤跳过不需要删除的第一个推特账号。
我有一个图,其中标签“organization”的顶点和边到标签“social_account”的顶点等。在这种一对多关系中,组织顶点在每个不同的社交平台上应该只有一个 social_account 顶点。也就是说,一个组织可以有到“Twitter”social_account 顶点、“Medium”social_account 顶点和“Instagram”social_account 顶点的边,但它不应该有到多个“Twitter”social_account 顶点的边,此处说明:
Toy data diagram
我想要一个标识的查询:
- 组织 具有多个 social_account 个给定社交平台顶点的顶点
- 应删除的关联 social_account 顶点的 ID,基于它们的 updated_at 值.
我有以下查询,其中 returns 组织 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 比较陌生,因此非常感谢有关有效过滤这些结果以达到所需输出的建议。
如果您提供构建图表的遍历,我们可以为您提供经过测试的答案,这会很有帮助。
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和他们需要删除的推特账号。
要获取需要删除的推特账号,在第二个by
调制器中嵌套遍历获取与组织关联的推特账号,根据“updated_at的值对它们进行排序"(降序)。然后使用range
步骤跳过不需要删除的第一个推特账号。