2
votes

As per new indexing rules, the auto_index will go away in future and its expected to create indexes using cypher. According to this new way, to index a node property, you MUST provide a Node Label.

I have a 'nodeId' property present on all types of Node Labels - User, Employee, Bank, Car, etc. I used to auto-index this property to retrieve any type of node if its nodeId is known. Please note that since auto-index did not require me to give a Node Label, it was possible for me to do what I did.

ReadableIndex<Node> readableIndex = this.graphDatabaseService.index().getNodeAutoIndexer().getAutoIndex();
readableIndex.get("nodeId", "0").getSingle();

But with new style, I have to create index on nodeId property for each and every Node Label. So I have to do this:

create index on :User(nodeId)
create index on :Employee(nodeId)
...

Moreover, my method getByNodeId(String nodeId) is useless now because this cypher query IMHO will not be able to use the index anymore since I am not passing any node label.

match (node) where node.nodeId = {nodeId} return node;

Since the whole point of my getByNodeId() method was to be generic across all nodes, I cannot give this cypher query a node label. So what should I do here. My 2 questions are:

  • How do I tell neo4j via cypher to index on all node labels
  • How do I write a cypher query which uses index not based on node label, but based on node property.

Note:

  • It is essential for me to use cypher because I am using neo4j-jdbc and they have no method to create auto-index or access the auto-indexer (atleast not that I know of).

  • Some might suggest me to change the neo4j.properties to enable auto-indexing there, but I dont like changing configuration files. I want to do it in my program. Anyway, that would have only solved the first issue. Second issue is still there.

1
Thanks for asking this! I was about to ask the exact same thing.ADTC

1 Answers

3
votes

A node can have multiple labels.

Thus, if you make all your nodes share a common label, say Base (in addition to whatever labels they currently have), you can just have a single index that covers all your nodes:

CREATE INDEX ON :Base(nodeId)