0

I want to set the node ID like 0,1,2,3,.... instead of setProperty().

Node[] newno = new Node[265214];
for (int i = 0; i < newno.length; i++) {
     newno[i] = db.createNode();
     System.out.println(newno[i].getId());
}

It shows the following big ID number:

996660
996661
996662
996663
996656
996657
996658
996659
996668
996669
996670

While I want to make node for example the ID of newno[i] is just i. For large data sets, if every cycle calls getProperty() it will slow down execution. What is a more efficient method of accomplishing this?

Laurel
  • 5,965
  • 14
  • 31
  • 57
user3382017
  • 235
  • 9
  • 15

2 Answers2

1

The ids are automatically assigned by Neo4j, you cannot change this. Try using a property on the node if this is what you need.

  newno[i].setProperty("internal_id", i);
RaduK
  • 1,463
  • 10
  • 16
  • this is the only way? – user3382017 Apr 26 '14 at 19:56
  • Well, it is sure that you cannot change the ids generated by neo4j; if the id property doesn't suit your needs (which I don't know in details), you may find other types of data modeling that should. – RaduK Apr 28 '14 at 06:47
1

As pointed by @RaduK, neo4j sets the id of the node itself and does not allows the user to override this behaviour. Maybe something that they might do in future. However, if you are considering to use the neo4j generated id in your system, neo4j issued a warning on their page.

A node's id is unique, but note the following: Neo4j reuses its internal ids when nodes and relationships are deleted, which means it's bad practice to refer to them this way. Instead, use application generated ids.

So don't use the neo4j's id in your system.

That being said, you are mostly concerned about the fact that using property every time will result in delays. With Neo4J 2.x, they give you the option to set an index on your property so that the traversal is fast. Here is how you do that. Inside your code where you have the object of GraphDatabaseService, insert this piece of code:

try(Transaction transaction = graphDatabaseService.beginTx())
        {
            Schema schema = graphDatabaseService.schema();
            schema.indexFor(DynamicLabel.label("User")).on("internal_id").create();

            transaction.success();
        }

This code will tell neo4j that from now on, you have to make and maintain index on all nodes that are of type "User" on the property "internal_id".

Moreover, neo4j has an internal caching system which people usually refer to as warm cache. It means that your query will only take time the first time it runs. From then onwards, if you try the same query repeatedly, the query will be blazing fast.

Rash
  • 7,677
  • 1
  • 53
  • 74