Mittenchops
Mittenchops

Reputation: 19664

Creating indexed nodes on neo4j cypher via rest api

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

Upvotes: 0

Views: 798

Answers (2)

Nigel Small
Nigel Small

Reputation: 4495

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.

Upvotes: 1

MrDosu
MrDosu

Reputation: 3435

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

Upvotes: 2

Related Questions