Tezra
Tezra

Reputation: 8833

Create a property index for all nodes in Cypher

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

Upvotes: 0

Views: 1080

Answers (1)

cybersam
cybersam

Reputation: 66967

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.

Upvotes: 3

Related Questions