Neo4j - 模型节点和关系

时间:2017-01-25 12:23:34

标签: neo4j model cypher

我以{company:[c1,c2,c3],指定:[d1,d2,d3]}的格式获得了100000个员工数据,其中c1是员工的第一家公司,c2是第二家公司,c3是第三家公司... 这同样适用于员工在这些公司的指定。 这里的交换意味着员工从c1-> c2或d1-> d2切换。

我希望在neo4j中对此进行建模以回答以下问题:

  1. 获取公司开关,例如。 c1-> c2,c2-> c3
  2. 获取指定开关,例如。 d1-> d2,d2-> d3。
  3. 对于公司开关c1-> c2什么是指定开关d1-> d2。
  4. 我尝试了这段代码,但即使我已编入索引,它也会一直运行。

    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))))
    

    此处序列是用户/员工持有的职位序列

1 个答案:

答案 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;