简体   繁体   中英

py2neo how to retrieve a node based on node's property?

I've found related methods:

  1. find - doesn't work because this version of neo4j doesn't support labels.
  2. match - doesn't work because I cannot specify a relation, because the node has no relations yet.
  3. match_one - same as match .
  4. node - doesn't work because I don't know the id of the node.

I need an equivalent of:

start n = node(*) where n.name? = "wvxvw" return n;

Cypher query. Seems like it should be basic, but it really isn't...

PS. I'm opposed to using Cypher for too many reasons to mention. So that's not an option either.

Well, you should create indexes so that your start nodes are reduced. This will be automatically taken care of with the use of labels, but in the meantime, there can be a work around.

  1. Create an index, say "label", which will have keys pointing to the different types of nodes you will have (in your case, say 'Person')
  2. Now while searching you can write the following query :

      START n = node:label(key_name='Person') WHERE n.name = 'wvxvw' RETURN n; //key_name is the key's name you will assign while creating the node. 

user797257 seems to be out of the game, but I think this could still be useful:

If you want to get nodes, you need to create an index. An index in Neo4j is the same as in MySQL or any other database (If I understand correctly). Labels are basically auto-indexes, but an index offers additional speed. (I use both).

somewhere on top, or in neo4j itself create an index:

index = graph_db.get_or_create_index(neo4j.Node, "index_name")

Then, create your node as usual, but do add it to the index:

new_node = batch.create(node({"key":"value"}))
batch.add_indexed_node(index, "key", "value", new_node)

Now, if you need to find your new_node, execute this:

 new_node_ref = index.get("key", "value")

This returns a list. new_node_ref[0] has the top item, in case you want/expect a single node.

use selector to obtain node from the graph The following code fetches the first node from list of nodes matching the search

selector = NodeSelector(graph)
node = selector.select("Label",key='value')
nodelist=list(node)
m_node=node.first()

using py2neo , this hacky function will iterate through the properties and values and labels gradually eliminating all nodes that don't match each criteria submitted. The final result will be a list of all (if any) nodes that match all the properties and labels supplied.

def find_multiProp(graph, *labels, **properties):
    results = None
    for l in labels:
        for k,v in properties.iteritems():
            if results == None:
                genNodes = lambda l,k,v: graph.find(l, property_key=k, property_value=v)
                results = [r for r in genNodes(l,k,v)]
                continue
            prevResults = results
            results = [n for n in genNodes(l,k,v) if n in prevResults]
    return results

see my other answer for creating a merge_one() that will accept multiple properties...

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