You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
2.2 KiB

from py2neo import Graph
import json
from Config import *
def query_all_questions():
# Neo4j连接配置
neo4j_config = {
"uri": NEO4J_URI, # 从Config导入
"auth": NEO4J_AUTH # 从Config导入
}
# 初始化图数据库连接
graph = Graph(**neo4j_config)
try:
# 获取所有试题ID
question_ids = graph.run(
"MATCH (q:Question) RETURN q.id AS question_id"
).data()
results = []
# 遍历每个试题
for record in question_ids:
qid = record['question_id']
# 修改查询部分
data = graph.run("""
MATCH (q:Question {id: $qid})
OPTIONAL MATCH (q)-[:REQUIRES_KNOWLEDGE]->(kp:KnowledgePoint)
OPTIONAL MATCH (q)-[:DEVELOPS_LITERACY]->(lp:LiteracyNode)
RETURN
q.content AS content,
collect(DISTINCT {id: kp.id, name: kp.name}) AS knowledge_points,
collect(DISTINCT {id: lp.value, title: lp.title}) AS literacy_points
""", qid=qid).data()
if data:
result = {
"question_id": qid,
"content": data[0]['content'],
# 保留原有结构
"knowledge_points": data[0]['knowledge_points'], # 现在每个元素包含id+name
"literacy_points": data[0]['literacy_points'] # 现在每个元素包含id+title
}
results.append(result)
# 增强版输出展示
print(f"\n试题ID: {qid}")
print(f"知识点: {[kp['name'] for kp in result['knowledge_points']]}")
print(f"知识点ID: {[kp['id'] for kp in result['knowledge_points']]}")
print(f"素养点: {[lp['title'] for lp in result['literacy_points']]}")
print(f"素养点ID: {[lp['id'] for lp in result['literacy_points']]}")
except Exception as e:
print(f"❌ 查询失败: {str(e)}")
if __name__ == '__main__':
query_all_questions()