1

我从 csv 导入时遇到问题。

我在 shell 中运行以下内容,最后一部分(MERGE (e1)-[:NEXT]->(hit))))永远不会发生。有点沮丧……

每个会话有 x 次点击。我想找到插入会话的最后一次命中,并通过 NEXT 关系将其与新命中连接

PSV 样本:

Session_id|date_time Xxx|2015-01-01T01:00:00 Xxx|2015-02-02T09:00:00 Yyy|2015-03-03T06:00:44

代码:

USING PERIODIC COMMIT 100
 LOAD CSV WITH HEADERS FROM 'file:///home/xxx.csv' AS line FIELDTERMINATOR '|'


 MERGE (session :Session { session_id:line.session_id })
 MERGE (hit:Hit{date:line.date_time})

//........更多合并......

//关系

CREATE (hit)-[:IN_SESSION]->(session) 
 CREATE ....//more relations

 WITH session

 MATCH (prev_hit:Hit)-[:IN_SESSION]->(session)
 WITH prev_hit ORDER BY prev_hit.date_time DESC LIMIT 2
 WITH collect(prev_hit) as entries

 FOREACH(i in RANGE(0, length(entries)-1) | 
   FOREACH(e1 in [entries[i]] | 
        MERGE (e1)-[:NEXT]->(hit)))
4

1 回答 1

3

我看不到您试图通过嵌套FOREACH循环实现什么。

如果你真的得到了hit节点和session节点,一个简单的MERGE应该没问题。我认为你必须包括hitWITH声明中。

MERGE (session :Session { id: "xxx" })
MERGE (hit:Hit { date_time:"2015-04-03T06:00:44" })
CREATE (hit)-[:IN_SESSION]->(session)
WITH session, hit
MATCH (prev_hit:Hit)-[:IN_SESSION]->(session)
WHERE prev_hit <> hit // make sure that you only match other hits
WITH hit, prev_hit 
ORDER BY prev_hit.date_time DESC LIMIT 1
MERGE (prev_hit)-[:NEXT]->(hit) // create relationship between the two 

更新

我将查询更新为仅匹配prev_hit不是当前命中的。上面的查询可以按您的意愿工作,即它创建与与相同NEXT的单个Hit节点相关的单个关系Session。见这里:http ://console.neo4j.org/?id=ov7mer

date_time 可能存在问题。我认为您将其存储为字符串,排序可能并不总是给您预期的结果。

更新 2

关于您的第二条评论:如果您逐行查看文件并添加Hit节点,则只能将关系添加到Hit已添加的节点。如果您想要节点之间的连续NEXT关系链,Hit则只能在一个查询中执行此操作,前提是您确保 CSV 文件的条目按 date_time 升序排列。

您可以稍后添加节点NEXT之间的关系,如下所述:http: //www.markhneedham.com/blog/2014/04/19/neo4j-cypher-creating-relationships-between-a-collection-of-nodes-invalid -输入/Hit

开始查询:

MATCH (s:Session)--(hit:Hit)
// first order by hit.date_time
WITH DISTINCT s, hit ORDER BY hit.date_time DESC
// this will return one row per session with the hits in a collection
WITH s, collect(hit) AS this_session_hits
// try this to check the ordering:
// RETURN s.session_id, this_session_hits

// the following queries will be done on each row, this is like iterating over the sessions
FOREACH(i in RANGE(0, length(this_session_hits)-2) | 
    FOREACH(e1 in [this_session_hits[i]] |
        FOREACH(e2 in [this_session_hits[i+1]] |
            MERGE (e1)-[:NEXT]->(e2))))

最终答案;)

此查询适用于 neo4j 控制台 ( http://console.neo4j.org/?id=mginka ) 中的数据集。它将Hit会话中的所有内容与NEXT关系联系起来。

MATCH (s:Session)<--(hit:Hit)
WITH DISTINCT s, hit
ORDER BY hit.date_time ASC 
WITH s, collect(hit) AS this_session_hits
FOREACH (i IN RANGE(0, length(this_session_hits)-2)| 
    FOREACH (e1 IN [this_session_hits[i]]| 
        FOREACH (e2 IN [this_session_hits[i+1]]| 
            MERGE (e1)-[:NEXT]->(e2))))
于 2015-05-18T20:21:34.807 回答