0

I'm trying to create an indexed node in cypher, with the following syntax:

neo4j-sh (?)$ start m=node:person(UID= "1")  return m;
==> +------------+
==> | m          |
==> +------------+
==> | Node[64]{} |
==> +------------+
==> 1 row
==> 0 ms
==> 
neo4j-sh (?)$ start n = node(64) return n.UID;
==> EntityNotFoundException: The property 'UID' does not exist on Node[64]

Why is it that the node appears to be created, but the property I'm creating, and that seems to be successfully returned, does not exist?

Is there an easier way? I used to use py2neo's function:

neo4j.GraphDatabaseService("http://localhost:7474/db/data/").get_or_create_indexed_node(index='person', key='UID', value=self.id, properties={'UID' : self.id})

But this appears to have been deprecated---it no longer works with the latest version of py2neo, which does not appear to support the properties argument any longer (and for future folks, index is replaced with index_name).

Mittenchops
  • 18,633
  • 33
  • 128
  • 246

2 Answers2

2

An index and a property are two different things.

It seems you have a node in your graph with an index named person and key/value pair UID:"1". Your first query gets your node by its index. But the index is not a property of the node. You can fire up the webadmin to visualize how the indices are managed in your graph.

As far as i know there is nothing in the documentation on how to create indices with cypher, but you can easily use the REST API to manipulate them (link).

MrDosu
  • 3,427
  • 15
  • 18
  • Isn't the index "person," as given by `start n node:/person/`? The properties are defined in parens after. Py2neo uses the REST API under the hood to have created the index. – Mittenchops Apr 18 '13 at 13:50
  • Yes, the above query uses the index named person looking up the key/value pair UID:"1". While in practice the key/value pairs are often created to reflect node properties this is not necessary as far as i know. If you look at the REST index documentation (http://docs.neo4j.org/chunked/stable/rest-api-indexes.html) you can easily create key/value indices that have nothing to do with the nodes properties. – MrDosu Apr 18 '13 at 14:48
  • I think I don't understand---are you saying my syntax is not creating a key/value on an index, or not creating a node at all, or not creating a property on an indexed node? – Mittenchops Apr 18 '13 at 16:45
  • I mean the cypher statement is just a query. What you want to do is CREATE UNIQUE (http://assets.neo4j.org/download/Neo4j_CheatSheet_v3.pdf), but i dont know if you can index nodes at all with cypher (unless auto indexing). – MrDosu Apr 19 '13 at 10:10
1

The method has not been deprecated and the properties argument is still valid, as before. The only change, as you identify, is the change from index to index_name for the first argument.

The documentation is here:

http://book.py2neo.org/en/latest/graphs_nodes_relationships.html#py2neo.neo4j.GraphDatabaseService.get_or_create_indexed_node

Note that the properties will only actually be used when the node does not already exist, i.e. when doing a 'create' but not when doing a 'get'. Otherwise, the existing node will remain as-is.

Nigel Small
  • 4,475
  • 1
  • 17
  • 15