如何使用不同的 APOC 为 neo4j 添加多个标签
How to add multiple labels using distinct using APOC for neo4j
我正在尝试使用密码查询向节点添加多个标签,我想添加 2 个标签:
- 标题
- 团队
两者都存在于节点道具上:
CREATE (p:Person {id: 1, name: bob, title:developer, team: team1});
以下正在运行并将标题添加为标签:
match (n:Person)
with distinct n.title as title, collect(distinct n) as persons
call apoc.create.addLabels(persons, [apoc.text.upperCamelCase(title)]) yield node
return *
而不是 运行再次使用团队属性进行相同的查询,我想使用具有多个地区值的相同查询,例如:
with distinct n.title as title and with distinct n.team as team collect(distinct n) as persons
哪个不起作用,知道这是否可行吗?或者我应该 运行 2 个不同的查询吗?
我不确定我是否理解不同值的要求,因为 apoc.create.addLabels
将忽略目标节点上已经存在的标签。
例如,给出如下测试数据:
MERGE (: Person { id: 1, name: 'Both Properties', title: 'some title', team: 'some team' })
MERGE (: Person { id: 2, name: 'Only title', title: 'some title' })
MERGE (: Person { id: 3, name: 'Only team', team: 'some team' })
MERGE (: Person { id: 4, name: 'Neither team nor title' })
MERGE (: Person { id: 5, name: 'Same team and title', title: 'admin', team: 'admin' })
下面将从这两个属性构建要创建的标签列表,然后设置它们:
MATCH (p: Person)
WITH p, [x in [p.title] + [p.team] WHERE x IS NOT NULL | apoc.text.upperCamelCase(x)] as desiredTitles
CALL apoc.create.addLabels(p, desiredTitles) YIELD node
RETURN p.name, labels(p)
╒════════════════════════╤═════════════════════════════════╕
│"p.name" │"labels(p)" │
╞════════════════════════╪═════════════════════════════════╡
│"Both Properties" │["Person","SomeTitle","SomeTeam"]│
├────────────────────────┼─────────────────────────────────┤
│"Only title" │["Person","SomeTitle"] │
├────────────────────────┼─────────────────────────────────┤
│"Only team" │["Person","SomeTeam"] │
├────────────────────────┼─────────────────────────────────┤
│"Neither team nor title"│["Person"] │
├────────────────────────┼─────────────────────────────────┤
│"Same team and title" │["Person","Admin"] │
└────────────────────────┴─────────────────────────────────┘
如果 属性 中的任何一个为 null,那么 WITH
语句中的列表推导式将删除它们,如果它们相同,则 APOC 会自动忽略重复项。
我正在尝试使用密码查询向节点添加多个标签,我想添加 2 个标签:
- 标题
- 团队
两者都存在于节点道具上:
CREATE (p:Person {id: 1, name: bob, title:developer, team: team1});
以下正在运行并将标题添加为标签:
match (n:Person)
with distinct n.title as title, collect(distinct n) as persons
call apoc.create.addLabels(persons, [apoc.text.upperCamelCase(title)]) yield node
return *
而不是 运行再次使用团队属性进行相同的查询,我想使用具有多个地区值的相同查询,例如:
with distinct n.title as title and with distinct n.team as team collect(distinct n) as persons
哪个不起作用,知道这是否可行吗?或者我应该 运行 2 个不同的查询吗?
我不确定我是否理解不同值的要求,因为 apoc.create.addLabels
将忽略目标节点上已经存在的标签。
例如,给出如下测试数据:
MERGE (: Person { id: 1, name: 'Both Properties', title: 'some title', team: 'some team' })
MERGE (: Person { id: 2, name: 'Only title', title: 'some title' })
MERGE (: Person { id: 3, name: 'Only team', team: 'some team' })
MERGE (: Person { id: 4, name: 'Neither team nor title' })
MERGE (: Person { id: 5, name: 'Same team and title', title: 'admin', team: 'admin' })
下面将从这两个属性构建要创建的标签列表,然后设置它们:
MATCH (p: Person)
WITH p, [x in [p.title] + [p.team] WHERE x IS NOT NULL | apoc.text.upperCamelCase(x)] as desiredTitles
CALL apoc.create.addLabels(p, desiredTitles) YIELD node
RETURN p.name, labels(p)
╒════════════════════════╤═════════════════════════════════╕
│"p.name" │"labels(p)" │
╞════════════════════════╪═════════════════════════════════╡
│"Both Properties" │["Person","SomeTitle","SomeTeam"]│
├────────────────────────┼─────────────────────────────────┤
│"Only title" │["Person","SomeTitle"] │
├────────────────────────┼─────────────────────────────────┤
│"Only team" │["Person","SomeTeam"] │
├────────────────────────┼─────────────────────────────────┤
│"Neither team nor title"│["Person"] │
├────────────────────────┼─────────────────────────────────┤
│"Same team and title" │["Person","Admin"] │
└────────────────────────┴─────────────────────────────────┘
如果 属性 中的任何一个为 null,那么 WITH
语句中的列表推导式将删除它们,如果它们相同,则 APOC 会自动忽略重复项。