Zack
Zack

Reputation: 551

How can I find all nodes with a specific label using the Java API in Neo4j 2.0.x?

I need to find a specific node in the entire graph using the Java low level API. I used to do this using the Reference node in versions 1.x but that concept has been removed with the 2.0 release.

I thought I could use labels to do this: I would assign a label to this node (and only this node) when it is created. Subsequently I would get all the nodes with this particular label, which should return a single hit, ie the special node I'm looking for. Unfortunately I can't find a way to look up all nodes having a specific label using the Java API.

I am able to do it with Cypher but I'd like this look up to be as fast as possible, so saving the cost of query parsing, planning and execution would be great.

Upvotes: 7

Views: 3721

Answers (4)

Ventres
Ventres

Reputation: 31

The provided answer is deprecated. The preferred method is now to use: GraphDatabaseService.findNodes(Label label)

Upvotes: 3

LoveTW
LoveTW

Reputation: 3832

You can use GlobalGraphOperations.at(gdb).getAllRelationshipTypes() to get all nodes with the label, and gdb is your graph database.

Upvotes: 3

Dan G
Dan G

Reputation: 1118

GraphDatabaseService.findNodesByLabelAndProperty (Label label, String propName, String propValue) might suit your bill....

or you could save the id of the node when you create it initially, then you can call GraphDatabaseService.getNodeById(long id) - which would be, by far, the fastest way to retrieve a specific node.

Upvotes: 1

Lisa Li
Lisa Li

Reputation: 2592

This method GlobalGraphOperations.getAllNodesWithLabel(Label label) returns all nodes with the specified label.

Upvotes: 5

Related Questions