#!/bin/bash
# Neo4j 数据导入脚本

NEO4J_HOME="/data/neo4j/neo4j-community-4.4.46"
CYPHER_SHELL="$NEO4J_HOME/bin/cypher-shell"

# 默认用户名和密码（首次登录后需要修改）
USERNAME="neo4j"
PASSWORD="neo4j"

echo "开始导入数据到 Neo4j..."

# 1. 创建约束和索引
echo "步骤 1/4: 创建约束和索引..."
$CYPHER_SHELL -u $USERNAME -p $PASSWORD <<EOF
CREATE CONSTRAINT node_name_unique IF NOT EXISTS FOR (n:Interest) REQUIRE n.name IS UNIQUE;
CREATE INDEX node_level_index IF NOT EXISTS FOR (n:Interest) ON (n.level);
CREATE INDEX node_name_index IF NOT EXISTS FOR (n:Interest) ON (n.name);
EOF

# 2. 导入节点
echo "步骤 2/4: 导入节点..."
$CYPHER_SHELL -u $USERNAME -p $PASSWORD <<EOF
USING PERIODIC COMMIT 1000
LOAD CSV WITH HEADERS FROM 'file:///nodes.csv' AS row
CREATE (n:Interest {
    name: row.name,
    level: row.level,
    parent_path: row.parent_path,
    topic_count: toInteger(coalesce(row.topic_count, '0')),
    source_file: row.source_file,
    definition: row.definition,
    keywords: row.keywords,
    keyword_count: toInteger(coalesce(row.keyword_count, '0'))
});
EOF

# 3. 导入关系
echo "步骤 3/4: 导入关系..."
$CYPHER_SHELL -u $USERNAME -p $PASSWORD <<EOF
USING PERIODIC COMMIT 1000
LOAD CSV WITH HEADERS FROM 'file:///relationships.csv' AS row
MATCH (parent:Interest {name: row.parent})
MATCH (child:Interest {name: row.child})
MERGE (parent)-[r:PARENT_OF]->(child)
ON CREATE SET r.created_at = datetime();
EOF

# 4. 显示统计信息
echo "步骤 4/4: 统计信息..."
$CYPHER_SHELL -u $USERNAME -p $PASSWORD <<EOF
MATCH (n:Interest)
RETURN count(n) as total_nodes;

MATCH ()-[r:PARENT_OF]->()
RETURN count(r) as total_relationships;
EOF

echo "导入完成！"





