0

I have 1 rule for my database. EVERYTHING must have an 'id'. Since this is my primary key field, I want to index it, but it seems the only way to create an index is to specify a label.

So with this dataset

CREATE (:TEST1{id:"<uuid>"}),
(:RAWR{id:"<uuid>"}),
(:FOO:BAR{id:"<uuid>"}),
({id:"<uuid>"})

I would like to use an index to find the unlabeled node by its UUID.

Is this possible in Cypher? Or is my ONLY option to inject a 'node' label onto EVERYTHING entering the database? (It feels wrong to create a label and then assign it to everything. And to hijack all create requests to add that additional label feels like I'm asking for trouble.)

Tezra
  • 8,463
  • 3
  • 31
  • 68
  • Yes, you need a label to create an index. Reconsider setting an id on everything. The uuid is a technical id, it can be reused to another node after you delete a node. Do not use it. – Jerome_B Jun 07 '17 at 21:17
  • I saw the is an id generator in apoc. You may have a batch query running regularly to create your id field (are you sure?) with a value from apoc, that handles the sequence. – Jerome_B Jun 07 '17 at 21:21
  • @JeromeB I'm not really concerned with assigning the id, nor do they actually need to be unique. Just knowing that there is an id field, and I have the value of that field, but not the nodes' labels, I'd like to be able to quickly find nodes by that id instead of scanning through all of them, even if that node has not yet been assigned a label. – Tezra Jun 08 '17 at 18:14
  • should your list of labels be limited and known ... an option is to create an index per label (via a constraint) then do a UNION query. Means you can have two nodes of different labels with the same id. – Jerome_B Jun 08 '17 at 19:37
  • @JeromeB but what if the node I want to look up has no labels? – Tezra Jun 08 '17 at 19:53
  • in that case ... wont work However, why would a node have no label ? – Jerome_B Jun 08 '17 at 20:08
  • @JeromeB because some else created them without labels, and I want to add labels to them some time after. – Tezra Jun 08 '17 at 20:27
  • You (or the DB admin) can add label(s) on nodes based on the properties present (use apoc). – Jerome_B Jun 08 '17 at 21:37

1 Answers1

3

A node can have multiple labels. So, in addition to your existing labels, you could assign a common label to all your nodes and then create an index using that common label and id.

However, since you intend the id value to be globally unique, instead of creating an index you should create a uniqueness constraint (which automatically creates an index for you as a side effect). That would tell neo4j to enforce id uniqueness for you.

cybersam
  • 63,203
  • 6
  • 53
  • 76