WildBill
WildBill

Reputation: 9291

How to find a particular node with neo4j/Java

I'd like to retrieve a node, or list of nodes, that have a certain label (or set of labels) or attribute (or set of attributes) via the embedded java framework for neo4j.

Looking at this question: Select a node by name in NEO4J in Java

It appears that you have to create an index if you ever want to search for a node with a certain attribute or label. However if I write my own cypher commands to do teh same thing I do not have to create indexes, I just perform a simple query as such:

Match (n:Entity:Person) return n;

I will simply get a list of nodes that have that label composition. Is this not easily done in embedded Java without creating an index every time?

Upvotes: 0

Views: 1129

Answers (1)

FrobberOfBits
FrobberOfBits

Reputation: 18002

If you have labels on your nodes like your query suggests, then there's an easy method to GraphDatabaseService#getNodesByLabelAndProperty(Label label, String key, Object value)

So yeah, you can do it.

Indexes are still advised though. Ask yourself, how is the database going to do this if you don't have an index? Well, likely it's going to go pull up a list of all nodes labeled in the way you specified, and then it's going to iterate through each and every one of them until it finds the right one. This is going to be very slow and inefficient. As a result, if you usually have to look up nodes by a certain property key/value, you're going to want an index. It isn't to make the lookup possible in the first place, it's to make it efficient.

Upvotes: 1

Related Questions