2014-10-05 105 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)