我以{company:[c1,c2,c3],指定:[d1,d2,d3]}的格式获得了100000个员工数据,其中c1是员工的第一家公司,c2是第二家公司,c3是第三家公司... 这同样适用于员工在这些公司的指定。 这里的交换意味着员工从c1-> c2或d1-> d2切换。
我希望在neo4j中对此进行建模以回答以下问题:
我尝试了这段代码,但即使我已编入索引,它也会一直运行。
CREATE CONSTRAINT ON (u:User) ASSERT u.id IS UNIQUE;
CREATE CONSTRAINT ON (n:Company) ASSERT n.name IS UNIQUE;
CREATE CONSTRAINT ON (d:Designation) ASSERT d.name IS UNIQUE;
CREATE INDEX ON :Company(type),
USING PERIODIC COMMIT 1000
LOAD CSV WITH HEADERS FROM "file:/positions.csv" AS row
WITH row
// user
MERGE (u:User {id: row.user})
ON CREATE SET u.id = row.user
//comp
MERGE (n:Company {name:row.name})
ON CREATE SET n.name = row.name, n.type = row.type
// --------------------------- Approach 2
CREATE (p:Position{user: row.user, duration: toFloat(row.duration), sequence:toInt(row.sequence)})
CREATE (p)-[:AT]->(n)
// optionally build Designation
FOREACH(c1 IN (CASE WHEN row.type = 'c' THEN [1] else [] END) |
// maybe check for empty
//title
MERGE (d:Designation {title:row.title})
ON CREATE SET d.title = row.title
CREATE (p)-[:AS]->(d)
)
WITH row
MATCH (u:User{id:row.user})
MATCH (p:Position{user: u.id, sequence:0})
CREATE (u)-[:NEXT]->(p)
WITH row, p
// todo - link sequence 0 position
MATCH (p:Position {user: row.user})
WITH p ORDER BY p.sequence
WITH COLLECT(p) AS positions
FOREACH(i in RANGE(0, length(positions)-2) |
FOREACH(pos1 in [positions[i]] |
FOREACH(pos2 in [positions[i+1]] |
CREATE UNIQUE (pos1)-[:NEXT]->(pos2))))
此处序列是用户/员工持有的职位序列
答案 0 :(得分:1)
以下是如何对数据建模以及用例查询的示例。
此样本数据模型使用"链"由Job
关系分隔的HISTORY
个节点,用于记录员工的工作历史记录。最近的工作直接附加到员工(也通过HISTORY
关系)。每个Job
节点与Company
节点有一个关系,另一个与Designation
节点有关系。
CREATE
(c1:Company {id: 123, name: 'Acme Corp.'}),
(c2:Company {id: 321, name: 'Bank of Peoria'}),
(c3:Company {id: 444, name: 'Consolidated Cans'}),
(d1:Designation {id: 234, title: 'Assistant Clerk'}),
(d2:Designation {id: 345, title: 'Clerk'}),
(d3:Designation {id: 456, title: 'Head Clerk'}),
(e1:Employee {id: 123456789, name: 'Fred Murphy'}),
(c1)<-[:AT]-(j1:Job {started: 123456789})-[:AS]->(d1),
(c2)<-[:AT]-(j2:Job {started: 234567890})-[:AS]->(d2),
(c3)<-[:AT]-(j3:Job {started: 345678901})-[:AS]->(d3),
(e1)-[:HISTORY]->(j3)-[:HISTORY]->(j2)-[:HISTORY]->(j1);
MATCH p=(n)<-[:HISTORY*]-(e1:Employee {id: 123456789})
WHERE NOT ()<-[:HISTORY]-(n)
WITH NODES(p)[0..-1] AS jobs
UNWIND jobs AS job
MATCH (job)-[:AT]->(c)
RETURN c, job.started;
MATCH p=(n)<-[:HISTORY*]-(e1:Employee {id: 123456789})
WHERE NOT ()<-[:HISTORY]-(n)
WITH NODES(p)[0..-1] AS jobs
UNWIND jobs AS job
MATCH (job)-[:AS]->(d)
RETURN d, job.started;
MATCH p=(n)<-[:HISTORY*]-(e1:Employee {id: 123456789})
WHERE NOT ()<-[:HISTORY]-(n)
WITH NODES(p)[0..-1] AS jobs
UNWIND jobs AS job
MATCH (c)<-[:AT]-(job)-[:AS]->(d)
RETURN c, d, job.started;