2013-01-16 7 views
0

py2neo에 새로운 소식이있어서 간단한 프로그램으로 시작하겠다고 생각했습니다. 그것의 반환 TypeError : 인덱스 iterable되지 않습니다. 어쨌든 노드 집합을 추가하고 중복을 피하면서 관계를 만들려고합니다. 내가 뭘 잘못하고 있는지 모르겠다.py2neo의 색인 오류, Neo4j

from py2neo import neo4j, cypher 
graph_db = neo4j.GraphDatabaseService("http://localhost:7474/db/data/") 

happy = "happy" 
glad = "glad" 
mad = "mad" 
irate = "irate" 
love = "love" 

wordindex = graph_db.get_or_create_index(neo4j.Node, "word") 

for node in wordindex: 
       wordindex.add("word", node["word"], node) 


def createnodes(a, b, c, d, e): 
nodes = wordindex.get_or_create(
    {"word": (a)}, 
    {"word": (b)}, 
    {"word": (c)}, 
    {"word": (d)}, 
    {"word": (e)}, 
    ) 

def createrel(a, b, c, d, e): 
rels = wordindex.get_or_create(
    ((a), "is", (b)), 
    ((c), "is", (d)), 
    ((e), "is", (a)), 
    ) 


createnodes(happy, glad, mad, irate, love) 
createrel(happy, glad, mad, irate, love) 

답변

2

여기서 Index.add 메서드로 시작하는 여러 가지 방법을 잘못 사용했습니다. 이 메소드는 기존 노드를 색인에 추가하는 데 사용해야하지만 코드의이 시점에서는 실제로 노드가 작성되지 않았습니다. 나는 다음과 같이 대신 Index.get_or_create 사용하려는 생각 : 노드가 고유성을 유지, 인덱스를 통해 직접 만들어지면

nodes = [] 
for word in [happy, glad, mad, irate, love]: 
    # get or create an entry in wordindex and append it to the `nodes` list 
    nodes.append(wordindex.get_or_create("word", word, {"word": word})) 

이것은 본질적으로 다음 createnodes 기능을 대체합니다. 다음과 같이

그런 다음 고유 위의 코드에 의해 얻은 노드 객체 플러스 GraphDatabaseService.get_or_create_relationships 방법과의 관계를 만들 수 있습니다

graph_db.get_or_create_relationships(
    (nodes[0], "is", nodes[1]), 
    (nodes[2], "is", nodes[3]), 
    (nodes[4], "is", nodes[0]), 
) 

희망이

나이젤

+0

감사 나이젤 데 도움이 매우 도움이! 나는 또한 슬라이드 쉐어에 대한 귀하의 프리젠 테이션이 매우 유용하다는 것을 발견했습니다. –