简体   繁体   中英

neo4jrestclient index results returns Iterable instead of node/url?

I'm trying to follow the docs http://readthedocs.org/docs/neo4j-rest-client/en/latest/indices.html

I expect the index query to return a node, but it returns an "Iterable: Node" instead:

db = GraphDatabase("http://localhost:7474/db/data")
playerIndex = db.nodes.indexes.get("index1")
playerNode = db.nodes.create(player_id = "Homer")
playerIndex.add("player_id", "Homer", playerNode)
print playerIndex["player_id"]["Homer"], "\n", playerNode

prints:

<Neo4j Iterable: Node>
<Neo4j Node: http://localhost:7474/db/data/node/157>

How can I get neo4jrestclient index query results to return a Node like the 2nd line?

Index lookups can return more than one node so in this case, it's returning an iterator. To get the next item in the iterator, do .next():

print playerIndex["player_id"]["Homer"].next(), "\n", playerNode

See: https://github.com/versae/neo4j-rest-client/blob/master/neo4jrestclient/iterable.py

As @espeed has pointed, call .next() will give you the next node in the index. You can also iterate over the iterator:

for node in playerIndex["player_id"]["Homer"]:
    print node, "\n", playerNode

Or just get all the results in the iterator using the slice [:] :

print playerIndex["player_id"]["Homer"][:], "\n", playerNode

The client behaves in this way because any index query always returns a list of elements, even if there is only one. So, the best weay to handle this is using an iterator with lazy load.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM