Search code examples
pythonneo4jcypherpy2neo

Cannot view the nodes I created using py2neo using a cypher query in the browser


I am creating nodes using py2neo as follows:

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

print graph_db.neo4j_version
graph_db.clear()

if not graph_db.get_index(neo4j.Node, "Kiran"):
        from py2neo import node,rel
        trial = graph_db.create(
        node(name="Kiran"),
        node(name="teja"),
        rel(0, "Brother", 1),
        )

#else:
details = graph_db.get_index(neo4j.Node, "Kiran")
print details

the get_index returns me some data like

Index(Node, u'http://localhost:7474/db/data/index/node/Kiran')

but when I search for the node on the browser, it returns me nothing... am I doing something wrong here?

also I am trying to publish some network information as follows :

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

def nodepublish(dpid, port, mac):
  if graph_db.get_index( neo4j.Node, dpid ) == None:
    create = graph_db.create({"DPID": dpid})
    print "switch "+str(dpid)+" added to graph"
  if graph_db.get_index( neo4j.Node, mac ) == None:
    query = neo4j.CypherQuery(graph_db, "MATCH (sw) WHERE sw.DPID = "+str(dpid)+" CREATE (nd {MAC: "+str(mac)+"}) CREATE (sw)-[:connected {PORT: "+str(port)+"}]->(nd)")
    print "node "+str(mac)+" added to graph"

When I call the nodepublish() function like

nodepublish(1,1,"aa:aa:aa:aa:aa:aa")

It creates a new node with dpid:1 every time instead of skipping the if statement once the get_index doesn't return None.

can someone help me out with this please?

Thanks!


Solution

  • Point no 1: make sure you have a trailing slash on your GraphDatabaseService URI. You may get incorrect results without it.

    Point no 2: you are using legacy indexes here. Be clear about which type of index you are using by reading this.

    I think you have mixed up indexes and index entries. An index (perhaps called People in this instance) points to a set of entries, each identified by a key-value pair. At each entry point, you may reference one or more nodes. Read more on legacy indexes here.

    You probably want your code to look more like this:

    from py2neo import neo4j 
    graph_db = neo4j.GraphDatabaseService("http://localhost:7474/db/data/")
    
    # Create a "People" index if one doesn't already exist
    people = graph_db.get_or_create_index(neo4j.Node, "People"):
    
    # Create two people nodes if they don't already exist
    kiran = people.get_or_create("name", "Kiran", {"name": "Kiran"})
    teja = people.get_or_create("name", "Teja", {"name": "Teja"})
    
    # Relate the two
    brothers, = graph_db.create((kiran, "BROTHER", teja))
    
    print kiran
    print teja
    print brothers
    

    This page might help with some of the details in the code as it describes the legacy index functions that you need here.