5

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.

puredevotion
  • 1,135
  • 1
  • 11
  • 27
  • So, if you want not to use cypher, maybe use a server plugin, http://docs.neo4j.org/chunked/milestone/server-plugins.html ? – Peter Neubauer Oct 03 '13 at 21:27
  • @PeterNeubauer well, if I was to write in Java, I'd just embed `neo4j`. I was sort of hoping to save that trouble for the worst case scenario :) –  Oct 03 '13 at 22:19
  • Just index your nodes if you don't want to use cypher. And the methods you mention use cypher under the hood, so I don't know what you would gain from another method wrapping your specific query if you don't want to use cypher. – jjaderberg Nov 11 '13 at 18:57
  • @jjaderberg I would gain a privilege of not using cypher, which would both be a more general code and would spare me the "pleasure" of dealing with this language :) Indexing is 1) taxing, 2) very taxing when the number of features is large. Solving the lack of proper API by damaging the database isn't really a good idea, not in the general case at least. –  Nov 11 '13 at 19:07
  • @wvxvw I doubt indexing will hurt more than binding every node in the database and test for a property value, especially as the database grows. I thought py2neo had some equivalent of java API:s GlobalGraphOperations to let you grab 'everything', but it's been a while and I assume you have read the documentation. – jjaderberg Nov 11 '13 at 19:37
  • @jjaderberg well, you doubt, I don't. And the discussion ends here. Sorry, these comments are only getting further from the issue in the OP. –  Nov 11 '13 at 20:59
  • @wvxvw I think our comments are on topic, but if you feel strongly about it you can delete yours and flag mine. I would recommend instead that you edit your question to add that you don't want to use indices to retrieve nodes by property value and that you don't want to write your own server extension. That would enhance your question and make it more likely you would get a relevant answer. – jjaderberg Nov 11 '13 at 21:25

4 Answers4

1

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.
    
gaurav.singharoy
  • 3,751
  • 4
  • 22
  • 25
  • 1
    Well, you wrote it using Cypher... which defies the reason I asked the question + royalty-free version of neo4j doesn't allow labels, so that's not a great solution really. –  Nov 11 '13 at 17:51
  • The answer doesn't make use of labels, it uses an index called "label". And what do you mean about royalties and using labels? – jjaderberg Nov 11 '13 at 18:52
  • The syntax `start x = y:z(...)` is not allowed in this version of neo4j, whatever the name, but it doesn't matter, because I already can write a Cypher query that does what I need, I don't want to write it at all. The reason I asked the question is to find out whether there is already an API to spare me this work. –  Nov 11 '13 at 19:10
1

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.

puredevotion
  • 1,135
  • 1
  • 11
  • 27
  • this is now outdated for the [recent implementation](http://neo4j.com/blog/py2neo-2-0-unleashed/) of py2neo. These functions are now all found in the legacy class – Alexander McFarlane Jun 04 '15 at 04:33
1

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()
aradhyamathur
  • 340
  • 7
  • 21
0

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...

Community
  • 1
  • 1
Alexander McFarlane
  • 10,643
  • 9
  • 59
  • 100