Created
August 14, 2012 08:31
-
-
Save peterneubauer/3347559 to your computer and use it in GitHub Desktop.
Python basic index search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from neo4j import GraphDatabase | |
# Create db | |
db = GraphDatabase('db') | |
with db.transaction: | |
noun_id = 1 | |
#-----1----------------- | |
#first creating the index, reference node and storing it... | |
noun_index = db.node.indexes.create('noun_idx') | |
noun_ref = db.node() | |
noun_index['type']['noun_ref'] = noun_ref | |
#------2------------------ | |
#then later I use the reference node to add some new nodes and make sure they are "connected to" the ref-node | |
noun = db.node(id=noun_id) | |
noun.INSTANCE_OF(noun_ref) | |
#------3------------------- | |
#however the next time I run the code and want to actually grab the reference | |
#node from the index like this.... | |
noun_index = db.index().forNodes('noun_idx') | |
noun_ref = noun_index['type']['noun_ref'].single | |
#and then traverse the nouns connected to the reference node (code below in step #4) | |
#-----4------------- | |
for relationship in noun_ref.INSTANCE_OF: | |
noun = relationship.start | |
print noun | |
# Always shut down your database | |
db.shutdown() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just as an aside, there's a helper method in py2neo called
get_or_create_indexed_node
which can help reduce the amount of code required in this type of case:http://packages.python.org/py2neo/neo4j.html#py2neo.neo4j.GraphDatabaseService.get_or_create_indexed_node
Nige