Neo4j: How ot restrict auto id generation and generate your own specified Id through Cypher

1,376 views
Skip to first unread message

Ankit Agarwal

unread,
Jan 19, 2014, 7:46:50 AM1/19/14
to ne...@googlegroups.com
I am a Database guy but agnostic to Java/.NET and rely on query language (Cypher in this case). I am trying to create some nodes and add relationship between them. I want to assign my own id value to the nodes, however I don't know how to do that because neo4j automatically assigns id value to all nodes. For example these are my create node statements where I am trying to specify my own identifier:

CREATE (`833`:CATEGORY {PartnerCode:"XXXXX002507", MerchantCode:"XXXX_0001", Name:'ABCD', Leaf:1 ,CategoryCode:"100-175858"}) 
CREATE (`835`:CATEGORY {PartnerCode:"ZZZZZ0002507", MerchantCode:"ZZZZ_0001", Name:'DEFT', Leaf:1 ,CategoryCode:"100-175857"}) 

When I browse these, I don't see 833 and 835 as their identifiers, rather I see auto-generated identifiers. Why? Is there a way I can enforce my own Id values ?

Secondly, how to assign relationships between these 2 Nodes ? For instance I want to make '833' :PARENT_OF '835'

Thanks

Michael Hunger

unread,
Jan 19, 2014, 4:02:54 PM1/19/14
to ne...@googlegroups.com
There is no built-in method to assign custom generated id's.

in your case 833, 835 are just variable names within the query.

If you want to generated your custom identifiers, you can either do so externally or use a locking mechanism workaround to do that.

something like.

1. Create a unique constraint in a separate call.

create constraint (n:Ids) assert n.name is unique;

2. In your queries use, get or create the "Ids" reference node and increment the "id" property
then use the value of that property to do your own custom identifiers

MERGE (ids:Ids {name:"ids"}) ON CREATE SET ids.id = 1 ON MATCH SET ids.id = ids.id + 1
CREATE (`833`:CATEGORY {MyCustomId:"XXX-"+ids.id, PartnerCode:"XXXXX002507", MerchantCode:"XXXX_0001", Name:'ABCD', Leaf:1 ,CategoryCode:"100-175858"}) 

HTH

Michael


--
You received this message because you are subscribed to the Google Groups "Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email to neo4j+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages