2
votes

I have some database with nodes. Each node has an internal id that is, by default, automatically generated by the Neo4j database when the node is created.

Is it possible to copy this id to properties? I mean a following effect:

MATCH (n)
WHEN id(n)=4
RETURN n

It returns.

n{id : 4}

I need do this transformation for each node. Any ideas? Moreover, is it good idea to create an index on this property if I am going to search on it? How can I do this?

3

3 Answers

0
votes

This is possible.

I would ask though: are you sure this is the best solution for your use case? The reason I ask is that if a node gets deleted, its original ID may get reused again for a different node. This can cause unexpected results depending on how your application/queries are going to utilize the ID.

The way to map an internal id to a property for all nodes is the following:

match (n) set n.id = id(n)

An index will make your searches faster when you search on the ID property - but is it a good idea? It's possible that it is a good idea but it depends on how important speed is to you, how large your database is, how frequently nodes will be created & deleted (the more frequently this occurs, the more storage/overhead your index is going to require), and how much storage space you have available on your database.

Indexes in Neo4j are mapped to a label - they can be single property indexes or composite (multiple property) indexes. Let's say you want to create an index for the ID property for those nodes with the label of :Person - you can achieve this with the following:

CREATE INDEX ON :Person(id)

If you find that the index was not a good idea, you could always just drop the index - see the following:

DROP INDEX ON :Person(id)

If you'd like, you can read more about indexes in Neo4j's official developer manual.

0
votes

You never "need" to store the native ID of a node as a property of that node.

You can readily get the native ID for every node (and relationship) via the ID() function, as you already know.

You can also readily get a node (or relationship) by its native ID -- without an index (which is not even supported for relationships). In fact, a query by native ID is even more efficient than indexing. For example, your query (after replacing WHEN with the proper WHERE) is already as fast as it can be:

MATCH (n)
WHERE ID(n) = 4
RETURN n;

NOTE: in the above example, you should not assume that you will always get back the same node when you query for a native ID of 4. If the original node with that ID were to be deleted, then the same ID could be assigned to a new node afterwards. So, you need to carefully consider if relying on native IDs is appropriate for your use case.

0
votes

It's not a good idea to use neo4j id property. If you delete some node after a while neo4j may use it for new nodes. For the reason I mentioned and other reasons it's not recommended to use id. You can use uuid or other approach to create unique id. There is github project can help you.

You can copy id like this:

MATCH (n) SET n.id = id(n)

Create a single index:

CREATE INDEX ON :Person(id)

Create a composite index:

CREATE INDEX ON :Person(id, firstname)

For more information about indexing read docs.