2014-10-05 57 views
0

我试图创建一个存储节点之间基于时间的迭代的图。我希望节点是唯一的,节点间的关系在给定timestamp属性时是唯一的。py2neo具有唯一关系的唯一节点给定时间戳

我的第一次尝试创建2个节点和1个关系,这不是我想要的。

from py2neo import neo4j, node, rel 

graph_db = neo4j.GraphDatabaseService() 
graph_db.get_or_create_index(neo4j.Node, "node_index") 

batch = neo4j.WriteBatch(graph_db) 

# a TALKED_TO b at timestamp 0 
batch.get_or_create_indexed_node('node_index', 'name', 'a', {'name': 'a'}) 
batch.get_or_create_indexed_node('node_index', 'name', 'b', {'name': 'b'}) 
batch.get_or_create_indexed_relationship('rel_index', 'type', 'TALKED_TO', 0, 'TALKED_TO', 1, {"timestamp": 0}) 

# a TALKED_TO b at timestamp 1 
batch.get_or_create_indexed_node('node_index', 'name', 'a', {'name': 'a'}) 
batch.get_or_create_indexed_node('node_index', 'name', 'b', {'name': 'b'}) 
batch.get_or_create_indexed_relationship('rel_index', 'type', 'TALKED_TO', 3, 'TALKED_TO', 4, {"timestamp": 1}) 

# a TALKED_TO b at timestamp 2 
batch.get_or_create_indexed_node('node_index', 'name', 'a', {'name': 'a'}) 
batch.get_or_create_indexed_node('node_index', 'name', 'b', {'name': 'b'}) 
batch.get_or_create_indexed_relationship('rel_index', 'type', 'TALKED_TO', 6, 'TALKED_TO', 7, {"timestamp": 0}) 

results = batch.submit() 
print results 

#[Node('http://localhost:7474/db/data/node/2'), 
#Node('http://localhost:7474/db/data/node/3'), 
#Relationship('http://localhost:7474/db/data/relationship/0'), 
#Node('http://localhost:7474/db/data/node/2'), 
#Node('http://localhost:7474/db/data/node/3'), 
#Relationship('http://localhost:7474/db/data/relationship/0'), 
#Node('http://localhost:7474/db/data/node/2'), 
#Node('http://localhost:7474/db/data/node/3'),  
#Relationship('http://localhost:7474/db/data/relationship/0')] 

我的第二次尝试创建2个节点和0的关系,不知道为什么它不产生任何关系。

from py2neo import neo4j, node, rel 

graph_db = neo4j.GraphDatabaseService() 
graph_db.get_or_create_index(neo4j.Node, "node_index") 

batch = neo4j.WriteBatch(graph_db) 

# a TALKED_TO b at timestamp 0 
batch.get_or_create_indexed_node('node_index', 'name', 'a', {'name': 'a'}) 
batch.get_or_create_indexed_node('node_index', 'name', 'b', {'name': 'b'}) 
batch.create(rel(0, 'TALKED_TO', 1, {"timestamp": 0})) 

# a TALKED_TO b at timestamp 1 
batch.get_or_create_indexed_node('node_index', 'name', 'a', {'name': 'a'}) 
batch.get_or_create_indexed_node('node_index', 'name', 'b', {'name': 'b'}) 
batch.create(rel(3, 'TALKED_TO', 4, {"timestamp": 1})) 

# a TALKED_TO b at timestamp 2 
batch.get_or_create_indexed_node('node_index', 'name', 'a', {'name': 'a'}) 
batch.get_or_create_indexed_node('node_index', 'name', 'b', {'name': 'b'}) 
batch.create(rel(6, 'TALKED_TO', 7, {"timestamp": 0})) 

results = batch.submit() 
print results 

#[Node('http://localhost:7474/db/data/node/2'), 
#Node('http://localhost:7474/db/data/node/3'), 
#None] 

那么如何实现下图中描述的内容呢?

enter image description here

回答

0

好了,所以我想我想通了,但我不知道,如果它的高效。有谁知道比以下更好的方法吗?

# Create nodes a and b if they do not exist. 
query = """MERGE (p:Person { name: {name} }) RETURN p""" 
cypher_query = neo4j.CypherQuery(neo4j_graph, query) 
result = cypher_query .execute(name='a') 
result = cypher_query .execute(name='b') 

# Create a relationship between a and b if it does not exist with the given timestamp value. 
query = """ 
    MATCH (a:Person {name: {a}}), (b:Person {name: {b}}) 
    MERGE (a)-[r:TALKED_TO {timestamp: {timestamp}}]->(b) 
    RETURN r 
""" 
cypher_query = neo4j.CypherQuery(neo4j_graph, query) 
result = cypher_query.execute(a='a', b='b', timestamp=0) 
result = cypher_query.execute(a='a', b='b', timestamp=1)