我有两个表事件和位置。
我已将它们下载到csv文件中。
我运行以下查询来创建节点:
USING PERIODIC COMMIT 10000
LOAD CSV WITH HEADERS FROM "file:////path/tb_location.csv" AS row
CREATE(:tb_location{ latitude: row.latitude,longitude: row.longitude,location_description:
row.location_description,community_area: row.community_area,block: row.block,district: row.district,ward: row.ward,x_coordinate: row.x_coordinate,y_coordinate: row.y_coordinate,id_city: row.id_city,composite_key: row.composite_key });
USING PERIODIC COMMIT 10000
LOAD CSV WITH HEADERS FROM "file:////path/tb_incident.csv" AS row
CREATE(:tb_incident{ id: row.id,primary_type: row.primary_type,domestic: row.domestic,date: row.date,description: row.description,arrest: row.arrest,beat: row.beat,year: row.year,updated_on: row.updated_on,latitude : row.latitude,longitude: row.longitude,case_number: row.case_number,composite_foreign_key: row.composite_foreign_key});
然后我在匹配属性上创建索引:
CREATE INDEX ON :tb_incident(composite_foreign_key);
CREATE INDEX ON :tb_location(composite_key);
然后我尝试建立关系:
USING PERIODIC COMMIT 10000
LOAD CSV WITH HEADERS FROM "file:////path/tb_incident.csv" AS row1
MATCH(tb_incident:tb_incident{composite_foreign_key: row1.composite_foreign_key})
LOAD CSV WITH HEADERS FROM "file:////path/tb_location.csv" AS row2
MATCH(tb_location:tb_location{composite_key: row2.composite_key})
WHERE tb_incident.composite_foreign_key = tb_location.composite_key
MERGE (tb_incident)-[:occured_at]->(tb_location);
但是,上次查询会将一个事件链接到所有位置。 我是新来的密码,我无法找到我做错了什么。我打算只将一个事件与一个地点联系起来。 如果您可以帮我纠正这个不正确的查询,请提供帮助。
答案 0 :(得分:1)
由于您已导入所有数据,因此无需再使用CSV文件。这可能是导致你的问题的原因。
请改为尝试:
MATCH (tb_incident:tb_incident), (tb_location:tb_location)
WHERE tb_incident.composite_foreign_key = tb_location.composite_key
MERGE (tb_incident)-[:occured_at]->(tb_location);
该查询的复杂度为N * M(其中N是tb_incident
个节点的数量,M是tb_location
个节点的数量),因此可能需要一段时间。不幸的是,neo4j在比较节点之间的值时不使用索引。
[增订]
从第二个CSV文件导入时,您不仅可以创建每个tb_incident
节点,还可以创建相应的关系。它使用的MATCH
应能够使用索引,因为它不需要比较节点之间的值。这意味着此步骤的复杂性降低到N:
USING PERIODIC COMMIT 10000
LOAD CSV WITH HEADERS FROM "file:////path/tb_incident.csv" AS row
CREATE(tb_incident:tb_incident{ id: row.id,primary_type: row.primary_type,domestic: row.domestic,date: row.date,description: row.description,arrest: row.arrest,beat: row.beat,year: row.year,updated_on: row.updated_on,latitude : row.latitude,longitude: row.longitude,case_number: row.case_number,composite_foreign_key: row.composite_foreign_key})
WITH tb_incident, row.composite_foreign_key AS cfk
MATCH (tb_location:tb_location{composite_key: cfk})
MERGE (tb_incident)-[:occured_at]->(tb_location);